解决txt文件上传oss服务器乱码的问题

摘要:
Stringcode2=getCode2(file.getInputStream());}否则{Stringstr=IOUItils.toString(file.getInputStream();byte[]head=newbyte[3];outputStream.write(head;while((nrread=inputStream.read(buf))>

今天上传txt文件下载下来却乱码,搞了一下午,发现还挺复杂。记录一下。

1.首先服务器只接受utf-8格式的文件,所以首先想到的就是转码问题。

解决txt文件上传oss服务器乱码的问题第1张

 这是网上很容易就找到的判断文件编码的代码。判断出来之后如果是UTF8格式的文件就正常上传,如果不是就先转成UTF8格式再上传。

我以为问题解决了的时候,发现上传之后还是乱码。然后我就创建两个内容一样但是编码不一样的文件仔细比较,发现我转码之后的byte数组少了正常文件的utf8标识符。

然后byte数组前面就要加上-17  -69 -65的标识符

 代码如下

解决txt文件上传oss服务器乱码的问题第2张

就我以为要万事大吉的时候,那边测试告诉我还不行。于是我把他的文件拿过来测试。

发现他的文件是UTF8格式,但是没有标识符!!我的getCode代码判断不出来他是UTF8 当成GBK处理了,自然还是乱码。

于是我百度一番,找到了一个jar包可以帮住我识别他是utf8文件,可是直接上传还不行,因为没有前缀 oss服务器那边也不认。

于是我就要用jar包的方式判断编码来转码,用前缀判断编码的方式来给byte数组增加前缀。

 下面是完整的代码和关联的pom文件

<dependency>
<groupId>com.googlecode.juniversalchardet</groupId>
<artifactId>juniversalchardet</artifactId>
<version>1.0.3</version>
</dependency>
    private static InputStream create(MultipartFile file) throws IOException {
        if (!file.getOriginalFilename().endsWith("txt")) {
            return file.getInputStream();
        }

        OutputStream outputStream = new ByteArrayOutputStream();
        String code = getCode(file.getInputStream());
        String code2 = getCode2(file.getInputStream());
        //getFilecharset(file.getInputStream())
        if (code.equals("UTF-8")) {
            return file.getInputStream();
        } else {
            String str = IOUtils.toString(file.getInputStream(), code2);
            byte[] head = new byte[3];
            head[0] = -17;
            head[1] = -69;
            head[2] = -65;
            outputStream.write(head, 0, 3);
            outputStream.write(str.getBytes(), 0, str.getBytes().length);
            ByteArrayOutputStream baos = (ByteArrayOutputStream)outputStream;
            InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
            return inputStream;
        }
    }

    private static String getCode2(InputStream inputStream) throws IOException {
        UniversalDetector detector = new UniversalDetector(null);
        byte[] buf = new byte[4096];
        // (2)
        int nread;
        while ((nread = inputStream.read(buf)) > 0 && !detector.isDone()) {
            detector.handleData(buf, 0, nread);
        }
        // (3)
        detector.dataEnd();

        // (4)
        String encoding = detector.getDetectedCharset();

        return encoding;
    }

    private static String getCode(InputStream inputStream) {
        String charsetName = "gbk";
        byte[] head = new byte[3];
        try {
            inputStream.read(head);
            inputStream.close();
            if (head[0] == -1 && head[1] == -2 ) //0xFFFE
                charsetName = "UTF-16";
            else if (head[0] == -2 && head[1] == -1 ) //0xFEFF
                charsetName = "Unicode";//包含两种编码格式:UCS2-Big-Endian和UCS2-Little-Endian
            else if(head[0]==-27 && head[1]==-101 && head[2] ==-98)
                charsetName = "UTF-8"; //UTF-8(不含BOM)
            else if(head[0]==-17 && head[1]==-69 && head[2] ==-65)
                charsetName = "UTF-8"; //UTF-8-BOM
        } catch (Exception e) {

        }
        return charsetName;
    }

免责声明:文章转载自《解决txt文件上传oss服务器乱码的问题》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Sqlserver内存管理:限制最大占用内存C#访问带有安全协议的Webservice(https、生成wsdl代理类)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Mac 安装redis

一.下载安装 1. 官网http://redis.io/ 下载最新的稳定版本,这里是3.2.0  2. sudu mv 到 /usr/local/  3. sudo tar -zxf redis-3.2.0.tar 解压文件  4. 进入解压后的目录 cd redis-3.2.0  5. sudo make test 测试编译  6. sudo make...

未加载的程序集

错误一个接着一个 蹂躏着我脆弱的心灵。。 未处理的“System.IO.FileNotFoundException”类型的异常出现在Microsoft.Xna.Framework.Game.dll中 其他信息:未能加载文件或程序集“Microsoft.Xna.Framework,Version=2.0.0.0,Culture=neutral,Publick...

【Oracle】Oracle安装配置、创建数据库实例及用户和连接

https://blog.csdn.net/wudiyong22/article/details/78904361 参考资料:https://www.cnblogs.com/hoobey/p/6010804.html 一、Oracle下载 注意Oracle分成两个文件,下载完后,将两个文件解压到同一目录下即可。 路径名称中,最好不要出现中文,也不要出现空格...

提取Windows下的微信提示音

安卓版的微信没有像QQ那样自带音效,而Windows版本微信有,故想从windows版微信里提取微信通知音,提高手机通知音的区分度。 方法很简单,从官网下载Windows微信安装程序后,解压得到WeChatResource.dll文件,然后继续解压该文件,在.rsrc/1033/WAVE下就可以得到微信声音资源文件。复制到其他地方增加文件名后缀.wav即可...

Django流程-以登录功能为例

Django流程-以登录功能为例一、注意点 1、新创建的app一定要先去settings.py注册 简写:'app01' 完整:'app01.apps.App01Config' 2、启动Django项目的时候,一定要确保一个端口号只有一个Django项目占用,不然的话,会容易造成bug(修改代码后刷新页面没有效果) 3、用户可以访问的资源,都在url中,只...

转:使用xhprof进行线上PHP性能追踪及分析

原文来自于:http://avnpc.com/pages/profiler-php-performance-online-by-xhprof 原创作者:AlloVince 之前一直使用基于Xdebug进行PHP的性能分析,对于本地开发环境来说是够用了,但如果是线上环境的话,xdebug消耗较大,配置也不够灵活,因此线上环境建议使用xhprof进行PHP性能...