gpt4 book ai didi

java - 上传大文件时暂停/恢复 Httpurl 连接 - Android

转载 作者:可可西里 更新时间:2023-11-01 16:40:30 29 4
gpt4 key购买 nike

我能够使用 httpurlconnection 使用 .setChunkedstreamingmode() 将大文件(测试高达 1.2GB)上传到服务器。我了解到我们必须保持 url 连接有效才能发送大文件。我用来上传大文件的代码是,

public static String uploadFileToServer(String filename, String targetUrl) {
String response = "error";
HttpURLConnection connection = null;
DataOutputStream outputStream = null;

String pathToOurFile = filename;
String urlServer = targetUrl;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";

int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024;
try {
FileInputStream fileInputStream = new FileInputStream(new File(
pathToOurFile));

URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();

// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(1024);
// Enable POST method
connection.setRequestMethod("POST");

connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);

outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);

String token = "anyvalye";
outputStream.writeBytes("Content-Disposition: form-data; name=\"Token\"" + lineEnd);
outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
outputStream.writeBytes("Content-Length: " + token.length() + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(token + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);

String taskId = "anyvalue";
outputStream.writeBytes("Content-Disposition: form-data; name=\"TaskID\"" + lineEnd);
outputStream.writeBytes("Content-Type: text/plain;charset=UTF-8" + lineEnd);
outputStream.writeBytes("Content-Length: " + taskId.length() + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(taskId + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);

String connstr = null;
connstr = "Content-Disposition: form-data; name=\"UploadFile\";filename=\""
+ pathToOurFile + "\"" + lineEnd;

outputStream.writeBytes(connstr);
outputStream.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
System.out.println("Image length " + bytesAvailable + "");
try {
while (bytesRead > 0) {
try {
outputStream.write(buffer, 0, bufferSize);
} catch (OutOfMemoryError e) {
e.printStackTrace();
response = "outofmemoryerror";
return response;
}
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
} catch (Exception e) {
e.printStackTrace();
response = "error";
return response;
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens
+ lineEnd);

// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
System.out.println("Server Response Code " + " " + serverResponseCode);
System.out.println("Server Response Message "+ serverResponseMessage);

if (serverResponseCode == 200) {
response = "true";
}else
{
response = "false";
}

fileInputStream.close();
outputStream.flush();

connection.getInputStream();
//for android InputStream is = connection.getInputStream();
java.io.InputStream is = connection.getInputStream();

int ch;
StringBuffer b = new StringBuffer();
while( ( ch = is.read() ) != -1 ){
b.append( (char)ch );
}

String responseString = b.toString();
System.out.println("response string is" + responseString); //Here is the actual output

outputStream.close();
outputStream = null;

} catch (Exception ex) {
// Exception handling
response = "error";
System.out.println("Send file Exception" + ex.getMessage() + "");
ex.printStackTrace();
}
return response;
}

现在在我的上传过程中,如果我暂停上传并继续,那么它会从第一个字节开始,而不是计算服务器接收到的 block 。为了克服这个问题,我编码为

// Initial download.
String lastModified = connection.getHeaderField("Last-Modified");

// Resume Upload.
connection.setRequestProperty("If-Range", lastModified);

但我无法恢复上传过程。任何人都可以在这个问题上帮助我,因为我是新学习这个概念的。提前致谢。

最佳答案

如果接收服务器支持,您可以使用 Content-Range header 来标识恢复上传。 Google-Drive API 支持它。如果您自己推出,我会遵循 Google 使用的模式:

  • 开始上传并获取 session 标识符。
  • 上传中断时,请等待互联网恢复可用。
  • 恢复上传时,首先向服务器查询它收到了多少字节。 (*)
  • 在服务器状态后的下一个字节恢复上传。

(*) 请注意,当我之前自己滚动时,我添加了来自服务器的编码响应以及上传的最后 KB,只是为了验证它在传输过程中没有损坏。但是,在生产中,我从未见过服务器收到损坏数据的情况。

关于java - 上传大文件时暂停/恢复 Httpurl 连接 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45464795/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com