I implemented java code to upload files to server with org.apache.commons.net.ftp.FTPClient (commons-net-3.3.jar).
Following is example code:
// create instance of FTPClient FTPClient ftp = new FTPClient(); ftp.setControlEncoding("UTF-8"); ftp.setDefaultTimeout(30000); // connect to server try { ftp.connect("10.1.1.1", 21); } catch(Exception e) { System.out.println("Cannot connect to server"); return; } // login to server if (!ftp.login("username", "password")) { ftp.logout(); System.out.println("Cannot login to server"); return; } try { ftp.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE); ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE); ftp.enterLocalPassiveMode(); } catch(Exception e) { } // create directory on server // dirs is list of required directories on server for (String dir : dirs) { try { ftp.makeDirectory(dir); } catch(IOException e) { } } // files is a map of local file and string of remote file // such as // file on client is "C://test/a.txt" // location on server is "/test/a.txt" for (Map.Entry<File, String> entry : files.entrySet()) { File localFile = entry.getKey(); String remoteFile = entry.getValue(); InputStream input = null; try { input= new FileInputStream(localFile); ftp.storeFile(remoteFile, input); } catch (Exception e) { try { ftp.deleteFile(remoteFile); } catch (IOException e1) { } } finally { if (input != null) { try { input.close(); } catch (IOException e) { } } } } // disconnect if (ftp != null && ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException f) { // do nothing } }
I try to upload 100 files (each file is about 2-10 KB). It took about 174375 - 210203 millisec. I would like to improve the speed.
How should I do?
- Change library? What is the powerful FTP client class library for uploading multiple files (only open source or free library)?
- Use multiple threads? How can I implement ftp upload function with multiple threads? Could someone show me an example? I am a new for multiple threading programming.
I try to use this code for multiple threads:
final CountDownLatch latch = new CountDownLatch(files.size()); ExecutorService pool = Executors.newFixedThreadPool(5); final org.apache.commons.net.ftp.FTPClient client = ftp; for (Map.Entry<File, String> entry : files.entrySet()) { final File localFile = entry.getKey(); final String remoteFile = entry.getValue(); pool.execute(new Runnable() { public void run() { try { uploadFile(client, localFile, remoteFile); } catch (Exception e) { } finally { latch.countDown(); } } }); } try { // waiting for all threads finish latch.await(); } catch (Exception e) { }
uploadFile method:
private void uploadFile(org.apache.commons.net.ftp.FTPClient ftp, File payLoad, String remoteLoc) { InputStream input = null; try { input = new FileInputStream(payLoad); ftp.storeFile(remoteLoc, input); } catch (Exception e) { try { ftp.deleteFile(remoteLoc); } catch (Exception e1) { } e.printStackTrace(); } finally { if (input != null) try { input.close(); } catch (Exception e) { } } }
Is it correct?
Can anyone help me?
Thanks in advance
Darknight