关于openoffice转换pdf中txt乱码的问题

什么文件类型都好使,docx,doc,ppt,xls,xlsx,就踏马txt不行,各种乱码
气得我直接把txt统一转换成utf-8格式了,
看源码这个import org.jodconverter.DocumentConverter;里面写的没有设置编码类型的代码,也没法设置,找一下午找不见

@Autowired
    private DocumentConverter converter;

EncodingDetect这个类是我的检测文件编码的类

if("txt".equals(fileType)){
            String substring = UUID.randomUUID().toString().substring(7);
            File fileCon = new File(rootPath + "/"  + fileName);
            try {
                //转换之后文件生成的地址
                File newFile = new File(rootPath);
                if (!newFile.exists()) {
                    newFile.mkdirs();
                }
                File file2 = new File(rootPath + "/"+substring+"/");
                if(!file2.exists()){
                    file2.mkdirs();
                }


                String inputFileUrl = rootPath + "/" + fileName;
                String outputFileUrl = rootPath + "/" + substring + "/" + fileName + ".odt";
                String inputFileEncode = null;
                try {
                    inputFileEncode = EncodingDetect.getJavaEncode(inputFileUrl);
                } catch (Exception e) {
                    return "文件内容为空";
                }
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(new FileInputStream(inputFileUrl), inputFileEncode));
                BufferedWriter bufferedWriter = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(outputFileUrl), "GBK"));
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    bufferedWriter.write(line + "\r\n");
                }
                bufferedWriter.close();
                bufferedReader.close();

                File file1 = new File(outputFileUrl);
                converter.convert(file1).to(new File(rootPath + "/"+substring+"/" + fileName + ".odt.pdf")).execute();
                //使用response,将pdf文件以流的方式发送的前段
                ServletOutputStream outputStream = response.getOutputStream();
                InputStream in = new FileInputStream(new File(rootPath + "/"+substring+"/" + fileName +".odt.pdf"));// 读取文件

                response.setContentType("application/force-download;text/html;charset=ISO8859-1");// 设置强制下载不打开
                String a = "attachment;filename=" +fileName+ ".pdf";
                String gbk = new String(a.getBytes("GBK"), "ISO8859-1");
                response.addHeader("Content-Disposition",gbk);

                DownloadUtils.downLoadFile(response,fileName+ ".pdf",new File(rootPath + "/"+substring+"/" + fileName +".odt.pdf"));
                in.close();
                outputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值