package com.ots.project.common;
|
|
import com.ots.common.utils.file.FileUploadUtils;
|
import com.ots.common.utils.file.FileUtils;
|
import com.ots.framework.config.EssConfig;
|
import com.ots.framework.config.ServerConfig;
|
import com.ots.framework.web.domain.AjaxResult;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
|
/**
|
* 通用请求处理
|
*
|
* @author ots
|
*/
|
@Controller
|
public class CommonController {
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
|
|
@Autowired
|
private ServerConfig serverConfig;
|
|
/**
|
* 通用下载请求
|
*
|
* @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 = EssConfig.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);
|
}
|
}
|
|
/**
|
* 通用上传请求
|
*/
|
@PostMapping("/common/upload")
|
@ResponseBody
|
public AjaxResult uploadFile(MultipartFile file) throws Exception {
|
try {
|
// 上传文件路径
|
String filePath = EssConfig.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());
|
}
|
}
|
/**
|
* 上传图片服务器
|
* /common/images 这里请求的连接是images,你的文件夹也要叫images
|
*/
|
@PostMapping("/common/images")
|
@ResponseBody
|
public AjaxResult upImage(MultipartFile file) throws Exception {
|
try {
|
// 上传文件路径
|
String filePath = EssConfig.getUploadImagePath();
|
// 上传并返回新文件名称
|
String fileName = FileUploadUtils.upload(filePath, file);
|
//String url = serverConfig.getUrl() + fileName;
|
String url = fileName.replaceFirst("/profile/images", EssConfig.getImagePath());
|
|
//1、先压缩出一个新的
|
// filePath = "/Users/shawnli/Downloads/images"
|
// fileName = "/profile/images/2019/12/19/aba5e2f850edf254a9e5b2a35c50089a.png"
|
// // url = "http://localhost:8090/profile/images/2019/12/19/aba5e2f850edf254a9e5b2a35c50089a.png"
|
// String filePathOriginal = fileName.replaceFirst("/profile/images", filePath);
|
// String fileNew = filePathOriginal + "."+filePathOriginal.substring(filePathOriginal.lastIndexOf(".") + 1);
|
// PicUtils.commpressPicForScale(filePathOriginal, fileNew, 20, 0.5);
|
//
|
// //2、删除旧的
|
// File fileOper = new File(filePathOriginal);
|
// fileOper.delete();
|
//
|
// //3、命名新的为旧的名字
|
// File fileNewOper = new File(fileNew);
|
// fileNewOper.renameTo(new File(filePathOriginal));
|
|
AjaxResult ajax = AjaxResult.success();
|
ajax.put("fileName", fileName);
|
ajax.put("url", url);
|
return ajax;
|
} catch (Exception e) {
|
return AjaxResult.error(e.getMessage());
|
}
|
}
|
}
|