java通过url获取 jpg、png、pdf 文件格式

目录

前言

如果一个 url

例如 : https://i-blog.csdnimg.cn/direct/4b54f5f678bb404f8f92007792e46592.png

上面的链接我们可以轻松的通过 后缀 .png 判断它的文件类型,但是很多时候有的 url 链接是不带这种后缀的,并且有时候即使 url 带了.png 后缀,它下载下来也不一定是 png 文件 ,所以需要下载文件后根据文件字节码去判断。

本文通过文件url 获取到文件字节数组,通过文件魔数(Magic Number)识别文件类型,比只靠文件后缀识别更可靠。

依赖

xml 复制代码
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.29</version>
        </dependency>

枚举类和工具类

java 复制代码
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 文件类型枚举
 *
 */
@Getter
@AllArgsConstructor
public enum FileTypeEnum {

    /**
     * JEPG.
     */
    JPEG("FFD8FF", "image/jpg"),


    /**
     * PNG.
     */
    PNG("89504E47", "image/png"),

    /**
     * Adobe Acrobat.
     */
    PDF("255044462D312E", "application/pdf"),

    OTHER("OTHER", "OTHER");

    private String value;

    private String contentType;
}
java 复制代码
import com.hai.tang.model.FileTypeEnum;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Base64;

public class FileTypeUtils {

    /**
     * 从文件 url 下载获取文件字节数组
     * @param url 文件url
     * @turn FileTypeEnum 文件类型枚举
     */
    public static ResponseEntity<byte[]> downloadFile(final String url) {
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        // 设置超时时间为5秒
        requestFactory.setReadTimeout(5000);
        requestFactory.setConnectTimeout(60000);

        RestTemplate restTemplate = new RestTemplate(requestFactory);
        HttpHeaders headers = new HttpHeaders();
        ResponseEntity<byte[]> entity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(headers), byte[].class);
        return entity;
    }

    /**
     * 从文件字节数组中前16比特获取文件类型
	 * @param bytes 文件字节数组
	 * @turn FileTypeEnum 文件类型枚举
     */
    public static FileTypeEnum getFileType(byte[] bytes) {
        int length = Math.min(bytes.length, 16);
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < length; i++) {
            sb.append(String.format("%02X", bytes[i]));
        }
        String fileHead = sb.toString();
        for (FileTypeEnum type : FileTypeEnum.values()) {
            if (fileHead.startsWith(type.getValue())) {
                return type;
            }
        }
        return FileTypeEnum.OTHER;
    }
    
     /**
     * 将 base64 文件保存到本地
     *
     * @param fileBase64 文件base64
     * @param fileType 文件类型 FileTypeEnum 里的 contentType
     * @param outPath 文件要保存到的本地路径,如: C:\Users\haitang\Desktop
     *
     */
    public static void save(String fileBase64, String fileType, String outPath) throws IOException {
        //将 base64 文件 转为 字节数组
        byte[] bytes = Base64.getDecoder().decode(fileBase64);
        String suffix = fileType.split("/")[1];
        DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd HH-mm-ss");
        String time = LocalDateTime.now().format(f);
        String fileName = time + "." + suffix;
        //文件保存到本地
        Files.write(Paths.get(outPath + File.separator + fileName), bytes);
    }

}

测试:

用于测试的三个 png、jpg、pdf 文件链接

java 复制代码
https://i-blog.csdnimg.cn/direct/4b54f5f678bb404f8f92007792e46592.png
https://pic3.zhimg.com/v2-e88c84efc46685275936b278784c0d16_1440w.jpg
https://www.kkoworld.com/kitablar/Nikolas_Sparks_Sevimli_Con_eng.pdf

测试代码如下,通过工具类下载 url 链接为字节数组然后打印文件类型

java 复制代码
import com.hai.tang.util.FileTypeUtils;
import org.springframework.http.ResponseEntity;  

public class MainServer {
	public static void main(String[] args) throws Exception {
        String fileUrl = "https://i-blog.csdnimg.cn/direct/4b54f5f678bb404f8f92007792e46592.png";
        ResponseEntity<byte[]> entity = FileTypeUtils.downloadFile(fileUrl);
        if (entity.hasBody() && null != entity.getBody()) {
            //获取文件类型 image/jpg 或 image/png 或 application/pdf
            String attachmentType = FileTypeUtils.getFileType(entity.getBody()).getContentType();
            System.out.println(attachmentType);

            //将 entity.getBody() 即文件字节数组 转化为 base64 编码的字符串(可用于存储到数据库)
            String attachmentBase64Stream = DatatypeConverter.printBase64Binary(entity.getBody());
            // base64 编码的文件字符串 输出到 C:\Users\haitang\Desktop 保存
            FileTypeUtils.save(attachmentBase64Stream,attachmentType,"C:\\Users\\haitang\\Desktop");
        }
    }
}
相关推荐
SunnyDays10111 小时前
Java 实现 PDF 附件的添加与删除:四种实用方法
java·pdf·附件
phltxy1 小时前
Redis 持久化机制
java·redis·git
Gerardisite1 小时前
企业微信客户管理系统实战:标签、分层与自动化流程搭建
java·python·机器人·自动化·企业微信
ch.ju1 小时前
Java程序设计(第3版)第三章——数组的定义方式
java·开发语言
Chloeis Syntax1 小时前
JavaEE学习日记(2)---文件操作和IO
java·笔记·学习·java-ee
无风听海1 小时前
OAuth 2.0 response_type完全指南
java·开发语言·oauth
Cyan_RA91 小时前
SpringMVC 数据格式化处理 详解
java·开发语言·spring·mvc·ssm·springmvc·数据格式化
SunnyDays10111 小时前
Java 实现 PDF 中文文本查找与高亮的四种方法
java·pdf·查找文字
倒流时光三十年1 小时前
PostgreSQL 一次由 string_agg 引发的数据错位 Bug 深度复盘
java·postgresql·string_agg