前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
前幾天 有個需求就是上傳文件的時候,本地存一份,其他服務器也保存一份,于是就研究了一下,功能只實現了上傳文件不能上傳文件夾,
這里主要用到了 ftp服務器軟件 網上有很多我用的是 serv-u,操作比較簡單很容易像我這樣的小菜使用
服務器下載地址
點擊打開鏈接? ? ? ? ? ?絕對安全無病毒
實現類代碼
[java]?view plain?copy
- <pre?code_snippet_id="244843"?snippet_file_name="blog_20140319_4_6086968"?name="code"?class="java">package?com.core.haction;??
- ??
- import?java.io.File;??
- import?java.io.FileInputStream;??
- import?java.io.IOException;??
- import?java.io.PrintWriter;??
- import?org.apache.commons.net.ftp.FTPClient;??
- import?org.apache.commons.net.ftp.FTPReply;??
- import?com.core.action.BaseAction;??
- ??
- public?class?ShangChuanFtp?extends?BaseAction<Model>?{??
- ??
- ????public?ShangChuanFtp()?{??
- ????????super(Model.class);??
- ????????//?TODO?Auto-generated?constructor?stub??
- ????}??
- ??
- ????/**?
- ?????*??
- ?????*/??
- ????private?static?final?long?serialVersionUID?=?1L;??
- ????private?FTPClient?ftp;??
- ??
- ????/**?
- ?????*??
- ?????*?@param?path?
- ?????*????????????上傳到ftp服務器哪個路徑下?
- ?????*?@param?addr?
- ?????*????????????地址?
- ?????*?@param?port?
- ?????*????????????端口號?
- ?????*?@param?username?
- ?????*????????????用戶名?
- ?????*?@param?password?
- ?????*????????????密碼?
- ?????*?@return?
- ?????*?@throws?Exception?
- ?????*/??
- ????private?boolean?connect(String?path,?String?addr,?int?port,??
- ????????????String?username,?String?password)?throws?Exception?{??
- ????????boolean?result?=?false;??
- ????????ftp?=?new?FTPClient();??
- ????????int?reply;??
- ????????ftp.connect(addr,?port);//連接ftp服務器??
- ????????ftp.login(username,?password);//登錄ftp??
- ????????ftp.setFileType(FTPClient.BINARY_FILE_TYPE);??
- ????????reply?=?ftp.getReplyCode();??
- ????????if?(!FTPReply.isPositiveCompletion(reply))?{??
- ????????????ftp.disconnect();??
- ????????????return?result;??
- ????????}??
- ????????ftp.changeWorkingDirectory(path);??
- ????????result?=?true;??
- ????????return?result;??
- ????}??
- ??
- ????/**?
- ?????*??
- ?????*?@param?file?
- ?????*????????????上傳的文件或文件夾?
- ?????*?????????????
- ?????*????????????代碼我是從網上找的,在使用過程中出現了,中文文件名稱不能上傳,后來自己改變了一下編碼?
- ?????*?@throws?Exception?
- ?????*/??
- ????private?void?upload(File?file)?throws?Exception?{??
- ????????if?(file.isDirectory())?{??
- ????????????//?System.out.println(file.isDirectory()+"\n"+file.getName());??
- ????????????ftp.makeDirectory(new?String(file.getName().getBytes("utf-8"),"8859_1"));//給文件名轉換編碼??
- ????????????//System.out.println(file.getName());??
- ????????????ftp.changeWorkingDirectory(new?String(file.getName().getBytes("utf-8"),"8859_1"));??
- ????????????String[]?files?=?file.list();??
- ????????????for?(int?i?=?0;?i?<?files.length;?i++)?{??
- ????????????????File?file1?=?new?File(file.getPath()?+?"\\"?+?files[i]);??
- ????????????????if?(file1.isDirectory())?{??
- ????????????????????upload(file1);??
- ????????????????????ftp.changeToParentDirectory();??
- ????????????????}?else?{??
- ????????????????????File?file2?=?new?File(file.getPath()?+?"\\"?+?files[i]);??
- ????????????????????FileInputStream?input?=?new?FileInputStream(file2);??
- ????????????????????ftp.storeFile(new?String(file2.getName().getBytes("utf-8"),"8859_1"),?input);??
- ????????????????????input.close();??
- ????????????????}??
- ????????????}??
- ????????}?else?{??
- ????????????File?file2?=?new?File(file.getPath());??
- ????????????System.out.println(file2.getName());??
- ????????????FileInputStream?input?=?new?FileInputStream(file2);??
- ????????????ftp.storeFile(new?String(file2.getName().getBytes("utf-8"),"8859_1"),?input);??
- ????????????input.close();??
- ????????}??
- ????}??
- ??
- ????//?public?static?void?main(String[]?args)?throws?Exception{//本地測試方法??
- ????//?ShangChuanFtp?t?=?new?ShangChuanFtp();??
- ????//?t.connect("",?"遠程服務器的Ip地址",?21,?"ftp的登錄名",?"ftp的登錄密碼");??
- ????//?File?file?=?new?File("d:\\webapps");//要上傳的文件地址??
- ????//?t.upload(file);??
- ????//?System.out.println("上傳完成");??
- ????//?}??
- ????/**?
- ?????*??
- ?????*?@param?file?
- ?????*??????????以下代碼,住web頁面用的,strut2的語法大家應該都會知道???
- ?????*?@throws?IOException?
- ?????*/??
- ????private?File?pphoto;??
- ????private?String?pphotoFileName;??
- ????private?String?pphotoFileContentType;??
- ????private?static?final?String?filePath?=?"/while/photo";//上傳文件到本地服務器的路徑??
- ????private?String?textfield;??
- ??
- ????public?String?scftp()?throws?IOException?{??
- ????????PrintWriter?out?=?getResponse().getWriter();??
- ??????????
- ????????try?{??
- ??????????????
- ????????????String?fileUrl?=?null;??
- ????????????if?(pphoto?!=?null)?{??
- ????????????????//自己封裝的上傳本地服務器的方法fileUrl是方法的返回值我這里是返回的文件名稱??
- ????????????????fileUrl?=?this.saveFile(pphoto,?pphotoFileName,?filePath,false);??
- ????????????????System.out.println("3:"?+?this.getSession().getAttribute("dir"));??
- ????????????????//t.connect("",?"遠程服務器的Ip地址",?21,?"ftp的登錄名",?"ftp的登錄密碼");21是端口號??
- ????????????????connect("",?"000.000.000.000",?21,?"",?"");??
- ????????????????//this.getSession().getAttribute("dir")這個是獲取的上傳到本地服務器的路徑,用了個懶辦法,在上傳方法我存到session里面,這邊獲取的??
- ????????????????//fileUrl是你上傳的那個文件名??
- ????????????????File?file?=?new?File(this.getSession().getAttribute("dir")+"/"+fileUrl);??
- ????????????????upload(file);??
- ????????????}?else?{??
- ????????????????out.print("0");??
- ????????????}??
- ??????????????
- ??
- ????????}?catch?(Exception?e)?{??
- ????????????//?TODO?Auto-generated?catch?block${ctx?}/json/scftp.action??
- ????????????System.out.println(e.getMessage());??
- ????????}??
- ??
- ????????out.print("1");??
- ????????return?null;??
- ????}??
- ??
- ????public?String?getTextfield()?{??
- ????????return?textfield;??
- ????}??
- ??
- ????public?void?setTextfield(String?textfield)?{??
- ????????this.textfield?=?textfield;??
- ????}??
- ??
- ????public?File?getPphoto()?{??
- ????????return?pphoto;??
- ????}??
- ??
- ????public?void?setPphoto(File?pphoto)?{??
- ????????this.pphoto?=?pphoto;??
- ????}??
- ??
- ????public?String?getPphotoFileName()?{??
- ????????return?pphotoFileName;??
- ????}??
- ??
- ????public?void?setPphotoFileName(String?pphotoFileName)?{??
- ????????this.pphotoFileName?=?pphotoFileName;??
- ????}??
- ??
- ????public?String?getPphotoFileContentType()?{??
- ????????return?pphotoFileContentType;??
- ????}??
- ??
- ????public?void?setPphotoFileContentType(String?pphotoFileContentType)?{??
- ????????this.pphotoFileContentType?=?pphotoFileContentType;??
- ????}??
- ??
- }
?