gpt4 book ai didi

java - 使用 DataOutputStream 上传文件不显示实际进度

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:07:49 24 4
gpt4 key购买 nike

我正在使用带有以下代码的 DataOutputStream 将文件从 android 上传到我的网络服务器:

public class SnapshotUploadTask extends AsyncTask<Object, Integer, Void> {

private final File file;
private final ProgressDialog progressDialog;
private final Activity activity;

public SnapshotUploadTask(File file, ProgressDialog progressDialog, Activity activity) {
this.progressDialog = progressDialog;
this.file = file;
this.activity = activity;
}

@Override
protected void onPreExecute() {
progressDialog.setProgress(0);
}

@Override
protected Void doInBackground(Object... arg0) {

String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";

int bytesRead;
int bytesAvailable;
int bufferSize;

byte[] buffer;

int maxBufferSize = 1 * 512;

try {

FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL("http://bla.bla.bla/upload");

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);

connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("ENCTYPE", "multipart/form-data");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
connection.setRequestProperty("uploaded_file", file.getName());

DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());

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

dataOutputStream.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();
final int hundredPercent = bytesAvailable;
progressDialog.setMax(hundredPercent);

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

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

while (bytesRead > 0) {
dataOutputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);

final int restBytes = bytesAvailable;
final int uploadedBytes = hundredPercent - restBytes;

activity.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setProgress((int) uploadedBytes);
if (restBytes <= 0) {
progressDialog.setMessage(activity.getString(R.string.camera_uploading_done));
}
}
});
}

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

int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();

if (serverResponseCode == 200) {
progressDialog.dismiss();
}

fileInputStream.close();
dataOutputStream.flush();
dataOutputStream.close();

} catch (Exception e) {
progressDialog.dismiss();
}

return null;
}

@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
}
}

现在发生的事情是,我在将一定数量的字节写入 OutputStream 后立即更新我的 progressDialog,这导致 progressDialog 在服务器实际接收数据之前达到 100%。

只有当我到达 connection.getResponseCode();

时,我才知道传输何时完成

我想要的是在服务器实际接收到数据 block 时更新进度。有办法吗?

最佳答案

您需要注意 HttpURLConnection 缓冲所有输出,除非您设置固定长度或分块传输模式,您当然应该这样做而不是自己实现它。它这样做是为了准确地设置 Content-Length header 。如果您使用其中一种传输模式,则没有缓冲。

注意你滥用了 available():查看 Javadoc;在大多数情况下你写的都是垃圾:

dataOutputStream.write(buffer, 0, bufferSize);

应该是

dataOutputStream.write(buffer, 0, bytesRead);

您也不需要在读取循环中摆弄 available()。这样的事情就足够了:

long total = 0;
int maxBufferSize = 8192; // at least. 512 is far too small.
while ((bytesRead = fileInputStream.read(buffer, 0, bufferSize)) > 0) {
dataOutputStream.write(buffer, 0, bytesRead);
total += bytesRead;
final long uploadedBytes = total;

activity.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setProgress(uploadedBytes);
}
});
}
}
// at this point we are at end of stream
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setMessage(activity.getString(R.string.camera_uploading_done));
}
}

环境与环境

关于java - 使用 DataOutputStream 上传文件不显示实际进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28596982/

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