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.SocketOpenOfficeConnection;
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_log;
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.entity.Record;
import org.nutz.ioc.IocException;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
@@ -40,6 +42,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -57,6 +61,9 @@ import java.util.List;
@Slf4j
public class FileServerController {
@Inject
private PropertiesProxy conf;
private static final List<String> IMGTYPES = new ArrayList<>() {{
add("JPG");
add("JPEG");
@@ -137,12 +144,17 @@ public class FileServerController {
response.setContentType("application/pdf");
ftpService.download(filepath, response.getOutputStream());
} 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");
String pdfPath = generatePDF(filepath, id + ".pdf");
assert pdfPath != null;
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 {
response.setHeader("Content-Disposition", "attachment;filename=" + new String(fullFileName.getBytes("utf-8"), "ISO8859-1"));
ftpService.download(filepath, response.getOutputStream());
@@ -167,21 +179,20 @@ public class FileServerController {
*/
private String generatePDF(String sourcePath, String targetPath) {
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);
if (!TEMPDIRECTORYFile.exists()) {
TEMPDIRECTORYFile.mkdir();
}
//要转换的pdf本地临时路径
Path localPdfPath = Files.createTempFile(null, ".pdf");
this.convert(localSourcePath.toFile(), localPdfPath.toFile());
File tempSourceFile = new File("D:\\FTP\\" + sourcePath);
//FileOutputStream tempSourceFileOutPutStream = new FileOutputStream(tempSourceFile);
//ftpService.download(sourcePath, tempSourceFileOutPutStream);
File tempTargetFile = new File(TEMPDIRECTORY + targetPath);
this.convert(tempSourceFile, tempTargetFile);
return tempTargetFile.getCanonicalPath();
//删除临时文件(docx,xlsx)
Files.deleteIfExists(localSourcePath);
return localPdfPath.toString();
} catch (Exception e) {
e.printStackTrace();
@@ -199,13 +210,12 @@ public class FileServerController {
throw new NullPointerException();
}
if (targetFile.exists() && targetFile.length() == 0L) {
throw new NullPointerException();
}
OpenOfficeConnection connection = new SocketOpenOfficeConnection();
String localIp = StrUtil.blankToDefault(conf.get("v.ip"), "127.0.0.1");
OpenOfficeConnection connection = new SocketOpenOfficeConnection(localIp, 8100);
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
//此处如果是使用远程的openoffice服务 需要使用StreamOpenOfficeDocumentConverter
// DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
StreamOpenOfficeDocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(sourceFile, targetFile);
connection.disconnect();
} catch (IocException var4) {
@@ -342,14 +352,14 @@ public class FileServerController {
/**
* 下载
*
* @param filepath
* @param id
* @param filename
* @param response
*/
@At
@Ok("void")
@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<>();
try {
String loginName = (String) ShiroUtil.getPrincipalProperty("loginname");
@@ -361,9 +371,18 @@ public class FileServerController {
logMap.put("执行类", this.getClass().getName() + "#download");
logMap.put("来源IP", Lang.getIP(request));
logMap.put("执行时间", DateUtil.now());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("utf-8"), "ISO8859-1"));
ftpService.download(filepath, response.getOutputStream());
if (StrUtil.isBlank(filename)) {
Sys_file sysFile = dao.fetch(Sys_file.class, Cnd.where("id", "=", id).or("filepath", "=", id));
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("执行结果", true);
log.info(logMap.toString());
@@ -12,10 +12,14 @@
</template>
<template v-else>
<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>
</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>
@@ -692,6 +696,9 @@ module.exports = {
preview(filename, id)
}
},
downLoadFile({id, filename}) {
window.open('/file_server/download?id=' + id)
}
},
created() {
this.updateFileList(this.files)
@@ -721,11 +728,12 @@ module.exports = {
position: relative;
display: flex;
width: 100%;
max-height: 256px;
/*max-height: 256px;*/
flex-direction: row;
flex-wrap: wrap;
overflow-y: auto;
overflow-x: hidden;
/*overflow-y: auto;*/
/*overflow-x: hidden;*/
/*height: 150px;*/
}
.viewFileItem {
@@ -736,7 +744,7 @@ module.exports = {
transition: all 500ms;
border-radius: 5px;
cursor: pointer;
margin-bottom: 10px;
/*margin-bottom: 10px;*/
}
.viewFileItem:hover {