/** * 一、從磁盤讀取相對應的文件 * 二、從配置文件讀取connect信息 */ //1 File file =new File(tempPath+"\\"+fileLoad.getFilename()); FileInputStream in=new FileInputStream(file); //2 String ip=Config.getProperty("IP"); int port=Integer.parseInt(Config.getProperty("PORT")); String username=Config.getProperty("USERNAME"); String password=Config.getProperty("PASSWORD"); String pa=Config.getProperty("PATH","utf-8"); //防止中文路徑亂碼的狀況 ,properties默認爲ISO-8859-1,若是存在用外部編輯器保存爲GBK格式的中文,須要轉換成GBK,不然路徑亂碼上傳失敗 String path=new String(pa.getBytes("ISO-8859-1"),"gbk"); //上傳路徑爲配置文件配置的文件路徑,與數據庫的發送文件加路徑組合而成, System.out.println("上傳的路徑爲:"+path+uploadPath); //調用connet方法連接FTP服務器 connect(ip, port, username,password,path+uploadPath,fileLoad.getFilename(),in); /** * @param connect 使用apache FTPcliint上傳文件至FTP服務器 * @param path 上傳到ftp服務器哪一個路徑下 // FTP://192.168.0.8/產品盤/RC盤/萬向財務/2015/8/erp_20150804/ * @param addr 地址 //FTP://192.168.0.8/ * @param port 端口號 //21 * @param username 用戶名 //PFMRC * @param password 密碼 //7nwW@C * @param filename 上傳的文件名稱 * @param input 輸入流 * @return boolean 上傳成功或者失敗的結果 * @throws Exception */ public boolean connect(String address,int port,String username,String password, String path,String filename, InputStream input) throws Exception { //初始化FTP服務器連接 boolean result=false; FTPClient ftp= new FTPClient(); try { int reply; //定義變量,用來測試FTP服務器連接是否連接成功 ftp.connect(address,port); //建立FTP連接 ftp.login(username,password);//登陸 /* * 若是reply爲空,關閉連接,返回false * */ reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return false; } //更換FTP的工做路徑,並解決中文路徑沒法識別的問題,設置編碼爲ISO-8859-1,和java默認編碼保持一致 boolean flag=ftp.changeWorkingDirectory(new String(path.getBytes(),"ISO-8859-1")); ftp.setFileType(FTP.BINARY_FILE_TYPE); //設置上傳文件的類型,防止亂碼 result=ftp.storeFile(filename, input); //上傳文件 System.out.println("上傳的結果:"+result); input.close(); //關閉輸入流 ftp.logout(); //退出登陸 } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; }