gpt4 book ai didi

java - 在android中使用带有multipart的HttpUrlConnection上传文件的进度非常快

转载 作者:行者123 更新时间:2023-12-02 10:41:52 24 4
gpt4 key购买 nike

我正在尝试使用带有 HttpUrlConnection POST 方法的多部分来上传文件。文件已正确上传,我正在收到相应的回复。但当我跟踪进度时,即使文件 >100 MB,它也只会在 1 秒内给出所有进度。看起来进度是将文件写入缓冲区而不是网络 OutputStream。写入每个数据 block 后在流上调用flush() 没有帮助。看起来刷新只是清除网络流,并且在写入下一个 block 之前不等待响应。

这是我上传文件的代码:

//Initialised in Constructor
boundary = twoHyphens + System.currentTimeMillis() + twoHyphens;

URL url = new URL(requestURL);
private HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setUseCaches(false);
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);

//This method is called to upload a file
public String uploadFile(String fieldName, File uploadFile) throws IOException, InterruptedException {
String fileName = uploadFile.getName();
FileInputStream fileInputStream = new FileInputStream(uploadFile);
DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(httpConn.getOutputStream()));

dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"" + fieldName
+ "\"; filename=\"" + fileName + "\"" + lineEnd);
dataOutputStream.writeBytes(lineEnd);

int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;

//returns no. of bytes present in fileInputStream
bytesAvailable = fileInputStream.available();

bufferSize = 4096;
buffer = new byte[4096];
long size = uploadFile.length();

bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0) {
int percentage = (int) ((bytesRead / (float) size) * 100);
dataOutputStream.write(buffer, 0, bufferSize);
dataOutputStream.flush(); //doesn't help
bytesAvailable = fileInputStream.available();
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}//This finishes in 1 second

dataOutputStream.writeBytes(lineEnd);
dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

int status = httpConn.getResponseCode();
StringBuilder sb = new StringBuilder();
BufferedReader reader = null;

try {
if (status == HttpURLConnection.HTTP_OK) {
reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} else {
throw new IOException("Server exception with status code: " + status);
}
} catch (Exception e) {

} finally {
if (reader != null) {
reader.close();
httpConn.disconnect();
}
}
return sb.toString();
}

对此的任何帮助或解释都非常感谢。

最佳答案

写入输出流实际上并不触发网络连接。当您调用 getResponseCode() 时,数据上传到服务器就完成了,正如我在其他 ansswer 中详细介绍的那样。

只有当您有办法要求 getResponseCode() 报告进度时,否则您无法监控进度。

关于java - 在android中使用带有multipart的HttpUrlConnection上传文件的进度非常快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52871660/

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