什么文件类型都好使,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();
}
}