2

我正在使用 FTPClient 上传本地文件,但现在有一个我不知道如何修复的错误。

这是代码:

public UploadStatus uploadFile(String remoteFile,File localFile,FTPClient ftpClient,long remoteSize) throws IOException{  
        UploadStatus status;
        long step = localFile.length() / 100;  
        long process = 0;  
        long localreadbytes = 0L;  
        RandomAccessFile raf = new RandomAccessFile(localFile,"r");

        //Here is the point, some times, it's frozen here, no reponse from the server.
        OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("UTF-8"),"iso-8859-1"));  

        //breakpoint resume  
        if(remoteSize>0){  
            ftpClient.setRestartOffset(remoteSize);  
            process = remoteSize /step;  
            raf.seek(remoteSize);  
            localreadbytes = remoteSize;  
        }  
        byte[] bytes = new byte[1024];  
        int c;  
        while((c = raf.read(bytes))!= -1){  
            out.write(bytes,0,c);  
            localreadbytes+=c;  
            if(localreadbytes / step != process){  
                process = localreadbytes / step;  
                System.out.println("progress:" + process);  
                //TODO call the progressbar function here 
            }  
        }  
        out.flush();  
        raf.close();  
        out.close();  
        boolean result =ftpClient.completePendingCommand();
}

对于以下行,

OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("UTF-8"),"iso-8859-1"));

它有时很好,有时很糟糕。上传一个服务器中不存在的新文件,它可能运行良好,并且到 100%,但有时上传文件的一部分,然后冻结。(例如 45%),
我调试了代码,找到了,因为 [ ftpClient.appendFileStream] 没有响应。

谢谢 。

4

0 回答 0