Java URL下载图片无法打开问题
生活随笔
收集整理的这篇文章主要介绍了
Java URL下载图片无法打开问题
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
最近在写Java爬虫,要爬取图片,图片下载工具类如下:
public class DownLoadUtils {/*** 下载图片工具** @param urlString* 图片链接地址* @param filename* 图片的文件名字* @param savePath* 图片保存的路径* @throws Exception*/public static void download(String urlString, String filename, String savePath) throws Exception {// 构造URLURL url = new URL(urlString);// 打开连接URLConnection con = url.openConnection();// 设置请求头con.addRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)");con.addRequestProperty("Accept-Encoding", "gzip");con.addRequestProperty("Referer","no-referrer");con.addRequestProperty("Content-Type","application/x-www-form-urlencoded");// 设置请求超时为5s//con.setConnectTimeout(5 * 1000);// 输入流InputStream is = con.getInputStream();// 1K的数据缓冲byte[] bs = new byte[1024];// 读取到的数据长度int len;// 输出的文件流File sf = new File(savePath);if (!sf.exists()) {sf.mkdirs();}OutputStream os = new FileOutputStream(sf.getPath() + "\\" + filename);// 开始读取while ((len = is.read(bs)) != -1) {os.write(bs, 0, len);}// 完毕,关闭所有链接os.close();is.close();}/*** 截取真实文件名** @param fileName* @return*/public static String subFileName(String fileName) {// 查找最后一个 \出现位置int index = fileName.lastIndexOf("\\");if (index == -1) {return fileName;}return fileName.substring(index + 1);}/*** 获得随机UUID文件名** @param fileName* @return*/public static String generateRandonFileName(String fileName) {// 获得扩展名String ext = fileName.substring(fileName.lastIndexOf("."));return UUID.randomUUID().toString().replace("-", "") + ext;} }下载下来的图片和浏览器下载的大小一样,说明没有文件损坏,但是就是打不开。
最后发现,URL下载的图片是gzip格式,需要将后缀改为.zip然后解压,里面的文件加上.jpg后缀就可以正常打开了。
如果想下载后就得到未压缩的图片则可以设置请求头为
con.addRequestProperty("Accept-Encoding", "identity");如果无法解决,则在下载图片的IO流处入手
// 输入流InputStream is = con.getInputStream();GZIPInputStream gzips = new GZIPInputStream(is);总结
以上是生活随笔为你收集整理的Java URL下载图片无法打开问题的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: C#中$用法
- 下一篇: Java飞机大战游戏(需求分析+代码+图