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

记录一些遇见的bug——springBoot+minio文件上传为txt文件时,如果上传时编码格式不是utf-8,就会出现通过文件链接直接在windows窗口预览文件,文件内容乱码问题

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

记录一些遇见的bug——springBoot+minio文件上传为txt文件时,如果上传时编码格式不是utf-8,就会出现通过文件链接直接在windows窗口预览文件,文件内容乱码问题

记录一些遇见的bug——springBoot+minio文件上传为txt文件时,如果上传时编码格式不是utf-8,就会出现通过文件链接直接在windows窗口预览文件,文件内容乱码问题
  • 一、问题描述
  • 二、问题原因
  • 三、解决方案
    • (1)判断文件的编码格式
    • (2)将MultipartFile转换为File
    • (3)解决txt乱码问题
    • (4)File 转 MultipartFile
  • 四、需要注意的地方

一、问题描述

springBoot项目上传文件到minio上,获取到文件url,直接在浏览器访问url(即预览文件)时,文件格式为txt时,会出现文件乱码问题。

二、问题原因

txt文件不是utf-8编码

三、解决方案

上传txt文件前,创建fileUtil工具类:手动设置txt文件编码格式后再上传
步骤:

(1)判断文件的编码格式
    public static String getCharset(InputStream is) throws IOException {
        BufferedInputStream bin = new BufferedInputStream(is);
        int p = (bin.read() << 8) + bin.read();//读取文件头前16位
        String code = null;
        switch (p) {
            case 0xefbb:
                code = "UTF-8";
                break;
            case 0xfffe:
                code = "Unicode";
                break;
            case 0xfeff:
                code = "UTF-16";
                break;
            default:
                code = "GB2312";
        }
        return code;
    }
(2)将MultipartFile转换为File
    public static File MultipartFileToFile(MultipartFile multiFile) {
        // 获取文件名
        String fileName = multiFile.getOriginalFilename();
        // 获取文件后缀
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        // 若须要防止生成的临时文件重复,能够在文件名后添加随机码

        try {
            File file = File.createTempFile(fileName, prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
(3)解决txt乱码问题
    public static File FileTurnUTF8(InputStream is, File targetFile, String code) throws IOException {
        if (!targetFile.exists()) {
            targetFile.createNewFile();
        }
        BufferedReader br = null;
        BufferedWriter bw = null;
        br = new BufferedReader(new InputStreamReader(is, code));
        bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8"));
        int i = 0;
        String str = "";
        while ((str = br.readLine()) != null) {
            byte[] bytes = str.getBytes("UTF-8");
            str = new String(bytes, 0, bytes.length);
            bw.write(str + "rn");
        }

        br.close();
        bw.close();
        return targetFile;
    }
(4)File 转 MultipartFile
    public static MultipartFile fileToMultipartFile(File file) throws Exception {

        FileInputStream fileInput = new FileInputStream(file);
//        MultipartFile toMultipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
//                IOUtils.toByteArray(fileInput));
        MultipartFile toMultipartFile = new MultipartFileDto("file", file.getName(), "text/plain",
                IOUtils.toByteArray(fileInput));
        toMultipartFile.getInputStream();
        return toMultipartFile;
    }
四、需要注意的地方
  • 将File 转 MultipartFile时,不能使用spring-test包中的MockMultipartFile方法转换。因为该依赖仅在开发环境生效,不会被打包。
转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1036546.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

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