package com.lzy.project.common;
import com.lzy.common.constant.Constants;
import com.lzy.common.utils.StringUtils;
import com.lzy.common.utils.file.FileUploadUtils;
import com.lzy.common.utils.file.FileUtils;
import com.lzy.framework.config.LzyConfig;
import com.lzy.framework.config.ServerConfig;
import com.lzy.framework.web.domain.AjaxResult;
import com.lzy.project.knowledge.domain.OpeKnowledgeBase;
import com.lzy.project.knowledge.domain.vo.OpeKnowledgeBaseVO;
import com.lzy.project.knowledge.service.IOpeKnowledgeBaseService;
import com.lzy.project.operation.domain.FileInfo;
import com.lzy.project.operation.service.FileInfoService;
import com.lzy.project.tool.pdf.ConvertSwf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
/**
* 通用请求处理
*
* @author lzy
*/
@RestController
public class CommonController {
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
@Autowired
private ServerConfig serverConfig;
@Autowired
private FileInfoService fileInfoService;
@Autowired
private IOpeKnowledgeBaseService iOpeKnowledgeBaseService;
/**
* 通用下载请求
*
* @param fileName 文件名称
* @param delete 是否删除
*/
@GetMapping("common/download")
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response,
HttpServletRequest request) {
try {
if (!FileUtils.isValidFilename(fileName)) {
throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
}
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
String filePath = LzyConfig.getDownloadPath() + fileName;
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition",
"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
FileUtils.writeBytes(filePath, response.getOutputStream());
if (delete) {
FileUtils.deleteFile(filePath);
}
} catch (Exception e) {
log.error("下载文件失败", e);
}
}
/**
* 通用下载请求
*
* @param fileName 文件名称
*/
@RequestMapping("common/downloadFile")
@ResponseBody
public void downloadFile(String fileName,String path, HttpServletResponse response,HttpServletRequest request) {
try {
if (!FileUtils.isValidFilename(fileName)) {
throw new Exception(StringUtils.format("文件名称({})非法,不允许下载。 ", fileName));
}
path=path.substring(8,path.length());
String filePath = LzyConfig.getProfile() + path;
File file = new File(filePath);
if (!file.exists()) {
throw new Exception(StringUtils.format("文件不存在", fileName));
}
FileInputStream inputStream = new FileInputStream(file);
// 设置输出格式\
response.setCharacterEncoding("utf-8");
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment;filename=" +
FileUtils.setFileDownloadHeader(request, fileName));
// 循环取出流中的数据
// byte[] b = new byte[61858764];
// int len;
// while ((len = inputStream.read(b)) > 0) {
// response.getOutputStream().write(b, 0, len);
// }
// inputStream.close();
FileUtils.writeBytes(filePath, response.getOutputStream());
} catch (Exception e) {
log.error("下载文件失败", e);
}
}
/**
* 通用上传请求
*/
@PostMapping("/common/upload")
public AjaxResult uploadFile(MultipartFile file) throws Exception {
try {
// 上传文件路径
String filePath = LzyConfig.getUploadPath();
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, file);
String url = serverConfig.getUrl() + fileName;
AjaxResult ajax = AjaxResult.success();
ajax.put("fileName", fileName);
ajax.put("url", url);
return ajax;
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
}
}
/**
* 进行office文件转换
*/
@GetMapping("/common/convertFile/{fileId}")
public AjaxResult convertFile(@PathVariable("fileId") Long fileId) throws Exception {
try {
log.debug("CommonController.convertFile method enter, and param fileId={}", fileId);
if (null == fileId) {
return AjaxResult.error("参数错误,文件ID不能为空.");
}
FileInfo fileInfo = this.fileInfoService.selectFileInfoById(fileId);
if (null == fileInfo || StringUtils.isEmpty(fileInfo.getCllj())) {
return AjaxResult.error("文件不存在。");
}
log.debug("CommonController.convertFile fileInfo.getCllj={}", fileInfo.getCllj());
String filepath = LzyConfig.getProfile() + fileInfo.getCllj().replace("/profile", "");
log.debug("CommonController.convertFile filepath={}", filepath);
ConvertSwf.getInstance().beginConvert(filepath);
return AjaxResult.success("转换成功。");
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
}
}
/**
* 知识库文件预览时进行office文件转换
* 根据fileSource判断该条数据来自附件材料管理表还是知识库模块的表的数据
*/
@GetMapping("/common/convertKnowledgeFile")
public AjaxResult convertKnowledgeFile(OpeKnowledgeBaseVO opeKnowledgeBaseVO) throws Exception {
Long fileId = opeKnowledgeBaseVO.getId();
String fileSource = opeKnowledgeBaseVO.getFileSource();
String filePath = "";
try {
log.debug("CommonController.convertFile method enter, and param fileId={}", fileId);
if (null == fileId) {
return AjaxResult.error("参数错误,文件ID不能为空.");
}
if ("knowledge".equals(fileSource)) {
OpeKnowledgeBase opeKnowledgeBase = this.iOpeKnowledgeBaseService.selectOpeKnowledgeBaseById(fileId);
if (null == opeKnowledgeBase || StringUtils.isEmpty(opeKnowledgeBase.getFilePath())) {
return AjaxResult.error("文件不存在。");
}
log.debug("CommonController.convertKnowledgeFile opeKnowledgeBase.getFilePath={}",
opeKnowledgeBase.getFilePath());
filePath = LzyConfig.getProfile() + opeKnowledgeBase.getFilePath().replace("/profile", "");
} else if ("fjclgl".equals(fileSource)) {
FileInfo fileInfo = this.fileInfoService.selectFileInfoById(fileId);
if (null == fileInfo || StringUtils.isEmpty(fileInfo.getCllj())) {
return AjaxResult.error("文件不存在。");
}
log.debug("CommonController.convertKnowledgeFile fileInfo.getCllj={}", fileInfo.getCllj());
filePath = LzyConfig.getProfile() + fileInfo.getCllj().replace("/profile", "");
} else {
return AjaxResult.error("参数传递错误,无法处理,请联系管理员。");
}
log.debug("CommonController.convertKnowledgeFile filepath={}", filePath);
ConvertSwf.getInstance().beginConvert(filePath);
return AjaxResult.success("转换成功。");
} catch (Exception e) {
return AjaxResult.error(e.getMessage());
}
}
/**
* 本地资源通用下载
*/
@GetMapping("/common/download/resource")
public void resourceDownload(String name, HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 本地资源路径
String localPath = LzyConfig.getProfile();
// 数据库资源地址
String downloadPath = localPath + StringUtils.substringAfter(name, Constants.RESOURCE_PREFIX);
// 下载名称
String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition",
"attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
FileUtils.writeBytes(downloadPath, response.getOutputStream());
}
}
版权声明:本文为answero原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。