




已阅读5页,还剩10页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
JAVA中的FtpClient与FTPClient,并实现jsp页面下载ftp服务器上的文件这段时间一直在研究Java如何访问Ftp,搞了一段时间了,也有一定的了解。故此记录一下。ftp和FTP我个人觉得FTP更符合我们程序员的口味,不管是方法命名还是API的详细与否,或者是开发平台的问题,FTP毕竟是Apache的东西,做的就是不错。其实web开发中一般都会涉及到编码问题,所以web上传下载一定会有中文乱码的问题存在,而FTP对中文的支持比ftp要好多了。使用ftpClient不需要导入其它jar包,只要你使用java语言开发就行了,而使用FTPClient需要使用commons-net-1.4.1.jar和jakarta-oro-2.0.8.jar,当然jar版本随便你自己。话不多说,上代码!FTP服务器的文件目录结构图:一、FtpClientFtpClient是属于JDK的包下面的类,但是jdk api并没有对此作介绍,在中文支持上面也有一定的限制。本段代码中的Ftp服务器的IP地址,用户名和密码均通过SystemCperties文档获取Ftp_client.javajavaview plaincopy1. packagecom.iodn.util;2. 3. importjava.io.ByteArrayOutputStream;4. importjava.io.File;5. importjava.io.FileInputStream;6. importjava.io.FileOutputStream;7. importjava.io.IOException;8. importjava.util.ResourceBundle;9. .TelnetInputStream;10. .TelnetOutputStream;11. .ftp.FtpClient;12. 13. publicclassFtp_client14. 15. /FTP客户端16. privateFtpClientftpClient;17. privateResourceBundleres=null;18. /*19. *连接FTP服务器20. *parampath指定远程服务器上的路径21. */22. publicFtp_client(Stringpath)23. 24. res=ResourceBundle.getBundle(com.iodn.util.SystemConfig);/获取配置文件propeties文档中的数据25. try26. ftpClient=newFtpClient(res.getString(Properties.ftpHostIp);/如果不想使用配置文件即可将数据写死(如:0)27. ftpClient.login(res.getString(Properties.ftpUser),res.getString(Properties.ftpPassword);/Ftp服务器用户名和密码28. ftpClient.binary();29. System.out.println(LoginSuccess!);30. if(path.length()!=0)31. /把远程系统上的目录切换到参数path所指定的目录(可不用设置,上传下载删除时加Ftp中的全路径即可)32. ftpClient.cd(path);33. 34. catch(Exceptione)35. e.printStackTrace();36. 37. 38. 39. /*40. *上传文件41. *paramremoteFile42. *paramlocalFile43. */44. publicbooleanupload(StringremoteFile,StringlocalFile)45. booleanbool=false;46. TelnetOutputStreamos=null;47. FileInputStreamis=null;48. try49. os=ftpClient.put(remoteFile);50. is=newFileInputStream(newFile(localFile);51. byteb=newbyte1024;52. intc;53. while(c=is.read(b)!=-1)54. os.write(b,0,c);55. 56. bool=true;57. catch(Exceptione)58. e.printStackTrace();59. System.out.println(上传文件失败!请检查系统FTP设置,并确认FTP服务启动);60. finally61. if(is!=null)62. try63. is.close();64. catch(IOExceptione)65. e.printStackTrace();66. 67. 68. if(os!=null)69. try70. os.close();71. catch(IOExceptione)72. e.printStackTrace();73. 74. 75. closeConnect();76. 77. returnbool;78. 79. /*80. *下载文件81. *paramremoteFile远程文件路径(服务器端)82. *paramlocalFile本地文件路径(客户端)83. */84. 85. publicvoiddownload(StringremoteFile,StringlocalFile)86. TelnetInputStreamis=null;87. FileOutputStreamos=null;88. try89. /获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。90. is=ftpClient.get(remoteFile);91. Filefile=newFile(localFile);92. os=newFileOutputStream(file);93. byteb=newbyte1024;94. intc;95. while(c=is.read(b)!=-1)96. os.write(b,0,c);97. 98. catch(Exceptione)99. e.printStackTrace();100. System.out.println(下载文件失败!请检查系统FTP设置,并确认FTP服务启动);101. finally102. if(is!=null)103. try104. is.close();105. catch(IOExceptione)106. e.printStackTrace();107. 108. 109. if(os!=null)110. try111. os.close();112. catch(IOExceptione)113. e.printStackTrace();114. 115. 116. closeConnect();117. 118. 119. 120. 121. /删除文件至FTP通用方法122. 123. publicvoiddeleteFileFtp(StringfileName)124. try125. ftpClient.sendServer(dele+fileName+rn);126. catch(Exceptione)127. System.out.println(删除文件失败!请检查系统FTP设置,并确认FTP服务启动);128. finally129. closeConnect();/关闭FTP连接130. 131. 132. 133. 134. /*135. *Ftp下载返回byte字节数组供前端下载使用136. *paramSourceFileName137. *return138. *throwsException139. */140. publicbytedownFileByte(StringSourceFileName)throwsException141. /ftpClient.binary();/一定要使用二进制模式142. TelnetInputStreamftpIn=ftpClient.get(SourceFileName);143. ByteArrayOutputStreambyteOut=newByteArrayOutputStream();144. bytebuf=newbyte204800;145. intbufsize=0;146. while(bufsize=ftpIn.read(buf,0,buf.length)!=-1)147. byteOut.write(buf,0,bufsize);148. 149. bytereturn_arraybyte=byteOut.toByteArray();150. byteOut.close();151. ftpIn.close();152. returnreturn_arraybyte;153. 154. /*155. *关闭FTP连接156. */157. publicvoidcloseConnect()158. try159. ftpClient.closeServer();160. catch(Exceptione)161. e.printStackTrace();162. 163. 164. 165. publicstaticvoidmain(Stringargs)throwsException166. Ftp_clienttest=newFtp_client(/zhou);167. /test.deleteFileFtp(20140412.zip);168. /test.upload(20141201.xls,E:201412.xls);/上传文件169. /test.download(321.txt,E:111.txt);/下载170. test.downFileByte(321.txt);171. 172. 173. 174. 175. 176. 二、FTPClientFTPClient是Apache包下面的类,主要依靠commons-net-1.4.1.jar,支持中文上传下载。本段代码中的Ftp服务器的IP地址,用户名和密码均通过SystemCperties文档获取FTPClientHelper.javajavaview plaincopy1. packagecom.iodn.util;2. 3. importjava.io.ByteArrayOutputStream;4. importjava.io.File;5. importjava.io.FileInputStream;6. importjava.io.FileOutputStream;7. importjava.io.InputStream;8. importjava.io.OutputStream;9. importjava.util.Date;10. importjava.util.ResourceBundle;11. 12. .ftp.FTPClient;13. .ftp.FTPClientConfig;14. .ftp.FTPFile;15. .ftp.FTPReply;16. 17. publicclassFTPClientHelper18. 19. /FTP客户端20. privateFTPClientftpClient;21. privateResourceBundleres=null;22. /*23. *连接FTP服务器24. *paramremotePath/设定当前需要操作的目录25. */26. publicFTPClientHelper(StringremotePath)27. 28. res=ResourceBundle.getBundle(com.iodn.util.SystemConfig);29. try30. ftpClient=newFTPClient();31. ftpClient.connect(res.getString(Properties.ftpHostIp),21);/FTP服务器IP地址32. ftpClient.login(res.getString(Properties.ftpUser),res.getString(Properties.ftpPassword);/FTP服务器用户名和密码33. ftpClient.setDataTimeout(120000);34. 35. /下面三行代码必须要,而且不能改变编码格式,否则不能正确下载中文文件36. ftpClient.setControlEncoding(GBK);37. FTPClientConfigconf=newFTPClientConfig(FTPClientConfig.SYST_UNIX);38. conf.setServerLanguageCode(zh);39. System.out.println(LoginSuccess!);40. intreply=ftpClient.getReplyCode();41. if(!FTPReply.isPositiveCompletion(reply)42. ftpClient.disconnect();43. ftpClient=null;44. 45. /转移到设置的目录下46. if(remotePath!=null&!remotePath.equals()47. ftpClient.changeWorkingDirectory(remotePath);48. System.out.println(filesuccess);49. 50. 51. catch(Exceptione)52. e.printStackTrace();53. 54. 55. 56. /*57. *上传文件58. *paramremoteFile59. *paramlocalFile60. */61. publicbooleanupload(StringlocalFile)62. booleanbool=false;63. if(ftpClient!=null)64. try65. Filefile=newFile(localFile);66. StringremoteFile=file.getName();67. System.out.println(remoteFile);68. InputStreamis=newFileInputStream(file);69. ftpClient.storeFile(remoteFile,is);70. is.close();71. ftpClient.logout();72. bool=true;73. catch(Exceptione)74. e.printStackTrace();75. System.out.println(上传文件失败!请检查系统FTP设置,并确认FTP服务启动);76. finally77. closeConnect();78. 79. 80. 81. returnbool;82. 83. /*84. *下载文件85. *paramremoteFile远程文件路径(服务器端)86. *paramfileName需要下载的文件名87. *paramlocalFile本地文件路径(客户端)88. */89. 90. publicbooleandownload(StringfileName,StringlocalPath)91. booleanbool=false;92. if(ftpClient!=null)93. try94. /ftpClient.changeWorkingDirectory(remoteFile);/转移到FTP服务器目录95. FTPFilefiles=ftpClient.listFiles();96. for(FTPFilefile:files)97. if(file.getName().equals(fileName)98. System.out.println(-start-+System.currentTimeMillis();99. Stringpath=localPath+file.getName();100. System.out.println(path);101. FilelocalFile=newFile(path);102. OutputStreamops=newFileOutputStream(localFile);103. ftpClient.retrieveFile(file.getName(),ops);104. ops.close();105. System.out.println(-ender-+System.currentTimeMillis();106. bool=true;107. break;108. 109. 110. catch(Exceptione)111. e.printStackTrace();112. System.out.println(下载文件失败!请检查系统FTP设置,并确认FTP服务启动);113. finally114. closeConnect();115. 116. 117. returnbool;118. 119. 120. /删除文件至FTP通用方法121. 122. publicbooleandeleteFileFtp(StringfileName)123. booleanbool=false;124. if(ftpClient!=null)125. try126. ftpClient.deleteFile(fileName);127. bool=true;128. catch(Exceptione)129. System.out.println(删除文件失败!请确定文件是否存在);130. finally131. closeConnect();/关闭FTP连接132. 133. 134. returnbool;135. 136. 137. /*138. *下载文件139. *返回byte140. *paramfileName需要下载的文件名141. *return142. *throwsException143. */144. publicbytedownFileByte(StringfileName)145. bytereturn_arraybyte=null;146. if(ftpClient!=null)147. try148. FTPFilefiles=ftpClient.listFiles();149. for(FTPFilefile:files)150. if(file.getName().equals(fileName)151. InputStreamins=ftpClient.retrieveFileStream(file.getName();152. ByteArrayOutputStreambyteOut=newByteArrayOutputStream();153. bytebuf=newbyte204800;154. intbufsize=0;155. while(bufsize=ins.read(buf,0,buf.length)!=-1)156. byteOut.write(buf,0,bufsize);157. 158. return_arraybyte=byteOut.toByteArray();159. byteOut.close();160. ins.close();161. break;162. 163. 164. catch(Exceptione)165. e.printStackTrace();166. finally167. closeConnect();168. 169. 170. returnreturn_arraybyte;171. 172. /*173. *关闭FTP连接174. */175. publicvoidcloseConnect()176. try177. ftpClient.disconnect();178. catch(Exceptione)179. e.printStackTrace();180. 181. 182. 183. publicstaticvoidmain(Stringargs)184. FTPClientHelpertest=newFTPClientHelper(/zhou);185. booleanbool=false;186. /bool=test.deleteFileFtp(光路由.doc);187. /bool=test.upload(你好.xls,E:你好.xls);/上传文件188. /bool=test.download(你好.xls,D:);/下载189. /bool=test.downloadToInputStream(/zhou/123.txt);190. System.out.println(+bool);191. 192. 193. 194. 195. 三、对web的支持,如何通过jsp页面直接下载对web的支持,必须要用到serlvet,调用java方法去获取FTP服务器上的文件流。1、HelpServletjavaview plaincopy1. packagecom.iodn.servlets;2. 3. importjava.io.BufferedOutputStream;4. importjava.io.IOException;5. importjava.io.OutputStream;6. 7. importjavax.servlet.ServletException;8. importjavax.servlet.http.HttpServlet;9. importjavax.servlet.http.HttpServletRequest;10. importjavax.servlet.http.HttpServletResponse;11. 12. importcom.iodn.util.FTPClientHelper;13. 14. /*15. *ServletimplementationclassHelpServlet16. */17. publicclassHelpServletextendsHttpServlet18. privatestaticfinallongserialVersionUID=1L;19. 20. /*21. *seeHttpServlet#HttpServlet()22. */23. publicHelpServlet()24. super();25. /TODOAuto-generatedconstructorstub26. 27. 28. /*29. *seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)30. */31. protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException32. request.setCharacterEncoding(UTF-8);33. this.downFtpFile(/HelpDocument,NJPTPXHelper.chm,request,response);34. 35. 36. /*37. *seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)38. */39. protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException40. /TODOAuto-generatedmethodstub41. 42. 43. 44. /*45. *根据路径下载Ftp中的文件46. *paramremoteFile
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 销售渠道拓展方案模板
- 2025-2030动力锂电池隔膜涂覆技术迭代与安全性能提升分析报告
- 2025-2030动力锂电池隔膜技术路线更迭对设备厂商的影响评估报告
- 2025-2030动力锂电池回收网络建设进度与循环经济模式分析报告
- 企业培训需求分析与计划制定
- 2025-2030动力电池硅基负极膨胀问题解决方案产业化成熟度评估报告
- 2025-2030动力电池梯次利用技术经济性分析与残值评估模型报告
- 2025-2030动力电池梯次利用市场政策支持与经济效益评估
- 2025-2030动力电池快充技术突破与市场应用前景研究报告
- 2025-2030动力电池回收利用技术路线比较及经济性测算专项研究
- 老旧小区健身设施增设规划方案
- T∕CEPPEA5004.5-2020核电厂常规岛施工图设计文件内容深度规定第5部分仪表与控制
- 酸碱防护知识培训课件
- 值勤岗亭安装方案范本
- 2025年吉林省中考数学真题卷含答案解析
- GB/T 45953-2025供应链安全管理体系规范
- 第十三章 三角形 单元试卷(含答案) 2025-2026学年人教版数学八年级上册
- 《数据库原理》课件第2章建立数据模型
- 产程干预的医学指征课件
- 2024年辽宁轨道交通职业学院单招《英语》真题含完整答案详解【易错题】
- 2025年picc置管与维护临床护理实践指南
评论
0/150
提交评论