【发布时间】:2020-02-07 15:36:17
【问题描述】:
我正在尝试使用以下代码从谷歌驱动器下载 PDF 文件:
try {
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.connect();
if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK){
Log.v(TAG,"server returned http " + urlConnection.getResponseCode()
+ urlConnection.getResponseMessage());
}
else{
Log.v(TAG + "downloading" ,"server returned http " + urlConnection.getResponseCode()
+ urlConnection.getResponseMessage());
}
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(directory);
int totalSize = urlConnection.getContentLength();
int total = 0;
byte[] buffer = new byte[MEGABYTE];
int bufferLength = 0;
int i = 0;
while((bufferLength = inputStream.read(buffer))!= -1 )
{
i+=1;
Log.v(TAG + "downloading","downloading mega #"+i);
total += bufferLength;
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
inputStream.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
如代码所示,while循环中每次迭代下载一个mega文件,但是2650次迭代只下载2个mega文件!!!!!! 知道如何解决这个问题吗?
【问题讨论】:
标签: android performance google-drive-api google-drive-android-api