Java URL下载图片无法打开问题

Java URL下载图片无法打开问题,第1张

Java URL下载图片无法打开问题

        最近在写Java爬虫,要爬取图片,图片下载工具类如下:

public class DownLoadUtils {
    
    public static void download(String urlString, String filename, String savePath) throws Exception {
        // 构造URL
        URL 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();

    }

    
    public static String subFileName(String fileName) {
        // 查找最后一个 出现位置
        int index = fileName.lastIndexOf("\");
        if (index == -1) {
            return fileName;
        }
        return fileName.substring(index + 1);
    }

    
    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);

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5562840.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存