- 论坛徽章:
- 0
|
- public String uploadFile(String remoteFile, File localFile, FTPClient ftpClient, long remoteSize) throws IOException {
- String status;
- // 显示进度的上传
- long step = localFile.length() / 100;
- long process = 0;
- long localreadbytes = 0L;
- RandomAccessFile raf = new RandomAccessFile(localFile, "r");
- OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"), "iso-8859-1"));
- // 断点续传
- //ftpClient.setDataTimeout(7200000);
- if (remoteSize > 0) {
- ftpClient.setRestartOffset(remoteSize);
- process = remoteSize / step;
- raf.seek(remoteSize);
- localreadbytes = remoteSize;
- }
- byte[] bytes = new byte[1024];
- int c;
- Date mytime = new Date();
- long beginTime = mytime.getTime();
- long passtime;
- System.out.println(beginTime);
- boolean NOOP = false;
- //while ((c = raf.read(bytes)) != -1) {
- while ((c = raf.read(bytes)) > 0) {
- out.write(bytes, 0, c);
- localreadbytes += c;
- if (localreadbytes / step != process) {
- process = localreadbytes / step;
- System.out.println("文件:"+remoteFile+" 进度:" + process + "%," + localreadbytes / 1024 + " KB");
- // TODO 汇报上传状态
- }
- Date passMyTime = new Date();
- passtime = passMyTime.getTime();
- //System.out.println(passtime - beginTime);
- if((passtime - beginTime) >= 6000) {
- beginTime = passtime;
- System.out.println(beginTime);
- //System.out.print(ftpClient.getReplyString());
- //NOOP = ftpClient.sendNoOp();
- //System.out.println("noop:" + NOOP);
- }
- }
- 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";
- }
- return status;
- }
复制代码 |
|