(如果出現android.os.NetworkOnMainThreadException的錯誤,可以參考 android.os.NetworkOnMainThreadException異常處理 點選打開連結)
在JAVA程式中,經常需要和FTP打交道,比如向FTP伺服器上傳檔案、下載下傳檔案,本文簡單介紹如何利用jakarta commons中的FTPClient(在commons-net包中)實作上傳下載下傳檔案。
- 所用到的jar包有:
- commons-net-1.4.1.jar
- jakarta-oro.jar
上傳測試用例:檔案上傳源代碼 /** * Description: 向FTP伺服器上傳檔案 * @Version1.0 * @param url FTP伺服器hostname * @param port FTP伺服器端口 * @param username FTP登入賬号 * @param password FTP登入密碼 * @param path FTP伺服器儲存目錄 * @param filename 上傳到FTP伺服器上的檔案名 * @param input 輸入流 * @return 成功傳回true,否則傳回false */ public static boolean uploadFile( String url,//FTP伺服器hostname int port,//FTP伺服器端口 String username, // FTP登入賬号 String password, //FTP登入密碼 String path, //FTP伺服器儲存目錄 String filename, //上傳到FTP伺服器上的檔案名 InputStream input // 輸入流 ) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(url, port);//連接配接FTP伺服器 //如果采用預設端口,可以使用ftp.connect(url)的方式直接連接配接FTP伺服器 ftp.login(username, password);//登入 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.changeWorkingDirectory(path); ftp.storeFile(filename, input); input.close(); ftp.logout(); success = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }
/** * 将本地檔案上傳到FTP伺服器上 * */ public void testUpLoadFromDisk(){ try { FileInputStream in=new FileInputStream(new File("D:/test.txt")); boolean flag = uploadFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", in); System.out.println(flag); } catch (FileNotFoundException e) { e.printStackTrace(); } }
檔案下載下傳:/** * 在FTP伺服器上生成一個檔案,并将一個字元串寫入到該檔案中 * */ public void testUpLoadFromString(){ try { String str = "這是要寫入的字元串!"; InputStream input = new ByteArrayInputStream(str.getBytes("utf-8")); boolean flag = uploadFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", input); System.out.println(flag); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
以下是檔案下載下傳的測試用例:檔案下載下傳源代碼 /** * Description: 從FTP伺服器下載下傳檔案 * @Version1.0 * @param url FTP伺服器hostname * @param port FTP伺服器端口 * @param username FTP登入賬号 * @param password FTP登入密碼 * @param remotePath FTP伺服器上的相對路徑 * @param fileName 要下載下傳的檔案名 * @param localPath 下載下傳後儲存到本地的路徑 * @return */ public static boolean downFile( String url, //FTP伺服器hostname int port,//FTP伺服器端口 String username, //FTP登入賬号 String password, //FTP登入密碼 String remotePath,//FTP伺服器上的相對路徑 String fileName,//要下載下傳的檔案名 String localPath//下載下傳後儲存到本地的路徑 ) { boolean success = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(url, port); //如果采用預設端口,可以使用ftp.connect(url)的方式直接連接配接FTP伺服器 ftp.login(username, password);//登入 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return success; } ftp.changeWorkingDirectory(remotePath);//轉移到FTP伺服器目錄 FTPFile[] fs = ftp.listFiles(); for(FTPFile ff:fs){ if(ff.getName().equals(fileName)){ File localFile = new File(localPath+"/"+ff.getName()); OutputStream is = new FileOutputStream(localFile); ftp.retrieveFile(ff.getName(), is); is.close(); } } ftp.logout(); success = true; } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return success; }
/** * 将FTP伺服器上檔案下載下傳到本地 * */ public void testDownFile(){ try { boolean flag = downFile("127.0.0.1", 21, "administrator", "zyuc2011", "test", "test.txt", "D:/"); System.out.println(flag); } catch (Exception e) { e.printStackTrace(); } }