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 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿