栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571595.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号