在线编辑下载docx文件接口

This commit is contained in:
wangzhijiang
2024-03-14 14:20:56 +08:00
parent edb972ab44
commit bec02629f4
@@ -21,6 +21,9 @@ import com.jero.modules.onlyoffice.service.IBusProcessModelHisService;
import com.jero.modules.onlyoffice.service.LawsUserInfoService;
import com.jero.modules.oss.entity.OSSFile;
import com.jero.modules.oss.service.IOSSFileService;
import com.spire.doc.Document;
import com.spire.doc.documents.PageNumberStyle;
import com.spire.doc.documents.PageOrientation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
@@ -646,5 +649,59 @@ public class OnlyOfficeController {
}
return result;
}
@ApiOperation(value = "|File|在线编辑下载docx文件")
@GetMapping("/onlyOfficeDownloadDocxFile")
public void onlyOfficeDownloadDocxFile(String fileId, HttpServletResponse response, HttpServletRequest request) {
OSSFile ossFile = ossFileService.getById(fileId);
if (ObjectUtils.isNotEmpty(ossFile)) {
InputStream is = null;
OutputStream os = null;
response.reset();
try {
String fileOldName = fileNameEncoding(ossFile.getFileName(), request);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileOldName + "\"");
response.setContentType("application/octet-stream");
// 加载文档
Document doc = new Document(filePath + ossFile.getUrl());
doc.getSections().get(0).getPageSetup().setPageNumberStyle(PageNumberStyle.Roman_Upper);
doc.getSections().get(0).getPageSetup().setOrientation(PageOrientation.Portrait);//纵向
doc.getSections().get(1).getPageSetup().setOrientation(PageOrientation.Portrait);//纵向
String downloadDocxFileTempUrl = filePath + "/downloadDocxFileTemp";
File downloadDocxFileTempUrlFile = new File(downloadDocxFileTempUrl);
if (!downloadDocxFileTempUrlFile.exists()) {
downloadDocxFileTempUrlFile.mkdirs();
}
String newFileName = UUID.randomUUID().toString().replace("-", "") + ".docx";
String newFileUrl = downloadDocxFileTempUrl + "/" + newFileName;
//保存文档
doc.saveToFile(newFileUrl);
File newFile = new File(newFileUrl);
is = new FileInputStream(newFile);
os = response.getOutputStream();
IOUtils.copy(is, os);
os.flush();
is.close();
if (newFile.delete()) {
logger.info("在线编辑下载-临时文件删除成功: " + newFileUrl);
} else {
logger.info("在线编辑下载-临时文件删除失败: " + newFileUrl);
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
}
}
}