如何使用java恢复ftp中断的文件上传?

时间:2014-02-26 05:59:50

标签: java ftp

您好我正在使用Apache通用网络jar进行FTP上传和下载。我需要在FTP上传中实现Resume功能。我提到了很多网站和文章,但找不到正确的解决方案。我在下面附上了我的代码。

FTPClient client;

public ApacheFtpClient() {
    client = new FTPClient();
}

public static void main(String[] args) {
    ApacheFtpClient ftpClient = new ApacheFtpClient();
    ftpClient.upload("myHostAddress", "username", "password", 
                                           "C:\\File.zip", "Rajesh.zip");

}

public void upload(String hostName, String userName,  
                   String password, String fileName, String remoteFileName) {
    try {
        client.connect(hostName);
        client.login(userName, password);
        client.setFileType(FTPClient.BINARY_FILE_TYPE);
        client.enterLocalPassiveMode();
        InputStream input = new FileInputStream(new File(fileName));
        //This is my Interupted file size in server 
        client.setRestartOffset(158400512); 
        System.out.println("File Upload Started.");
        client.appendFile(remoteFileName, input);
    } catch (Exception e) {
        e.printStackTrace();
    }
} // For Download ..

public void download(String hostName, String userName, 
                            String password, String fileName) {
        try {
            client.connect(hostName);
            client.login(userName, password);
            client.enterLocalPassiveMode();
            client.setFileType(FTPClient.BINARY_FILE_TYPE);
            OutputStream outputStream = 
                              new FileOutputStream(new File("local.zip"));
            client.retrieveFile(fileName, outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案