import cn.lonwin.rcs.rcscommon.api.ApiResponse;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.regex.Pattern;
@Api(value = "調用openoffice展示文檔Controller", tags = {"調用openoffice展示文檔"})
@RestController
public class ShowController {
@RequestMapping(value = "/sys/attach/preview")
public ApiResponse preview(HttpServletRequest request, HttpServletResponse response, @RequestParam String filePath, @RequestParam String fileName) throws IOException {
// ResponseData responseData = new ResponseData();
response.setContentType("text/html; charset=UTF-8");
if(!"".equals(filePath)) {
/*1)根據項目所在的服務器環境,確定路徑中的 / 和 \ */
String osName = System.getProperty("os.name");
if (Pattern.matches("Linux.*", osName)) {
filePath = "/" + filePath.replace("\\","/");
} else if(Pattern.matches("Windows.*", osName)) {
filePath.replace("/","\\");
}
/*2)獲得文件名後綴*/
String ext = "";
if(!"".equals(fileName) && fileName.contains(".")){
ext = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toUpperCase();
}
/*3)根據文件類型不同進行預覽*/
/*預覽圖片*/
if ("PNG".equals(ext) || "JPEG".equals(ext) || "JPG".equals(ext)) {
response.setContentType("image/jpeg");
}
/*預覽BMP格式的文件*/
if ("BMP".equals(ext)) {
response.setContentType("image/bmp");
}
/*預覽pdf*/
if ("PDF".equals(ext)) {
response.setContentType("application/pdf");
}
/*利用openOffice將office文件轉換爲pdf格式, 然後預覽doc, docx, xls, xlsx, ppt, pptx */
if ("DOC".equals(ext) || "DOCX".equals(ext) || "XLS".equals(ext) || "XLSX".equals(ext) || "PPT".equals(ext) || "PPTX".equals(ext)) {
/*filePath在數據庫中是不帶文件後綴的, 由於jodConverter必須要識別後綴,所以將服務器中的文件重命名爲帶後綴的文件*/
File docFile = new File(filePath);
/*File docFileWithExt = new File(filePath + "." + ext.toLowerCase()); //帶後綴的文件
docFile.renameTo(docFileWithExt);
*/
/*轉換之後的文件名*/
File pdfFile;
if(filePath.contains(".")){
pdfFile = new File(filePath.substring(0, filePath.lastIndexOf(".")) + ".pdf");
}else{
pdfFile = new File(filePath + ".pdf");
}
/*判斷即將要轉換的文件是否真實存在*/
if (docFile.exists()) {
/*判斷改文件是否已經被轉換過,若已經轉換則直接預覽*/
if (!pdfFile.exists()) {
/*打開OpenOffice連接,*/
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docFile, pdfFile);
connection.disconnect();
//文件轉換之後的路徑
filePath = pdfFile.getPath();
response.setContentType("application/pdf");
} catch (java.net.ConnectException e) {
e.printStackTrace(); //openoffice 服務未啓動
throw e;
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
e.printStackTrace(); //讀取轉換文件失敗
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally { //發生exception時, connection不會自動切斷, 程序會一直掛着
try{
if(connection != null){
connection.disconnect();
connection = null;
}
}catch(Exception e){}
}
} else {
//文件已經轉換過
filePath = pdfFile.getPath();
response.setContentType("application/pdf");
}
} else {
return ApiResponse.error("需要預覽的文檔在服務器中不存在!");
}
}
/*將文件寫入輸出流,顯示在界面上,實現預覽效果*/
FileInputStream fis = new FileInputStream(filePath);
OutputStream os = response.getOutputStream();
try {
int count = 0;
byte[] buffer = new byte[1024 * 1024];
while ((count = fis.read(buffer)) != -1){
os.write(buffer, 0, count);
}
os.flush();
return ApiResponse.ok("success",null);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null){
os.close();
}
if (fis != null){
fis.close();
}
}
}
return ApiResponse.ok("success",null);
}
}
註釋 :maven .pom
**<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.10</version>
</dependency>**
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
前端調用接口展示
var url = config.apiUrl+"/sys/attach/preview?filePath="+filePath+"&fileName="+fileName;
filepath:爲資源在服務器存放的路徑
filename爲資源的文件名