gpt4 book ai didi

java - 进度条自己重置?

转载 作者:行者123 更新时间:2023-11-30 03:40:30 24 4
gpt4 key购买 nike

当我上传超过 5MB 的文件时,进度条会在达到 4% 时自行重置并每 10 秒从 0 开始。但是,对于 5MB 以下的文件,进度条工作正常并达到 100%

这是因为maxBufferSize吗?服务器最大帖子大小为 512MB,其他一切都是无限的,所以问题一定出在我的代码中,但我不确定在哪里。

这是我的代码

@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);

//update Dialog box progress
mProgressDialog.setProgress(progress[0]);
if ( nofti_appended )
{
//update Notification progress
mBuilder.setProgress(100, progress[0], false);
}
}


protected String doInBackground(String... urls) {
String upLoadServerUri = upApi.uploadUrl;
String fileName = this.file_path;
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
File sourceFile = new File(fileName);
int sentBytes = 0;
long fileSize = sourceFile.length();

try
{
FileInputStream fileInputStream = new FileInputStream(new File(fileName));

URL url = new URL(upLoadServerUri);
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);
outputStream.writeBytes("Content-Disposition: form-data; name=\"file[]\";filename=\""+ fileName + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);

bytesAvailable = fileInputStream.available();
Log.v("Size",bytesAvailable+"");

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


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

while (bytesRead > 0)
{

if(isCancelled()){

break;
}

sentBytes += bytesRead;
publishProgress((int)(sentBytes * 100 / fileSize));

outputStream.write(buffer, 0, bufferSize);

bytesAvailable = fileInputStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}


if(isCancelled()){

return "faild";
}

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

Scanner s;
s = new Scanner(connection.getInputStream());
s.useDelimiter("\\Z");
final String response = s.next();

// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();

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

if(serverResponseCode == 200)
{
return response;
}


} catch (MalformedURLException ex) {

} catch (final Exception e) {

}

return "faild";
}

有什么想法吗?

最佳答案

我认为你的进度计算实际上有两个问题:

首先,你的逻辑不太对。应该是(原因见下一段):

(sentBytes / fileSize) * 100

但是,第二个问题是您正在使用 int 值,但试图进行浮点计算。 (百分比为0.01 - 1.00,然后乘以100变成1% - 100%。)您需要以 float 进行计算,然后将最终值转换为整数。

尝试类似的东西

publishProgress((int)(((sentBytes * 1.0) / fileSize) * 100)));

不过,这会变成很多 () 对。最好声明一个单独的 floatdouble 变量,然后使用它:

percentDone = (sentBytes * 1.0) / fileSize * 100;
publishProgress((int)percentDone);

关于java - 进度条自己重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15822954/

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