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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料