JAVA中的FtpClient与FTPClient,并实现jsp页面下载ftp服务器上的文件
- 格式:docx
- 大小:81.40 KB
- 文档页数:15
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的包下面的类,但是jdkapi并没有对此作介绍,在中文支持上面也有一定的限制。
本段代码中的Ftp服务器的IP地址,用户名和密码均通过SystemConfig.properties文档获取Ftp_client.java[java]view plain copy1.package com.iodn.util;2.3.import java.io.ByteArrayOutputStream;4.import java.io.File;5.import java.io.FileInputStream;6.import java.io.FileOutputStream;7.import java.io.IOException;8.import java.util.ResourceBundle;9.import .TelnetInputStream;10.import .TelnetOutputStream;11.import .ftp.FtpClient;12.13.public class Ftp_client {14.15.//FTP客户端16.private FtpClient ftpClient;17.private ResourceBundle res=null;18./**19. * 连接FTP服务器20. * @param path 指定远程服务器上的路径21. */22.public Ftp_client(String path){23.24. res = ResourceBundle.getBundle("com.iodn.util.SystemConfig");//获取配置文件propeties文档中的数据25.try{26. ftpClient=new FtpClient(res.getString("Properties.ftpHostIp"));//如果不想使用配置文件即可将数据写死(如:192.168.1.10)27. ftpClient.login(res.getString("Properties.ftpUser"), res.getString("Properties.ftpPassword"));//Ftp服务器用户名和密码28. ftpClient.binary();29. System.out.println("Login Success!");30.if(path.length()!=0){31.//把远程系统上的目录切换到参数path所指定的目录(可不用设置,上传下载删除时加Ftp中的全路径即可)32. ftpClient.cd(path);33. }34. }catch(Exception e){35. e.printStackTrace();36. }37. }38.39./**40. * 上传文件41. * @param remoteFile42. * @param localFile43. */44.public boolean upload(String remoteFile, String localFile){45.boolean bool=false;46. TelnetOutputStream os=null;47. FileInputStream is=null;48.try{49. os=ftpClient.put(remoteFile);50. is=new FileInputStream(new File(localFile));51.byte[] b=new byte[1024];52.int c;53.while((c=is.read(b))!=-1){54. os.write(b, 0, c);55. }56. bool=true;57. }catch(Exception e){58. e.printStackTrace();59. System.out.println("上传文件失败!请检查系统FTP设置,并确认FTP服务启动");60. }finally{61.if(is!=null){62.try {63. is.close();64. } catch (IOException e) {65. e.printStackTrace();66. }67. }68.if(os!=null){69.try {70. os.close();71. } catch (IOException e) {72. e.printStackTrace();74. }75. closeConnect();76. }77.return bool;78. }79./**80. * 下载文件81. * @param remoteFile 远程文件路径(服务器端)82. * @param localFile 本地文件路径(客户端)83. */84.85.public void download(String remoteFile, String localFile) {86. TelnetInputStream is=null;87. FileOutputStream os=null;88.try{89.//获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
90. is=ftpClient.get(remoteFile);91. File file=new File(localFile);92. os=new FileOutputStream(file);93.byte[] b=new byte[1024];94.int c;95.while((c=is.read(b))!=-1){96. os.write(b,0,c);97. }98. }catch(Exception e){99. e.printStackTrace();100. System.out.println("下载文件失败!请检查系统FTP设置,并确认FTP服务启动");101. }finally{102.if(is!=null){103.try {104. is.close();105. } catch (IOException e) {106. e.printStackTrace();107. }108. }109.if(os!=null){110.try {111. os.close();112. } catch (IOException e) {113. e.printStackTrace();114. }116. closeConnect();117.118. }119. }120.121.// 删除文件至FTP通用方法122.123.public void deleteFileFtp(String fileName){124.try {125. ftpClient.sendServer("dele " + fileName + "\r\n");126. } catch (Exception e) {127. System.out.println("删除文件失败!请检查系统FTP设置,并确认FTP服务启动");128. }finally{129. closeConnect();//关闭FTP连接130. }131.132. }133.134./**135. * Ftp下载返回byte[]字节数组供前端下载使用136. * @param SourceFileName137. * @return138. * @throws Exception139. */140.public byte[] downFileByte(String SourceFileName) throws Exception {141.//ftpClient.binary(); //一定要使用二进制模式142. TelnetInputStream ftpIn = ftpClient.get(SourceFileName); 143. ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); 144.byte[] buf = new byte[204800];145.int bufsize = 0;146.while ((bufsize = ftpIn.read(buf, 0, buf.length)) != -1) { 147. byteOut.write(buf, 0, bufsize);148. }149.byte[] return_arraybyte = byteOut.toByteArray();150. byteOut.close();151. ftpIn.close();152.return return_arraybyte;153. }154./**155. * 关闭FTP连接156. */157.public void closeConnect(){158.try{159. ftpClient.closeServer();160. }catch(Exception e){161. e.printStackTrace();162. }163. }164.165.public static void main(String[] args) throws Exception {166. Ftp_client test=new Ftp_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,支持中文上传下载。