程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java 圖片 下載 線程-java 下載圖片線程卡死問題,是什麼原因, 怎麼解決,謝謝

java 圖片 下載 線程-java 下載圖片線程卡死問題,是什麼原因, 怎麼解決,謝謝

編輯:編程綜合問答
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();
            }
        }
    }

最佳回答:


打印一些信息調試一個那個地方,看有沒有死循環。

hehe2670
bulusli3
hehe2670
hehe2670
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved