ZWate 2017-02-07 06:58 采纳率: 0%
浏览 2218

关于使用HttpURLConnection下载大文件的一些问题 求大神指导

下面是我的函数
private void getFile() {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (this) {
URL url = null;
InputStream inputStream = null;
FileOutputStream outputStream = null;
long fileSize = 0;
try {
url = new URL(downloadUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(28000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() == 200) {
inputStream = connection.getInputStream();
File file = new File(Environment.getExternalStorageDirectory() + "/" + directoriy, filename);
outputStream = new FileOutputStream(file);
fileSize = connection.getContentLength();
int length = 0;
byte[] buffer = new byte[1024];
int downloadSize = 0;
while ((length = inputStream.read(buffer)) != -1) {
downloadSize = downloadSize + length;
progress[0] = downloadSize / (float) fileSize;

                            outputStream.write(buffer, 0, length);
                            handler.sendEmptyMessage(INVALIDATE1);
                        }
                        outputStream.flush();
                        outputStream.close();
                        handler.sendEmptyMessage(LOAD_SUCCESS1);
                    }
                } catch (MalformedURLException e) {
                    handler.sendEmptyMessage(LOAD_ERROR1);
                    e.printStackTrace();
                    e.getMessage();
                    try {
                        if (inputStream != null)
                            inputStream.close();
                        if (outputStream != null)
                            outputStream.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                } catch (IOException e) {
                    handler.sendEmptyMessage(LOAD_ERROR1);
                    e.printStackTrace();
                    if (inputStream != null)
                        try {
                            inputStream.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    if (outputStream != null)
                        try {
                            outputStream.close();
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                }
            }
        }
    }).start();

}

用上面的函数下载一个1个G的文件要改成POST方式吗?需要的话函数需要拿些改动呢,还有就是下载启动大文件之后对UI有很大的影响,列表直接卡住了,想请问用什么方法可以优化一下呢?

  • 写回答

1条回答 默认 最新

  • ruijindp 2017-02-07 09:55
    关注

    下载是耗时操作,肯定要开启一条新的线程来做这件事啊

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法