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();
}
}
}
java 下载图片线程卡死问题,是什么原因, 怎么解决,谢谢
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
5条回答
- lhl_lqc 2015-06-19 04:27关注
打印一些信息调试一个那个地方,看有没有死循环。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报