|
@@ -1,287 +0,0 @@
|
|
|
-package cn.aiyangniu.api.controller.other;
|
|
|
-
|
|
|
-import cn.aiyangniu.api.common.util.AuthUtil;
|
|
|
-import cn.aiyangniu.api.common.util.CharacterFiltUtil;
|
|
|
-import org.apache.commons.io.FileUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.http.HttpHeaders;
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
-import org.springframework.http.MediaType;
|
|
|
-import org.springframework.http.ResponseEntity;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
-import javax.annotation.Resource;
|
|
|
-import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.*;
|
|
|
-
|
|
|
-/**
|
|
|
- * 文件上传业务接口类
|
|
|
- *
|
|
|
- * @author Henry Hall
|
|
|
- * @since 2019-10-22
|
|
|
- */
|
|
|
-@RestController
|
|
|
-@RequestMapping("/upload")
|
|
|
-public class UploadController {
|
|
|
-
|
|
|
- @Value("${file-path}")
|
|
|
- private String filePath;
|
|
|
-
|
|
|
- @Value("${root-path}")
|
|
|
- private String rootPath;
|
|
|
-
|
|
|
- @Resource
|
|
|
- private AuthUtil authUtil;
|
|
|
-
|
|
|
- /**
|
|
|
- * singleUpload 单文件上传
|
|
|
- */
|
|
|
- @RequestMapping(value="/single", method=RequestMethod.POST)
|
|
|
- public Map<String, Object> singleUpload(@RequestParam("upFile") MultipartFile upFile, HttpServletRequest req) {
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- Map<String, String> fileMap = new HashMap<>();
|
|
|
- String retCode="", retMsg="", token, savePath, visitPath, oldFileName, newFileName, extName, allowType;
|
|
|
- token = req.getHeader("Authorization");
|
|
|
- if(token == null || "null".equals(token)) {
|
|
|
- retCode = "1002";
|
|
|
- retMsg = "对不起,您的操作非法,请先登录后操作!";
|
|
|
- } else {
|
|
|
- boolean auth = authUtil.hasLogin(token);
|
|
|
- if(!auth) {
|
|
|
- retCode = "1002";
|
|
|
- retMsg = "对不起,您没有登录或会话超时,请重新登录!";
|
|
|
- } else {
|
|
|
- if(upFile == null) {
|
|
|
- retCode = "1000";
|
|
|
- retMsg = "对不起,您缺少必要的请求参数,请重试!";
|
|
|
- } else {
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
|
|
|
- String datetime = sdf.format(new Date());
|
|
|
- //文件存放的目录
|
|
|
- File folder;
|
|
|
- try {
|
|
|
- oldFileName = upFile.getOriginalFilename();
|
|
|
- assert oldFileName != null;
|
|
|
- extName = oldFileName.substring(oldFileName.lastIndexOf(".")).toLowerCase();
|
|
|
- allowType = ".jpg.jpeg.gif.png.bmp.flv.swf.mp3.mp4.wav.wma.wmv.mid.avi.mpg.asf.rm.rmvb.doc.docx.xls.xlsx.ppt.pptx.txt.pdf.rar.zip.gz.bz2";
|
|
|
- if(!allowType.contains(extName)) {
|
|
|
- retCode = "1004";
|
|
|
- retMsg = "对不起,您选择上传的文件不允许上传";
|
|
|
- } else {
|
|
|
- if(".jpg.jpeg.gif.png.bmp".contains(extName)) {
|
|
|
- savePath = filePath + "images\\" + datetime + "\\";
|
|
|
- visitPath = rootPath + "images/" + datetime + "/";
|
|
|
- } else if(".doc.docx.xls.xlsx.ppt.pptx.txt.pdf".contains(extName)) {
|
|
|
- savePath = filePath + "documents\\" + datetime + "\\";
|
|
|
- visitPath = rootPath + "documents/" + datetime + "/";
|
|
|
- } else if(".flv.swf.mp3.mp4.wav.wma.wmv.mid.avi.mpg.asf.rm.rmvb".contains(extName)) {
|
|
|
- savePath = filePath + "videos\\" + datetime + "\\";
|
|
|
- visitPath = rootPath + "videos/" + datetime + "/";
|
|
|
- } else {
|
|
|
- savePath = filePath + "files\\" + datetime + "\\";
|
|
|
- visitPath = rootPath + "files/" + datetime + "/";
|
|
|
- }
|
|
|
- newFileName = reName(extName);
|
|
|
- visitPath = visitPath + newFileName;
|
|
|
- savePath = savePath.replace("\\", "/");
|
|
|
- folder = new File(savePath);
|
|
|
- if(!folder.exists()) {
|
|
|
- folder.mkdirs();
|
|
|
- }
|
|
|
- File targetFile = new File(savePath + newFileName);
|
|
|
- upFile.transferTo(targetFile);
|
|
|
- fileMap.put("uid", String.valueOf(System.currentTimeMillis()));
|
|
|
- fileMap.put("status", "success");
|
|
|
- fileMap.put("name", oldFileName);
|
|
|
- fileMap.put("url", visitPath);
|
|
|
- result.put("upFile", fileMap);
|
|
|
- retCode = "1001";
|
|
|
- retMsg = "上传成功";
|
|
|
- }
|
|
|
- } catch(IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- result.put("retCode", retCode);
|
|
|
- result.put("retMsg", retMsg);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * multipleUpload 获取当前服务器系统时间
|
|
|
- */
|
|
|
- @RequestMapping(value="/multiple", method=RequestMethod.POST)
|
|
|
- public Map<String, Object> multipleUpload(@RequestParam("upFiles") MultipartFile[] files, HttpServletRequest req) {
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- String retCode="", retMsg="", token, savePath, visitPath, oldFileName, newFileName, extName;
|
|
|
- List<Map<String, String>> fileList = new ArrayList<>();
|
|
|
- token = req.getHeader("Authorization");
|
|
|
- if(token == null || "null".equals(token)) {
|
|
|
- retCode = "1002";
|
|
|
- retMsg = "对不起,您的操作非法,请先登录后操作!";
|
|
|
- } else {
|
|
|
- boolean auth = authUtil.hasLogin(token);
|
|
|
- if(!auth) {
|
|
|
- retCode = "1002";
|
|
|
- retMsg = "对不起,您没有登录或会话超时,请重新登录!";
|
|
|
- } else {
|
|
|
- if(files == null) {
|
|
|
- retCode = "1000";
|
|
|
- retMsg = "对不起,您缺少必要的请求参数,请重试!";
|
|
|
- } else {
|
|
|
- Map<String, String> fileMap;
|
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
|
|
|
- String datetime = sdf.format(new Date());
|
|
|
- //文件存放的目录
|
|
|
- File folder;
|
|
|
- boolean r = false;
|
|
|
- try {
|
|
|
- for(MultipartFile file: files) {
|
|
|
- oldFileName = file.getOriginalFilename();
|
|
|
- assert oldFileName != null;
|
|
|
- //文件新名字
|
|
|
- newFileName = reName(oldFileName);
|
|
|
- //文件后缀
|
|
|
- extName = oldFileName.substring(oldFileName.lastIndexOf("."));
|
|
|
- if(!(".jpg.jpeg.gif.png.bmp.flv.swf.mp3.mp4.wav.wma.wmv.mid.avi.mpg.asf.rm.rmvb.doc.docx.xls.xlsx.ppt.pptx.txt.pdf.rar.zip.gz.bz2").contains(extName)) {
|
|
|
- retCode = "1002";
|
|
|
- retMsg = "对不起,您选择上传的素材文件类型不允许上传,请重试!";
|
|
|
- r = false;
|
|
|
- break;
|
|
|
- } else if(oldFileName.length() > 100) {
|
|
|
- retCode = "1003";
|
|
|
- retMsg = "对不起,您选择上传的素材文件名称超出100个字符,请重试!";
|
|
|
- r = false;
|
|
|
- break;
|
|
|
- } else {
|
|
|
- rootPath = "";
|
|
|
- if(".jpg.jpeg.gif.png.bmp".contains(extName)) {
|
|
|
- savePath = filePath + "images\\" + datetime + "\\";
|
|
|
- visitPath = rootPath + "images/" + datetime + "/";
|
|
|
- } else if(".doc.docx.xls.xlsx.ppt.pptx.txt.pdf".contains(extName)) {
|
|
|
- savePath = filePath + "documents\\" + datetime + "\\";
|
|
|
- visitPath = rootPath + "documents/" + datetime + "/";
|
|
|
- } else if(".flv.swf.mp3.mp4.wav.wma.wmv.mid.avi.mpg.asf.rm.rmvb".contains(extName)) {
|
|
|
- savePath = filePath + "videos\\" + datetime + "\\";
|
|
|
- visitPath = rootPath + "videos/" + datetime + "/";
|
|
|
- } else {
|
|
|
- savePath = filePath + "files\\" + datetime + "\\";
|
|
|
- visitPath = rootPath + "files/" + datetime + "/";
|
|
|
- }
|
|
|
- visitPath = visitPath + newFileName;
|
|
|
- savePath = savePath.replace("\\", "/");
|
|
|
- folder = new File(savePath);
|
|
|
- if(!folder.exists()) {
|
|
|
- folder.mkdirs();
|
|
|
- }
|
|
|
- File targetFile = new File(savePath + newFileName);
|
|
|
- targetFile.setExecutable(true, true);
|
|
|
- file.transferTo(targetFile);
|
|
|
- fileMap = new HashMap<>();
|
|
|
- fileMap.put("uid", String.valueOf(System.currentTimeMillis()));
|
|
|
- fileMap.put("status", "success");
|
|
|
- fileMap.put("name", oldFileName);
|
|
|
- fileMap.put("url", visitPath);
|
|
|
- fileList.add(fileMap);
|
|
|
- r = true;
|
|
|
- }
|
|
|
- }
|
|
|
- if(r) {
|
|
|
- retCode = "1001";
|
|
|
- retMsg = "上传成功";
|
|
|
- result.put("fileList", fileList);
|
|
|
- }
|
|
|
- } catch(IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- result.put("retCode", retCode);
|
|
|
- result.put("retMsg", retMsg);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * reName 重命名上传文件
|
|
|
- *
|
|
|
- * @param extName 文件的后缀名
|
|
|
- * @return 重命名后的文件名
|
|
|
- */
|
|
|
- private String reName(String extName) {
|
|
|
- SimpleDateFormat simDtFmt = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
- String dateStr = simDtFmt.format(new Date()), randNbr = Math.round(Math.random() * 900) + 100 + "";
|
|
|
- return dateStr + randNbr + extName;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * delteByPath 通过路径删除文件
|
|
|
- */
|
|
|
- @RequestMapping(value="/delete", method=RequestMethod.POST)
|
|
|
- private Map<String, Object> delteByPath(HttpServletRequest req) {
|
|
|
- Map<String, Object> result = new HashMap<>();
|
|
|
- String retCode, retMsg, token, path;
|
|
|
- token = req.getHeader("Authorization");
|
|
|
- if(token == null || "null".equals(token)) {
|
|
|
- retCode = "1002";
|
|
|
- retMsg = "对不起,您请求的参数不正确,请重试!";
|
|
|
- } else {
|
|
|
- boolean auth = authUtil.hasLogin(token);
|
|
|
- if(!auth) {
|
|
|
- retCode = "1002";
|
|
|
- retMsg = "对不起,您没有登录或会话超时,请重新登录!";
|
|
|
- } else {
|
|
|
- path = req.getParameter("path");
|
|
|
- path = CharacterFiltUtil.inputFilter(path, true);
|
|
|
- if("".equals(path)) {
|
|
|
- retCode = "1000";
|
|
|
- retMsg = "对不起,您输入的信息有为空的必填项,请检查!";
|
|
|
- } else {
|
|
|
- path = filePath + path.replace(rootPath, "");
|
|
|
- File cFile = new File(path);
|
|
|
- if(cFile.exists()) {
|
|
|
- cFile.delete();
|
|
|
- }
|
|
|
- retCode = "1001";
|
|
|
- retMsg = "恭喜您,文件删除成功!";
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- result.put("retCode", retCode);
|
|
|
- result.put("retMsg", retMsg);
|
|
|
- return result;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * download 下载文件
|
|
|
- * @return 返回可供下载的文件
|
|
|
- */
|
|
|
- @RequestMapping(value="/download", method=RequestMethod.POST)
|
|
|
- public ResponseEntity<byte[]> download(HttpServletRequest req) {
|
|
|
- String path = req.getParameter("path");
|
|
|
- path = CharacterFiltUtil.inputFilter(path, true);
|
|
|
- if(!"".equals(path)) {
|
|
|
- File downFile = new File(path);
|
|
|
- if(downFile.exists()) {
|
|
|
- HttpHeaders headers = new HttpHeaders();
|
|
|
- headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
|
|
- ResponseEntity<byte[]> entity;
|
|
|
- try {
|
|
|
- entity = new ResponseEntity<>(FileUtils.readFileToByteArray(downFile), headers, HttpStatus.OK);
|
|
|
- return entity;
|
|
|
- } catch(IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-}
|