gpt4 book ai didi

android - 将移动设备的 SDCard 中的文件上传到文件服务器时出现进度条问题

转载 作者:行者123 更新时间:2023-11-29 14:00:45 26 4
gpt4 key购买 nike

使用以下代码通过 SIM 卡/WiFi 从移动应用程序实现文件(图像、文本等)上传(从移动设备的 SD 卡)到文件服务器位置的功能。

文件上传功能在以下代码下运行良好,这里只有进度对话框的更新是个问题。

uploadFile() 从 AsyncTask 的 doInBackground(String... params) 调用

protected void onProgressUpdate(String... values) 处理进度对话框百分比更新

下面的代码负责文件上传

以下代码遇到的问题:

i> 进度对话框以适当的间隔从 0% 更新到 100%,然后等待很长时间,然后返回文件状态( bool 值)。

protected boolean uploadFile(String serverUrl, String filePath) {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int serverResponseCode;
String serverResponseMessage;
boolean uploadstatus = false;
int count;
long lengthOfFile;

try {
FileInputStream fileInputStream;
fileInputStream = new FileInputStream(new File(filePath));
URL url = new URL(serverUrl);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
// 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);
outputStream.writeBytes("Content-Disposition: form-data; name=\"upload\";filename=\""
+ Utils.getInstance().getFileName(filePath) + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
lengthOfFile = new File(filePath).length();// length of file
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[1024];
bytesRead = 0;
String progressMsg = "";
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
total += bytesRead;
progressMsg = new StringBuffer("").append((int) ((total * 100) / totalLengthOfFile))
.toString();
prgressBarMsg[0] = progressMsg;
publishProgress(prgressBarMsg);
outputStream.write(buffer);
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = connection.getResponseCode();
serverResponseMessage = connection.getResponseMessage();
if (serverResponseCode == 200)// HTTP OK Message from server
{
uploadstatus = true;
} else {
uploadstatus = false;
}
catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
connection.disconnect();
}
return uploadstatus;
}

在我看来,进度对话框上显示的百分比实际上是从文件(位于 SDCard 上)读取到缓冲区的数据百分比,而不是通过 SIM 从移动设备发送到文件服务器的数据卡/无线网络。进度对话框显示100%后的长时间延迟是我担心的原因。

请确认源代码是否显示了更新进度对话框的正确方法。此外,水平样式的进度对话框同时显示值 100 %100/100

如何去除100/100的显示?

欢迎任何替代提示/建议。

最佳答案

我觉得还好。延迟的问题在于您的百分比是您上传的文件的百分比,但是所涉及的全部工作包括从服务器获取响应(可能还有其他内容)。这需要时间,并且是对话达到 100% 后出现延迟的原因。

如果您关心用户体验,您可以将文件上传量减少,例如总进度的95%,收到回复后更新到100%。在关闭对话框之前,您可能还在 onPostExecute 中做了一些工作,但您没有显示该代码,所以我不能肯定地说。

关于android - 将移动设备的 SDCard 中的文件上传到文件服务器时出现进度条问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9572165/

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