This commit is contained in:
zhaoxinyu
2024-09-01 11:23:18 +08:00
parent 219ad671bc
commit 9d17e7fe2a
2 changed files with 59 additions and 32 deletions
@@ -8,6 +8,7 @@ import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection; import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import io.v.nutz.sys.models.Sys_file; import io.v.nutz.sys.models.Sys_file;
import io.v.nutz.sys.models.Sys_log; import io.v.nutz.sys.models.Sys_log;
import io.v.nutz.sys.models.Sys_user; import io.v.nutz.sys.models.Sys_user;
@@ -22,6 +23,7 @@ import org.nutz.dao.Cnd;
import org.nutz.dao.Dao; import org.nutz.dao.Dao;
import org.nutz.dao.entity.Record; import org.nutz.dao.entity.Record;
import org.nutz.ioc.IocException; import org.nutz.ioc.IocException;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject; import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean; import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json; import org.nutz.json.Json;
@@ -40,6 +42,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.*; import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@@ -57,6 +61,9 @@ import java.util.List;
@Slf4j @Slf4j
public class FileServerController { public class FileServerController {
@Inject
private PropertiesProxy conf;
private static final List<String> IMGTYPES = new ArrayList<>() {{ private static final List<String> IMGTYPES = new ArrayList<>() {{
add("JPG"); add("JPG");
add("JPEG"); add("JPEG");
@@ -137,12 +144,17 @@ public class FileServerController {
response.setContentType("application/pdf"); response.setContentType("application/pdf");
ftpService.download(filepath, response.getOutputStream()); ftpService.download(filepath, response.getOutputStream());
} else if (OFFICETYPES.contains(suffix)) { } else if (OFFICETYPES.contains(suffix)) {
response.setHeader("Content-Disposition", "attachment;filename=" + new String((prefix + ".pdf").getBytes("utf-8"), "ISO8859-1")); /* response.setHeader("Content-Disposition", "attachment;filename=" + new String((prefix + ".pdf").getBytes("utf-8"), "ISO8859-1"));
response.setContentType("application/pdf"); response.setContentType("application/pdf");
String pdfPath = generatePDF(filepath, id + ".pdf"); String pdfPath = generatePDF(filepath, id + ".pdf");
assert pdfPath != null; assert pdfPath != null;
FileCopyUtils.copy(new FileInputStream(pdfPath), response.getOutputStream()); FileCopyUtils.copy(new FileInputStream(pdfPath), response.getOutputStream());
//ftpService.download(filepath, response.getOutputStream()); //ftpService.download(filepath, response.getOutputStream());*/
response.setHeader("Content-Disposition", "attachment;filename=" + new String((prefix + ".pdf").getBytes("utf-8"), "ISO8859-1"));
response.setContentType("application/pdf");
String pdfPath = generatePDF(filepath, id + ".pdf");
FileCopyUtils.copy(new FileInputStream(pdfPath), response.getOutputStream());
Files.delete(Path.of(pdfPath));
} else { } else {
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fullFileName.getBytes("utf-8"), "ISO8859-1")); response.setHeader("Content-Disposition", "attachment;filename=" + new String(fullFileName.getBytes("utf-8"), "ISO8859-1"));
ftpService.download(filepath, response.getOutputStream()); ftpService.download(filepath, response.getOutputStream());
@@ -167,21 +179,20 @@ public class FileServerController {
*/ */
private String generatePDF(String sourcePath, String targetPath) { private String generatePDF(String sourcePath, String targetPath) {
try { try {
//文件扩展名
String extName = FileUtil.extName(sourcePath);
//将ftp文件下载到本地的临时文件路径
Path localSourcePath = Files.createTempFile(null, "." + extName);
FileOutputStream localOutPutStream = new FileOutputStream(localSourcePath.toFile());
ftpService.download(sourcePath, localOutPutStream);
File TEMPDIRECTORYFile = new File(TEMPDIRECTORY); //要转换的pdf本地临时路径
if (!TEMPDIRECTORYFile.exists()) { Path localPdfPath = Files.createTempFile(null, ".pdf");
TEMPDIRECTORYFile.mkdir(); this.convert(localSourcePath.toFile(), localPdfPath.toFile());
}
File tempSourceFile = new File("D:\\FTP\\" + sourcePath); //删除临时文件(docx,xlsx)
Files.deleteIfExists(localSourcePath);
//FileOutputStream tempSourceFileOutPutStream = new FileOutputStream(tempSourceFile); return localPdfPath.toString();
//ftpService.download(sourcePath, tempSourceFileOutPutStream);
File tempTargetFile = new File(TEMPDIRECTORY + targetPath);
this.convert(tempSourceFile, tempTargetFile);
return tempTargetFile.getCanonicalPath();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@@ -199,13 +210,12 @@ public class FileServerController {
throw new NullPointerException(); throw new NullPointerException();
} }
if (targetFile.exists() && targetFile.length() == 0L) { String localIp = StrUtil.blankToDefault(conf.get("v.ip"), "127.0.0.1");
throw new NullPointerException(); OpenOfficeConnection connection = new SocketOpenOfficeConnection(localIp, 8100);
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection();
connection.connect(); connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection); //此处如果是使用远程的openoffice服务 需要使用StreamOpenOfficeDocumentConverter
// DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
StreamOpenOfficeDocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(sourceFile, targetFile); converter.convert(sourceFile, targetFile);
connection.disconnect(); connection.disconnect();
} catch (IocException var4) { } catch (IocException var4) {
@@ -342,14 +352,14 @@ public class FileServerController {
/** /**
* 下载 * 下载
* *
* @param filepath * @param id
* @param filename * @param filename
* @param response * @param response
*/ */
@At @At
@Ok("void") @Ok("void")
@RequiresAuthentication @RequiresAuthentication
public void download(String filepath, String filename, HttpServletResponse response, HttpServletRequest request) { public void download(String id, String filename, HttpServletResponse response, HttpServletRequest request) {
HashMap<String, Object> logMap = new HashMap<>(); HashMap<String, Object> logMap = new HashMap<>();
try { try {
String loginName = (String) ShiroUtil.getPrincipalProperty("loginname"); String loginName = (String) ShiroUtil.getPrincipalProperty("loginname");
@@ -361,9 +371,18 @@ public class FileServerController {
logMap.put("执行类", this.getClass().getName() + "#download"); logMap.put("执行类", this.getClass().getName() + "#download");
logMap.put("来源IP", Lang.getIP(request)); logMap.put("来源IP", Lang.getIP(request));
logMap.put("执行时间", DateUtil.now()); logMap.put("执行时间", DateUtil.now());
response.setContentType("application/octet-stream"); if (StrUtil.isBlank(filename)) {
response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO8859-1")); Sys_file sysFile = dao.fetch(Sys_file.class, Cnd.where("id", "=", id).or("filepath", "=", id));
ftpService.download(filepath, response.getOutputStream()); if (null == sysFile) {
log.error("下载文件不存在:id={}", id);
} else {
filename = sysFile.getFilename();
String filepath = sysFile.getFilepath();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO8859-1"));
ftpService.download(filepath, response.getOutputStream());
}
}
logMap.put("请求结果", Result.success().toJsonString()); logMap.put("请求结果", Result.success().toJsonString());
logMap.put("执行结果", true); logMap.put("执行结果", true);
log.info(logMap.toString()); log.info(logMap.toString());
@@ -12,10 +12,14 @@
</template> </template>
<template v-else> <template v-else>
<svg aria-hidden="true"> <svg aria-hidden="true">
<use :xlink:href="APP_DOMAIN + '/file_server/fileStreamPreview?id=' + file.id"></use> <!-- <use :xlink:href="APP_DOMAIN + '/file_server/fileStreamPreview?id=' + file.id"></use>-->
<use
:xlink:href="wpUploadFileShowResolving.getFileItemShowIcon(resolvingShowByFileName(file.filename), file.filepath, true)"></use>
</svg> </svg>
</template> </template>
<div :title="file.filename" class="viewFileName">{{ file.filename }}</div> <div :title="file.filename" class="viewFileName" title="点击下载" @click.stop="downLoadFile(file)">
<i class="el-icon-download"></i> {{file.filename }}
</div>
</div> </div>
</div> </div>
@@ -692,6 +696,9 @@ module.exports = {
preview(filename, id) preview(filename, id)
} }
}, },
downLoadFile({id, filename}) {
window.open('/file_server/download?id=' + id)
}
}, },
created() { created() {
this.updateFileList(this.files) this.updateFileList(this.files)
@@ -721,11 +728,12 @@ module.exports = {
position: relative; position: relative;
display: flex; display: flex;
width: 100%; width: 100%;
max-height: 256px; /*max-height: 256px;*/
flex-direction: row; flex-direction: row;
flex-wrap: wrap; flex-wrap: wrap;
overflow-y: auto; /*overflow-y: auto;*/
overflow-x: hidden; /*overflow-x: hidden;*/
/*height: 150px;*/
} }
.viewFileItem { .viewFileItem {
@@ -736,7 +744,7 @@ module.exports = {
transition: all 500ms; transition: all 500ms;
border-radius: 5px; border-radius: 5px;
cursor: pointer; cursor: pointer;
margin-bottom: 10px; /*margin-bottom: 10px;*/
} }
.viewFileItem:hover { .viewFileItem:hover {