gpt4 book ai didi

java - 从字符串中获取字节

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

如果 nUploadSize 是下面的 Long 整数,

nUploadSize += currentline.getBytes().length;

有没有办法得到实际的字节大小而不是字节的长度?如果我试试这个:

nUploadSize += currentline.getBytes();

它不接受说:

The operator += is undefined for the argument type(s) long, byte[]

我问这个问题的原因是因为我正在尝试使用显示百分比进度的对话框进行 httppost 上传。问题是在显示上传成功 toast 之前,进度条从不显示任何进度

我有以下代码:

boolean m_bSuccess = true; // initiating a boolean to be used later
try
{
//upload parameters done through multipartEntity

MultipartEntity me = new MultipartEntity();

File pFp = new File(strAttachedFile);

me.addPart("videoFile", new FileBody(pFp, "video/3gpp"));



httppost.setEntity(me);

// get the total size of the file
nTotalSize = pFp.length();

//httpPst execution
HttpResponse responsePOST = client.execute(httppost);
HttpEntity resEntity = responsePOST.getEntity();

//
InputStream inputstream = resEntity.getContent();//generate stream from post resEntity
BufferedReader buffered = new BufferedReader(new InputStreamReader(inputstream));//buffer initiated to read inputstream
StringBuilder stringbuilder = new StringBuilder();
String currentline = null;
while ((currentline = buffered.readLine()) != null)
{
// adding the

stringbuilder.append(currentline + "\n");// appending every line of upload to stringbuilder

String result = stringbuilder.toString();
nUploadSize += currentline.getBytes().length;// += upload size acquires all current line bites
//convert upload size from integer to string to be read
String s = String.valueOf(nUploadSize);
Log.i("Upload lineeeeeeeeeeeeeeeeee",s);
Log.v("HTTP UPLOAD REQUEST",result);
inputstream.close();}
}

catch (Exception e) {
e.printStackTrace();
m_bSuccess = false; //mbsucess is false on catch failure
}
publishProgress();

if(m_bSuccess)
return "success";
return null;
}


protected void onProgressUpdate(Integer... progress) {

if(m_vwProgress !=null){

m_vwProgress.setProgress((int)(nUploadSize/nTotalSize));
}

}


protected void onPostExecute(String result){

super.onPostExecute(result);

if(result==null){

if(m_vwProgress !=null)
m_vwProgress.dismiss();
Toast.makeText(m_activity, "Upload Successful", Toast.LENGTH_LONG).show();

}
}
}

尝试上传时,在我的 Log.i("Upload lineeeeeeeeeeeeeeeeeee",s) 期间;它显示了我从 nUploadSize += currentline.getBytes().length; 获得的最大字节大小是 200。解释我的主要问题为什么进度对话框从不显示百分比进度,因为数字太小。这就是为什么我想看看我是否真的得到了字符串字节大小。如果有人知道如何解决这个问题,我将不胜感激。

最佳答案

currentLine.getBytes().length “字节大小”,即字节数...

关于java - 从字符串中获取字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7986632/

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