hehe2670 2015-06-19 03:48 采纳率: 25%
浏览 2356
已采纳

java 下载图片线程卡死问题,是什么原因, 怎么解决,谢谢

 public static void downLoadImg(String imgName, String imgUrl, String fileURL) throws Exception{
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            // 创建流
            System.out.println("imgUrl:" + imgUrl);
            in = new BufferedInputStream( new URL(imgUrl).openStream());
            // 生成图片名
            int index = imgUrl.lastIndexOf("/");
            String sName = imgName == null ? imgUrl.substring(index + 1, imgUrl.length()) : imgName;
            File fir = new File(fileURL);
            if (!fir.exists()) {
                fir.mkdirs();
            }
            // 存放地址
            File img = new File(fileURL + sName);
            // 生成图片
            out = new BufferedOutputStream( new FileOutputStream(img));
            byte[] buf = new byte[2048];
            int length = in.read(buf);
            while (length != -1) {
                out.write(buf, 0, length);
                length = in.read(buf);//在一个下载图片的线程有的时候读取到这里就不动了 ,这种问题应该怎么解决
            }
        } catch (Exception e) {
            System.out.println("下载图片异常");
            e.printStackTrace();
            throw new RuntimeException("下载图片异常");   
        } finally{
            try {
                if( in != null){
                    in.close();
                }
                if(out != null){
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 写回答

5条回答

  • lhl_lqc 2015-06-19 04:27
    关注

    打印一些信息调试一个那个地方,看有没有死循环。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题