Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- String server = "HOST";
- int port = 21;
- String user = "USER";
- String pass = "PASS";
- FTPSClient ftpClient;
- try {
- ftpClient = new FTPSClient();
- try {
- ftpClient.connect(server, port);
- ftpClient.login(user, pass);
- ftpClient.enterLocalPassiveMode();
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- // APPROACH #1: uploads first file using an InputStream
- File firstLocalFile = new File("TEST1.CSV");
- String firstRemoteFile = "TEST1.txt";
- InputStream inputStream = new FileInputStream(firstLocalFile);
- System.out.println("Start uploading first file");
- boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
- inputStream.close();
- if (done) {
- System.out.println("The first file is uploaded successfully.");
- }
- // APPROACH #2: uploads second file using an OutputStream
- File secondLocalFile = new File("TEST2.CSV");
- String secondRemoteFile = "TEST2.TXT";
- inputStream = new FileInputStream(secondLocalFile);
- System.out.println("Start uploading second file");
- OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
- byte[] bytesIn = new byte[4096];
- int read = 0;
- while ((read = inputStream.read(bytesIn)) != -1) {
- outputStream.write(bytesIn, 0, read);
- }
- inputStream.close();
- //outputStream.close();
- boolean completed = ftpClient.completePendingCommand();
- if (completed) {
- System.out.println("The second file is uploaded successfully.");
- }
- } catch (IOException ex) {
- System.out.println("Error: " + ex.getMessage());
- ex.printStackTrace();
- } finally {
- try {
- if (ftpClient.isConnected()) {
- ftpClient.logout();
- ftpClient.disconnect();
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- } catch (NoSuchAlgorithmException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
Add Comment
Please, Sign In to add comment