免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5837 | 回复: 1
打印 上一主题 下一主题

FTPClient 上传大文件时 ,老不成功,怎么解决 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-12-26 09:44 |只看该作者 |倒序浏览
FTPClient 上传大文件时 ,老提示:


Software caused connection abort: recv failed
//

Software caused connection abort: socket write error



上传成功后 出错代码:

try{               
                        out.flush();
                        System.out.println("ok;" );
                        raf.close();
                        out.close();
                        System.out.println(ftpClient.getReplyCode());
                        NOOP = ftpClient.sendNoOp();
                        if(NOOP) {
                                System.out.println("noop:" + NOOP);
                        } else {
                                System.out.println("error:" + NOOP);
                        }
                        boolean result = ftpClient.completePendingCommand();
                        if (remoteSize > 0) {
                                status = result ? "Upload_From_Break_Success" : "UploadStatus.Upload_From_Break_Failed";
                        } else {
                                status = result ? "Upload_New_File_Success" : "Upload_New_File_Failed";
                        }
                        return status;
                } catch (Exception e) {
                        System.out.println("error:"+e.getMessage());
                        logger.error("error:" + e.getMessage());
                        status = process < 100 ? "Remote_Time_Out" : "Remote_Time_Out_Over";
                }

论坛徽章:
0
2 [报告]
发表于 2012-12-26 09:47 |只看该作者
  1. public String uploadFile(String remoteFile, File localFile,        FTPClient ftpClient, long remoteSize) throws IOException {
  2.                 String status;
  3.                 // 显示进度的上传
  4.                 long step = localFile.length() / 100;
  5.                 long process = 0;
  6.                 long localreadbytes = 0L;
  7.                 RandomAccessFile raf = new RandomAccessFile(localFile, "r");
  8.                 OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"), "iso-8859-1"));
  9.                 // 断点续传
  10.                 //ftpClient.setDataTimeout(7200000);
  11.                 if (remoteSize > 0) {
  12.                         ftpClient.setRestartOffset(remoteSize);
  13.                         process = remoteSize / step;
  14.                         raf.seek(remoteSize);
  15.                         localreadbytes = remoteSize;
  16.                 }

  17.                 byte[] bytes = new byte[1024];
  18.                 int c;
  19.                 Date mytime = new Date();
  20.                 long beginTime = mytime.getTime();
  21.                 long passtime;
  22.                 System.out.println(beginTime);

  23.                 boolean NOOP = false;
  24.                 //while ((c = raf.read(bytes)) != -1) {
  25.                 while ((c = raf.read(bytes)) > 0) {
  26.                         out.write(bytes, 0, c);
  27.                         localreadbytes += c;

  28.                         if (localreadbytes / step != process) {
  29.                                 process = localreadbytes / step;
  30.                                 System.out.println("文件:"+remoteFile+" 进度:" + process + "%," + localreadbytes / 1024 + " KB");
  31.                                 // TODO 汇报上传状态                       
  32.                         }
  33.                         Date passMyTime = new Date();
  34.                         passtime = passMyTime.getTime();
  35.                         //System.out.println(passtime - beginTime);
  36.                         if((passtime - beginTime) >= 6000) {
  37.                                 beginTime = passtime;
  38.                                 System.out.println(beginTime);
  39.                                 //System.out.print(ftpClient.getReplyString());
  40.                                 //NOOP = ftpClient.sendNoOp();
  41.                                 //System.out.println("noop:" + NOOP);
  42.                         }
  43.                 }
  44.                 try{               
  45.                         out.flush();
  46.                         System.out.println("ok;" );
  47.                         raf.close();
  48.                         out.close();
  49.                         System.out.println(ftpClient.getReplyCode());
  50.                         NOOP = ftpClient.sendNoOp();
  51.                         if(NOOP) {
  52.                                 System.out.println("noop:" + NOOP);
  53.                         } else {
  54.                                 System.out.println("error:" + NOOP);
  55.                         }
  56.                         boolean result = ftpClient.completePendingCommand();
  57.                         if (remoteSize > 0) {
  58.                                 status = result ? "Upload_From_Break_Success" : "UploadStatus.Upload_From_Break_Failed";
  59.                         } else {
  60.                                 status = result ? "Upload_New_File_Success" : "Upload_New_File_Failed";
  61.                         }
  62.                         return status;
  63.                 } catch (Exception e) {
  64.                         System.out.println("error:"+e.getMessage());
  65.                         logger.error("error:" + e.getMessage());
  66.                         status = process < 100 ? "Remote_Time_Out" : "Remote_Time_Out_Over";
  67.                 }
  68.                 return status;
  69.         }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP