Java 上传文件/上传图片 data:image/jpg, data:image/jpeg, data:image/png 亲测

时间:2025-04-15 07:45:17

Java 上传图片 data:image/jpg, data:image/jpeg, data:image/png

    • 1. 判断允许上传文件的 文件后缀/图片后缀/相片后缀 和 其它工具类
    • 2. 上传接口
    • 3. 上传成功

  • 上传通过 base64 编码后的图 Data URL

1. 判断允许上传文件的 文件后缀/图片后缀/相片后缀 和 其它工具类


import ;
import ;
import ;
import ;

@Component
public class FileUtil {
    public static final List<String> IMAGE_EXTENSIONS = (".jpg", ".jpeg", ".png");
}

import ;

public class CommonUtil {
    public static String getUUID() {
        String uuid = ().toString().replaceAll("-", "");
        return uuid;
    }
}

import ;
import ;

public class DateUtil {
    public static final String HH = "HH";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final DateTimeFormatter FORMATTER_HH = (HH);
    public static final DateTimeFormatter FORMATTER_YYYY_MM_DD = (YYYY_MM_DD);
    public static String getHH() {
        return FORMATTER_HH.format(());
    }
    public static String getYYYYMMDD() {
        return FORMATTER_YYYY_MM_DD.format(());
    }
}

2. 上传接口


    @PostMapping("/")
    public Map<String, String> updateImageOfWeb(@RequestParam("image") String fileTemp) {
        	Map<String, String> result = new HashMap<>();
        if ((fileTemp)) {
			("message", "请选择图片!");
			return result;
        }

        String suffix = null;
        if (("data:image/jpg")) {
            suffix = ".jpg";
        } else if (("data:image/jpeg")) {
            suffix = ".jpeg";
        } else if (("data:image/png")) {
            suffix = ".png";
        }

        if (suffix == null) {
			("message", "图片格式错误!");
			return result;
        }

        if (!FileUtil.IMAGE_EXTENSIONS.contains(suffix)) {
			("message", "图片格式错误!");
			return result;
        }

        String lastFilePath;
        String newFileName = () + suffix;
        String folderName =  + "temp" + ;
        String relativePath = folderName + () +  + ();
        String filePath = "D:\\file" + relativePath;
        String fileUrl = null;
        File targetFile = new File(filePath);
        if (!()) {
            ();
        }

        FileOutputStream out = null;
        final BASE64Decoder decoder = new BASE64Decoder();
        try {
            lastFilePath = filePath +  + newFileName;
            out = new FileOutputStream(lastFilePath);
            byte[] decoderBytes = ((",")[1]);
            (decoderBytes);
            fileUrl = "http://127.0.0.1:9000" + relativePath +  + newFileName;
        } catch (Exception e) {
            ();
        } finally {
            if (out != null) {
                try {
                    ();
                } catch (IOException e) {
                    ();
                }
                try {
                    ();
                } catch (IOException e) {
                    ();
                }
            }
        }

        if (fileUrl == null) {
			("message", "图片上传失败!");
			return result;
        }

		("message", "上传成功!");
		("url", fileUrl);
		return result;
    }

3. 上传成功


url => http://127.0.0.1:9000/temp/2019-09-12/14/

如果您觉得有帮助,欢迎点赞哦 ~ 谢谢!!