Java批量下载URL图片,为何总有些图片下载不完整?

2026-04-01 23:031阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计184个文字,预计阅读时间需要1分钟。

Java批量下载URL图片,为何总有些图片下载不完整?

你将代码改成了这样:

Java批量下载URL图片,为何总有些图片下载不完整?

javapublic class Client { public static void main(String[] args) { try { URL url=new URL(http://www.ite); } catch (Exception e) { e.printStackTrace(); } }}

你把代码改成这样试试publicclassClient{publicstaticvoidmain(String[]args){try{URLurlnewURL(www.iteye.com/upload/logo/user/1177132/a7159cc1-b11a-3122-9a9d-5183d6c6ba99.jpg");

HttpURLConnection connection (HttpURLConnection) url.openConnection();

connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.19 Safari/537.36");

connection.setConnectTimeout(5 * 1000);

InputStream inputStream connection.getInputStream();

byte[] tmp new byte[1024];

int length;

OutputStream outputStream new FileOutputStream("E:" File.separator "eee.jpg");

while ((length inputStream.read(tmp)) ! -1) {

outputStream.write(tmp, 0, length);

}

outputStream.close();

inputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

不添加UA下载下来的文件其实是这样的

本文共计184个文字,预计阅读时间需要1分钟。

Java批量下载URL图片,为何总有些图片下载不完整?

你将代码改成了这样:

Java批量下载URL图片,为何总有些图片下载不完整?

javapublic class Client { public static void main(String[] args) { try { URL url=new URL(http://www.ite); } catch (Exception e) { e.printStackTrace(); } }}

你把代码改成这样试试publicclassClient{publicstaticvoidmain(String[]args){try{URLurlnewURL(www.iteye.com/upload/logo/user/1177132/a7159cc1-b11a-3122-9a9d-5183d6c6ba99.jpg");

HttpURLConnection connection (HttpURLConnection) url.openConnection();

connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.19 Safari/537.36");

connection.setConnectTimeout(5 * 1000);

InputStream inputStream connection.getInputStream();

byte[] tmp new byte[1024];

int length;

OutputStream outputStream new FileOutputStream("E:" File.separator "eee.jpg");

while ((length inputStream.read(tmp)) ! -1) {

outputStream.write(tmp, 0, length);

}

outputStream.close();

inputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}

不添加UA下载下来的文件其实是这样的