Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- String server = "ftps-url";
- int port = 6321;
- String user = "";
- String pass = "";
- FTPSClient ftp = null;
- try {
- ftp = new FTPSClient("SSL");
- ftp.setAuthValue("SSL");
- ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
- int reply;
- ftp.connect(server, port);
- System.out.println("Connecting");
- System.out.print(ftp.getReplyString());
- // After connection attempt, you should check the reply code to verify success.
- reply = ftp.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftp.disconnect();
- System.err.println("FTP server refused connection.");
- System.exit(1);
- }
- ftp.login(user, pass);
- ftp.execPBSZ(0);
- ftp.execPROT("P");
- // ... // transfer files
- ftp.setBufferSize(1000);
- ftp.enterLocalPassiveMode();
- // ftp.setControlEncoding("GB2312");
- ftp.changeWorkingDirectory("/output"); //path where my files are
- ftp.setFileType(FTP.BINARY_FILE_TYPE);
- //System.out.println("Remote system is " + ftp.getSystemName());
- String[] filelist = ftp.listNames(); //returns null
- System.out.println(filelist.length);
- System.out.println(Arrays.toString(filelist));
- // method 1
- File inFile = new File("myfile.xls");
- if (!inFile.exists()) {
- inFile.createNewFile();
- }
- InputStream input = new FileInputStream(inFile);
- ftp.completePendingCommand();
- ftp.storeFile(filelist[0], input);
- input.close();
- // method 2
- FileOutputStream fos = new FileOutputStream("myfile.xls");
- ftp.retrieveFile(filelist[0], fos);
- fos.flush();
- fos.close();
- //ftp.completePendingCommand();
- // method 3
- File path = new File("C:/Users/user/Desktop/");
- String remoteFilePath = "/output/" + filelist[0];
- File resultFile = new File(path, filelist[0]);
- CheckedOutputStream fout = new CheckedOutputStream(new FileOutputStream(resultFile), new CRC32());
- ftp.retrieveFile(remoteFilePath, fout);
- } catch (Exception ex) {
- System.out.println("error");
- ex.printStackTrace();
- } finally {
- // logs out and disconnects from server
- try {
- if (ftp.isConnected()) {
- ftp.logout();
- ftp.disconnect();
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement