|
@@ -2,9 +2,14 @@ package com.ruoyi.project.officeConvert;
|
|
|
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
import com.ruoyi.common.constant.Constants;
|
|
import com.ruoyi.common.constant.HttpStatus;
|
|
import com.ruoyi.common.constant.HttpStatus;
|
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
|
+import com.ruoyi.framework.config.RuoYiConfig;
|
|
|
|
+import com.ruoyi.framework.web.controller.BaseController;
|
|
import com.ruoyi.framework.web.domain.AjaxResult;
|
|
import com.ruoyi.framework.web.domain.AjaxResult;
|
|
|
|
|
|
import org.jodconverter.DocumentConverter;
|
|
import org.jodconverter.DocumentConverter;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.bind.annotation.*;
|
|
@@ -15,6 +20,7 @@ import javax.annotation.Resource;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -38,8 +44,7 @@ public class OfficeConvertController {
|
|
@Resource
|
|
@Resource
|
|
DocumentConverter documentConverter;
|
|
DocumentConverter documentConverter;
|
|
|
|
|
|
- @Value("${ruoyi.profile}")
|
|
|
|
- private String profile;
|
|
|
|
|
|
+ protected final Logger logger = LoggerFactory.getLogger(BaseController.class);
|
|
|
|
|
|
private static String successMsg="请求成功!请预览文件!";
|
|
private static String successMsg="请求成功!请预览文件!";
|
|
|
|
|
|
@@ -57,7 +62,7 @@ public class OfficeConvertController {
|
|
|
|
|
|
String newFilePath=filepath.replace("/profile","");
|
|
String newFilePath=filepath.replace("/profile","");
|
|
// 需要转换的文件路径
|
|
// 需要转换的文件路径
|
|
- File file = new File(profile+newFilePath);
|
|
|
|
|
|
+ File file = new File(RuoYiConfig.getProfile()+newFilePath);
|
|
boolean flag= file.exists();
|
|
boolean flag= file.exists();
|
|
if(flag==false){
|
|
if(flag==false){
|
|
return new AjaxResult(HttpStatus.SUCCESS,fileMiss,Constants.RESOURCE_PREFIX+ "/" + newFilePath);
|
|
return new AjaxResult(HttpStatus.SUCCESS,fileMiss,Constants.RESOURCE_PREFIX+ "/" + newFilePath);
|
|
@@ -66,7 +71,7 @@ public class OfficeConvertController {
|
|
|
|
|
|
try {
|
|
try {
|
|
// 转换之后文件生成的本地地址
|
|
// 转换之后文件生成的本地地址
|
|
- File newFile = new File(profile);
|
|
|
|
|
|
+ File newFile = new File(RuoYiConfig.getProfile());
|
|
if (!newFile.exists()) {
|
|
if (!newFile.exists()) {
|
|
newFile.mkdirs();
|
|
newFile.mkdirs();
|
|
}
|
|
}
|
|
@@ -75,7 +80,7 @@ public class OfficeConvertController {
|
|
String newName=name.substring(0,t);
|
|
String newName=name.substring(0,t);
|
|
//根据文件类型判断转为pdf还是html,主要是预览效果问题
|
|
//根据文件类型判断转为pdf还是html,主要是预览效果问题
|
|
if(newFilePath.endsWith("docx")||newFilePath.endsWith("jpg")||newFilePath.endsWith("png")
|
|
if(newFilePath.endsWith("docx")||newFilePath.endsWith("jpg")||newFilePath.endsWith("png")
|
|
- ||newFilePath.endsWith(".tif")||newFilePath.endsWith(".rar")){
|
|
|
|
|
|
+ ||newFilePath.endsWith(".tif")||newFilePath.endsWith(".jpeg")){
|
|
converterPdf = newName+".pdf";
|
|
converterPdf = newName+".pdf";
|
|
}else if(newFilePath.endsWith(".csv")){
|
|
}else if(newFilePath.endsWith(".csv")){
|
|
converterPdf = newName+".html";
|
|
converterPdf = newName+".html";
|
|
@@ -88,25 +93,22 @@ public class OfficeConvertController {
|
|
newFilePath.endsWith("3gp")||newFilePath.endsWith("mkv")||newFilePath.endsWith(".dwg")) {
|
|
newFilePath.endsWith("3gp")||newFilePath.endsWith("mkv")||newFilePath.endsWith(".dwg")) {
|
|
//直接返回源文件路径
|
|
//直接返回源文件路径
|
|
return new AjaxResult(HttpStatus.SUCCESS,failMsg,Constants.RESOURCE_PREFIX+ "/" + newFilePath);
|
|
return new AjaxResult(HttpStatus.SUCCESS,failMsg,Constants.RESOURCE_PREFIX+ "/" + newFilePath);
|
|
- //原本是将其他格式转换为.mp4
|
|
|
|
- /* String codcFilePath= exchangeToMp4("D:\\ffmpeg\\ffmpeg-2022-06-12-git-4d45f5acbd-essentials_build\\ffmpeg-2022-06-12-git-4d45f5acbd-essentials_build\\bin\\ffmpeg.exe",
|
|
|
|
- profile+newFilePath,profile + "\\" +converterPdf);
|
|
|
|
- documentConverter.convert(new File((codcFilePath))).to(new File(profile + "\\" +converterPdf)).execute();*/
|
|
|
|
}else {
|
|
}else {
|
|
converterPdf = newName+".html";
|
|
converterPdf = newName+".html";
|
|
}
|
|
}
|
|
-
|
|
|
|
// 文件转化
|
|
// 文件转化
|
|
|
|
+ documentConverter.convert(file).to(new File(RuoYiConfig.getProfile() + File.separator +"temp" + File.separator +converterPdf)).execute();
|
|
|
|
|
|
- documentConverter.convert(file).to(new File(profile + "\\" +converterPdf)).execute();
|
|
|
|
-
|
|
|
|
|
|
+ this.doActionConvertedFile(RuoYiConfig.getProfile() + File.separator +"temp" + File.separator +converterPdf);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
|
|
//最后应该返回转换后的文件名称
|
|
//最后应该返回转换后的文件名称
|
|
|
|
|
|
- String pathFileName = Constants.RESOURCE_PREFIX+ "/" + converterPdf;
|
|
|
|
|
|
+ String pathFileName = Constants.RESOURCE_PREFIX+ File.separator +"temp" + File.separator + converterPdf;
|
|
|
|
+
|
|
|
|
+
|
|
return newFilePath.endsWith(".csv")? new AjaxResult(HttpStatus.SUCCESS,codeMiss,pathFileName): new AjaxResult(HttpStatus.SUCCESS,successMsg,pathFileName) ;
|
|
return newFilePath.endsWith(".csv")? new AjaxResult(HttpStatus.SUCCESS,codeMiss,pathFileName): new AjaxResult(HttpStatus.SUCCESS,successMsg,pathFileName) ;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -115,7 +117,7 @@ public class OfficeConvertController {
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
Map<String, Object> resultMap = new HashMap<>();
|
|
String newFilePath=filepath.replace("/profile","");
|
|
String newFilePath=filepath.replace("/profile","");
|
|
// 需要转换的PPT文件路径
|
|
// 需要转换的PPT文件路径
|
|
- File file = new File(profile+newFilePath);
|
|
|
|
|
|
+ File file = new File(RuoYiConfig.getProfile()+newFilePath);
|
|
//判断ppt,pptx文件是否存在本地
|
|
//判断ppt,pptx文件是否存在本地
|
|
boolean flag= file.exists();
|
|
boolean flag= file.exists();
|
|
if(flag==false){
|
|
if(flag==false){
|
|
@@ -123,7 +125,7 @@ public class OfficeConvertController {
|
|
}
|
|
}
|
|
|
|
|
|
// String imagePath = Constants.RESOURCE_PREFIX+ "/" + ;
|
|
// String imagePath = Constants.RESOURCE_PREFIX+ "/" + ;
|
|
- File imageFile = new File(profile);
|
|
|
|
|
|
+ File imageFile = new File(RuoYiConfig.getProfile() + File.separator + "temp");
|
|
if (!imageFile.exists()){
|
|
if (!imageFile.exists()){
|
|
imageFile.mkdirs();
|
|
imageFile.mkdirs();
|
|
} //判断生成文件路径是否已存在
|
|
} //判断生成文件路径是否已存在
|
|
@@ -143,4 +145,44 @@ public class OfficeConvertController {
|
|
return new AjaxResult(HttpStatus.SUCCESS,successMsg,resultMap);
|
|
return new AjaxResult(HttpStatus.SUCCESS,successMsg,resultMap);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private static final String getPathFileName(String uploadDir, String fileName) throws IOException
|
|
|
|
+ {
|
|
|
|
+ int dirLastIndex = RuoYiConfig.getProfile().length() + 1;
|
|
|
|
+ String currentDir = StringUtils.substring(uploadDir, dirLastIndex);
|
|
|
|
+ String pathFileName = Constants.RESOURCE_PREFIX + "/" + currentDir + "/" + fileName;
|
|
|
|
+ return pathFileName;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 对转换后的文件进行操作(改变编码方式)
|
|
|
|
+ *
|
|
|
|
+ * @param outFilePath 文件绝对路径
|
|
|
|
+ */
|
|
|
|
+ public void doActionConvertedFile(String outFilePath) {
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ try (InputStream inputStream = new FileInputStream(outFilePath);
|
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, System.getProperty("sun.jnu.encoding")))) {
|
|
|
|
+ String line;
|
|
|
|
+ while (null != (line = reader.readLine())) {
|
|
|
|
+ if (line.contains("charset=gb2312")) {
|
|
|
|
+ line = line.replace("charset=gb2312", "charset=utf-8");
|
|
|
|
+ }
|
|
|
|
+ sb.append(line);
|
|
|
|
+ }
|
|
|
|
+ // 添加sheet控制头
|
|
|
|
+ sb.append("<script src=\"js/jquery-3.0.0.min.js\" type=\"text/javascript\"></script>");
|
|
|
|
+ sb.append("<script src=\"js/excel.header.js\" type=\"text/javascript\"></script>");
|
|
|
|
+ sb.append("<link rel=\"stylesheet\" href=\"bootstrap/css/bootstrap.min.css\">");
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ // 重新写入文件
|
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(outFilePath);
|
|
|
|
+ BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8))) {
|
|
|
|
+ writer.write(sb.toString());
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|