這里僅僅是對ftp工具類的簡單使用,很多東西還不是很了解。當然學以致用,先用到這里吧。
- public class FtpTest {
- /**
- * 向ftp寫文件(數據)
- */
- @Test
- public void uploadFile() {
- // 要寫入的文件內容
- String fileContent = "hello world,你好世界";
- // ftp登錄用戶名
- String userName = "admin";
- // ftp登錄密碼
- String userPassword = "xxxx";
- // ftp地址
- String server = "127.0.0.1";//直接ip地址
- // 創建的文件
- String fileName = "ftp.txt";
- // 指定寫入的目錄
- String path = "wd";
- FTPClient ftpClient = new FTPClient();
- try {
- InputStream is = null;
- // 1.輸入流
- is = new ByteArrayInputStream(fileContent.getBytes());
- // 2.連接服務器
- ftpClient.connect(server);
- // 3.登錄ftp
- ftpClient.login(userName, userPassword);
- // 4.指定寫入的目錄
- ftpClient.changeWorkingDirectory(path);
- // 5.寫操作
- ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
- ftpClient.storeFile(new String(fileName.getBytes("utf-8"),
- "iso-8859-1"), is);
- is.close();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (ftpClient.isConnected()) {
- try {
- ftpClient.disconnect();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * ftp下載數據
- */
- @Test
- public void downFile() {
- // ftp登錄用戶名
- String userName = "admin";
- // ftp登錄密碼
- String userPassword = "xxxx";
- // ftp地址:直接IP地址
- String server = "xxxx";
- // 創建的文件
- String fileName = "ftp.txt";
- // 指定寫入的目錄
- String path = "wd";
- // 指定本地寫入文件
- String localPath="D:\\";
- FTPClient ftp = new FTPClient();
- try {
- int reply;
- //1.連接服務器
- ftp.connect(server);
- //2.登錄服務器 如果采用默認端口,可以使用ftp.connect(url)的方式直接連接FTP服務器
- ftp.login(userName, userPassword);
- //3.判斷登陸是否成功
- reply = ftp.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftp.disconnect();
- }
- //4.指定要下載的目錄
- ftp.changeWorkingDirectory(path);// 轉移到FTP服務器目錄
- //5.遍歷下載的目錄
- FTPFile[] fs = ftp.listFiles();
- for (FTPFile ff : fs) {
- //解決中文亂碼問題,兩次解碼
- byte[] bytes=ff.getName().getBytes("iso-8859-1");
- String fn=new String(bytes,"utf8");
- if (fn.equals(fileName)) {
- //6.寫操作,將其寫入到本地文件中
- File localFile = new File(localPath + ff.getName());
- OutputStream is = new FileOutputStream(localFile);
- ftp.retrieveFile(ff.getName(), is);
- is.close();
- }
- }
- ftp.logout();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (ftp.isConnected()) {
- try {
- ftp.disconnect();
- } catch (IOException ioe) {
- }
- }
- }
- }
- }
很多知識點是相互聯系的,希望以后的例子中能夠結合更多的知識點進行實例編寫,這樣也有助於知識的鞏固。
下面是我自己項目中用到的代碼
/** * 下載pdf文件 */ public String downLoadPdf(String url,String contNo,String localPdfName){ String newUrl=""; String pathUrl="172.18.100.165"; //FTP服務器hostname int port=21;//FTP服務器端口 String username="shwasextt20\\ftp"; //FTP登錄賬號 String password="qwerty1!"; //FTP登錄密碼 String remotePath="/Imagedownload";//FTP服務器上的相對路徑 String fileName;//要下載的文件名 // String localPath="I:\\2015\\workspace\\workspace_newng\\wj\\WebContent\\wwwroot\\ng\\downLoad";//下載后保存到本地的路徑 String localPath="C:\\project\\b2c\\cms.ear\\cms.war\\wwwroot\\ng\\downLoad";//下載后保存到本地的路徑 // String localPath="C:\\B2C\\cms.ear\\cms.war\\wwwroot\\ng\\downLoad";//下載后保存到本地的路徑 String localName = ""; Date date = new Date(); SimpleDateFormat dr = new SimpleDateFormat("yyyyMMddHHmmss"); //重命名(保單號+時間) localName = localPdfName; if(!"".equals(url) && url !=null){ // String backUrl = "ftp://172.18.100.165/Imagedownload/0180050037-個險合同-電子合同-201537071208(66e735db-8ddf-4e0e-b70c-339544ff630b).PDF"; fileName = url.split("/")[4]; pathUrl = url.split("/")[2]; //生產環境ftp判斷 if("172.16.252.100".equals(pathUrl)){ username = "shwasextp20\\ftp"; }else if("172.16.252.110".equals(pathUrl)){ username = "shwasextp21\\ftp"; }else{ username = "shwasextt20\\ftp"; } FTPClient ftp = new FTPClient(); try { ftp.connect(pathUrl,port); ftp.login(username,password); System.out.println(ftp.isConnected()); ftp.enterLocalPassiveMode(); ftp.setControlEncoding("GBK"); ftp.setFileType(ftp.BINARY_FILE_TYPE); ftp.changeWorkingDirectory(remotePath); OutputStream outputStream = null; FTPFile[] fs = ftp.listFiles(); for (int i = 0; i < fs.length; i++) { FTPFile ff = fs[i]; if (ff.getName().equals(fileName)) { InputStream in = ftp.retrieveFileStream(new String(ff.getName().getBytes("GBK"), "ISO-8859-1")); int len = 0; long size = 0; byte[] bt = new byte[1024]; outputStream=new BufferedOutputStream(new FileOutputStream(localPath+"\\"+localName+".pdf")); while ((len = in.read(bt)) > 0) { outputStream.write(bt, 0, len); // outputStream.flush(); size = size + len; // System.out.println(fileName + "已xiazai :" + size); } newUrl = "cms/wwwroot/ng/downLoad/"+localName+".pdf"; outputStream.flush(); } } outputStream.close(); ftp.logout(); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return newUrl; }