Android斷點上傳實現的方法可以通過以下兩種途徑實現:
示例代碼(使用OkHttp):
OkHttpClient client = new OkHttpClient();
File file = new File("path/to/file");
long fileSize = file.length();
Request request = new Request.Builder()
.url("http://example.com/upload")
.addHeader("Content-Range", "bytes " + uploadedBytes + "-" + (fileSize - 1) + "/" + fileSize)
.post(RequestBody.create(MediaType.parse("application/octet-stream"), file))
.build();
Response response = client.newCall(request).execute();
示例代碼(使用Apache Commons Net庫實現FTP斷點上傳):
FTPClient ftpClient = new FTPClient();
ftpClient.connect("ftp.example.com");
ftpClient.login("username", "password");
File file = new File("path/to/file");
long uploadedBytes = ftpClient.mlistFile("remote/file").getSize();
ftpClient.setRestartOffset(uploadedBytes);
InputStream inputStream = new FileInputStream(file);
ftpClient.storeFile("remote/file", inputStream);
inputStream.close();
ftpClient.logout();
ftpClient.disconnect();
需要注意的是,斷點上傳功能需要服務器支持斷點續傳,以及相應的權限和配置。