JAVA实现FTP断点上传

主要使用apache中的net包来实现。网址http://commons.apache.org/net/。具体包的下载和API文档请看官网。

断点上传就是在上传的过程中设置传输的起始位置。并设置二进制传输。

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;http://www.kmnk03.com/hxpfk/dzpz/309.html
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.io.PrintWriter;
  8. import org.apache.commons.net.PrintCommandListener;
  9. import org.apache.commons.net.ftp.FTP;
  10. import org.apache.commons.net.ftp.FTPClient;
  11. import org.apache.commons.net.ftp.FTPFile;
  12. import org.apache.commons.net.ftp.FTPReply;
  13. public class ContinueFTP {
  14. private FTPClient ftpClient = new FTPClient();
  15. public ContinueFTP(){http://www.kmnk03.com/hxpfk/npx/310.html
  16. //设置将过程中使用到的命令输出到控制台
  17. this.ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
  18. }
  19. /**
  20. java编程中用于连接到FTP服务器
  21. * @param hostname 主机名
  22. * @param port 端口
  23. * @param username 用户名
  24. * @param password 密码
  25. * @return 是否连接成功
  26. * @throws IOException
  27. */
  28. public boolean connect(String hostname,int port,String username,String password) throws IOException{
  29. ftpClient.connect(hostname, port);
  30. if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
  31. if(ftpClient.login(username, password)){
  32. return true;
  33. }http://www.kmnk03.com/hxpfk/npx/311.html
  34. }
  35. disconnect();
  36. return false;
  37. }
  38. /**
  39. * 从FTP服务器上下载文件
  40. * @param remote 远程文件路径
  41. * @param local 本地文件路径
  42. * @return 是否成功
  43. * @throws IOException
  44. */http://www.kmnk03.com/hxpfk/py/312.html
  45. public boolean download(String remote,String local) throws IOException{
  46. ftpClient.enterLocalPassiveMode();
  47. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  48. boolean result;
  49. File f = new File(local);
  50. FTPFile[] files = ftpClient.listFiles(remote);
  51. if(files.length != 1){
  52. System.out.println("远程文件不唯一");
  53. return false;
  54. }
  55. long lRemoteSize = files[0].getSize();
  56. if(f.exists()){
  57. OutputStream out = new FileOutputStream(f,true);
  58. System.out.println("本地文件大小为:"+f.length());
  59. if(f.length() >= lRemoteSize){
  60. System.out.println("本地文件大小大于远程文件大小,下载中止");
  61. return false;
  62. }http://www.kmnk03.com/hxpfk/py/313.html
  63. ftpClient.setRestartOffset(f.length());
  64. result = ftpClient.retrieveFile(remote, out);
  65. out.close();
  66. }else {
  67. OutputStream out = new FileOutputStream(f);
  68. result = ftpClient.retrieveFile(remote, out);
  69. out.close();
  70. }
  71. return result;
  72. }
  73. http://www.kmnk03.com/hxpfk/xmz/314.html
  74. /**
  75. * 上传文件到FTP服务器,支持断点续传
  76. * @param local 本地文件名称,绝对路径
  77. * @param remote 远程文件路径,使用/home/directory1/subdirectory/file.ext 按照Linux上的路径指定方式,支持多级目录嵌套,支持递归创建不存在的目录结构
  78. * @return 上传结果
  79. * @throws IOException
  80. */
  81. public UploadStatus upload(String local,String remote) throws IOException{
  82. //设置PassiveMode传输
  83. ftpClient.enterLocalPassiveMode();
  84. //设置以二进制流的方式传输
  85. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  86. UploadStatus result;
  87. //对远程目录的处理
  88. String remoteFileName = remote;
  89. if(remote.contains("/")){
  90. remoteFileName = remote.substring(remote.lastIndexOf("/")+1);
  91. String directory = remote.substring(0,remote.lastIndexOf("/")+1);
  92. if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(directory)){
  93. //如果远程目录不存在,则递归创建远程服务器目录
  94. int start=0;http://www.kmnk03.com/hxpfk/xmz/315.html
  95. int end = 0;
  96. if(directory.startsWith("/")){
  97. start = 1;
  98. }else{
  99. start = 0;
  100. }
  101. end = directory.indexOf("/",start);
  102. while(true){
  103. String subDirectory = remote.substring(start,end);
  104. if(!ftpClient.changeWorkingDirectory(subDirectory)){
  105. if(ftpClient.makeDirectory(subDirectory)){
  106. ftpClient.changeWorkingDirectory(subDirectory);
  107. }else {http://www.kmnk03.com/hxpfk/ylb/316.html
  108. System.out.println("创建目录失败");
  109. return UploadStatus.Create_Directory_Fail;
  110. }
  111. }
  112. start = end + 1;
  113. end = directory.indexOf("/",start);
  114. //检查所有目录是否创建完毕
  115. if(end <= start){
  116. break;
  117. }
  118. }
  119. }
  120. }
  121. //检查远程是否存在文件
  122. FTPFile[] files = ftpClient.listFiles(remoteFileName);
  123. if(files.length == 1){
  124. long remoteSize = files[0].getSize();
  125. File f = new File(local);
  126. long localSize = f.length();
  127. if(remoteSize==localSize){
  128. return UploadStatus.File_Exits;
  129. }else if(remoteSize > localSize){
  130. return UploadStatus.Remote_Bigger_Local;
  131. }http://www.kmnk03.com/hxpfk/ylb/317.html
  132. //尝试移动文件内读取指针,实现断点续传
  133. InputStream is = new FileInputStream(f);
  134. if(is.skip(remoteSize)==remoteSize){
  135. ftpClient.setRestartOffset(remoteSize);
  136. if(ftpClient.storeFile(remote, is)){
  137. return UploadStatus.Upload_From_Break_Success;
  138. }
  139. }
  140. //如果断点续传没有成功,则删除服务器上文件,重新上传
  141. if(!ftpClient.deleteFile(remoteFileName)){
  142. return UploadStatus.Delete_Remote_Faild;
  143. }
  144. is = new FileInputStream(f);
  145. if(ftpClient.storeFile(remote, is)){
  146. result = UploadStatus.Upload_New_File_Success;
  147. }else{
  148. result = UploadStatus.Upload_New_File_Failed;
  149. }
  150. is.close();
  151. }else {
  152. InputStream is = new FileInputStream(local);
  153. if(ftpClient.storeFile(remoteFileName, is)){
  154. result = UploadStatus.Upload_New_File_Success;
  155. }else{
  156. result = UploadStatus.Upload_New_File_Failed;
  157. }
  158. is.close();
  159. }
  160. return result;
  161. }
  162. /**http://www.kmnk03.com/hxpfk/ylb/319.html
  163. * 断开与远程服务器的连接
  164. * @throws IOException
  165. */
  166. public void disconnect() throws IOException{
  167. if(ftpClient.isConnected()){
  168. ftpClient.disconnect();
  169. }
  170. }
  171. public static void main(String[] args) {
  172. ContinueFTP myFtp = new ContinueFTP();
  173. try {
  174. myFtp.connect("192.168.21.171", 21, "test", "test");
  175. System.out.println(myFtp.upload("E:\\VP6.flv", "/MIS/video/VP6.flv"));
  176. myFtp.disconnect();
  177. } catch (IOException e) {
  178. System.out.println("连接FTP出错:"+e.getMessage());
  179. }
  180. }kmnk03.com   kmnk01.com
  181. }www.kmnk03.com   www.kmnk01.com
复制代码
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值