Commit 2aec567e authored by 赵啸非's avatar 赵啸非

修改上传文件

parent 03a0e2f2
...@@ -52,6 +52,4 @@ public interface UploadService extends IService { ...@@ -52,6 +52,4 @@ public interface UploadService extends IService {
void uploadDownload(String fileName, HttpServletResponse response); void uploadDownload(String fileName, HttpServletResponse response);
void deleteFile(String fileName);
} }
\ No newline at end of file
...@@ -15,7 +15,9 @@ import org.springframework.http.MediaType; ...@@ -15,7 +15,9 @@ import org.springframework.http.MediaType;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
...@@ -47,7 +49,9 @@ public class UploadServiceImpl implements UploadService { ...@@ -47,7 +49,9 @@ public class UploadServiceImpl implements UploadService {
if (tempFile == null || tempFile.getSize() == 0) { if (tempFile == null || tempFile.getSize() == 0) {
throw new AppException("没有要上传的文件!"); throw new AppException("没有要上传的文件!");
} }
if(StringUtils.isEmpty(prePath)){
prePath = "file/fileupload";
}
String fileName = tempFile.getOriginalFilename(); String fileName = tempFile.getOriginalFilename();
String extension = FilenameUtils.getExtension(fileName); String extension = FilenameUtils.getExtension(fileName);
UploadFileType type = UploadFileType.getFileType(extension); UploadFileType type = UploadFileType.getFileType(extension);
...@@ -55,7 +59,7 @@ public class UploadServiceImpl implements UploadService { ...@@ -55,7 +59,7 @@ public class UploadServiceImpl implements UploadService {
log.error("文件上传大小超过限制,当前文件---" + tempFile.getSize() + ",允许大小----------" + type.getMaxSize()); log.error("文件上传大小超过限制,当前文件---" + tempFile.getSize() + ",允许大小----------" + type.getMaxSize());
throw new AppException("文件上传大小超过限制!"); throw new AppException("文件上传大小超过限制!");
} }
if (null != fileName && fileName.length() > 255) { if (null != fileName && fileName.length() > 50) {
throw new AppException("文件名称过长,无法上传!"); throw new AppException("文件名称过长,无法上传!");
} }
...@@ -65,12 +69,14 @@ public class UploadServiceImpl implements UploadService { ...@@ -65,12 +69,14 @@ public class UploadServiceImpl implements UploadService {
if (!pathDir.exists()) { if (!pathDir.exists()) {
pathDir.mkdirs(); pathDir.mkdirs();
} }
String newName = new Date().getTime() + "." + extension; String newName = new Date().getTime() + "." + extension;
//String newName = IdUtil.fastSimpleUUID() + "." + extension;
String filePathAll = filePath + newName; String filePathAll = filePath + newName;
File uploadFile = new File(filePathAll); File uploadFile = new File(filePathAll);
try { try {
log.info("文件正在储存,filepath:"+filePathAll); log.info("文件正在储存");
tempFile.transferTo(uploadFile); tempFile.transferTo(uploadFile);
} catch (Exception e) { } catch (Exception e) {
throw new AppException(e.getMessage()); throw new AppException(e.getMessage());
...@@ -88,7 +94,7 @@ public class UploadServiceImpl implements UploadService { ...@@ -88,7 +94,7 @@ public class UploadServiceImpl implements UploadService {
@Override @Override
public void fileDownload(String fileName, Boolean delete, HttpServletResponse response) { public void fileDownload(String fileName, Boolean delete, HttpServletResponse response) {
String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1); String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
String filePath = this.filePath + fileName; String filePath = getFilePath(fileName);
try { try {
response.setContentType(MediaType.IMAGE_JPEG_VALUE); response.setContentType(MediaType.IMAGE_JPEG_VALUE);
setAttachmentResponseHeader(response, realFileName); setAttachmentResponseHeader(response, realFileName);
...@@ -101,14 +107,28 @@ public class UploadServiceImpl implements UploadService { ...@@ -101,14 +107,28 @@ public class UploadServiceImpl implements UploadService {
@Override @Override
public void preview(String fileName, HttpServletResponse response) { public void preview(String fileName, HttpServletResponse response) {
String filePath = this.filePath+"/file/preview/" + fileName; // String filePath = this.filePath+"/preview/" + fileName;
// try {
// response.setContentType(MediaType.IMAGE_JPEG_VALUE);
// setAttachmentResponseHeader(response, fileName);
// FileUtil.writeToStream(filePath, response.getOutputStream());
// } catch (Exception e) {
// log.error("下载文件失败", e);
// }
String filePath = getFilePath(fileName);
try { try {
response.setContentType(MediaType.IMAGE_JPEG_VALUE); File file = new File(filePath);
setAttachmentResponseHeader(response, fileName); BufferedImage image = ImageIO.read(file);
FileUtil.writeToStream(filePath, response.getOutputStream()); response.setHeader("Pragma", "No-cache");
} catch (Exception e) { response.setHeader("Cache-Control", "No-cache");
log.error("下载文件失败", e); response.setDateHeader("Expires", 0L);
response.setContentType("image/jpeg");
ImageIO.write(image, "JPEG", response.getOutputStream());
} catch (Exception var4) {
this.log.debug("响应图片消息异常-->" + var4.getMessage());
} }
} }
...@@ -159,14 +179,4 @@ public class UploadServiceImpl implements UploadService { ...@@ -159,14 +179,4 @@ public class UploadServiceImpl implements UploadService {
} }
} }
@Override
public void deleteFile(String fileName) {
String filePath = this.filePath+ fileName;
try {
FileUtil.del(filePath);
} catch (Exception e) {
log.error("下载文件失败", e);
}
}
} }
\ No newline at end of file
...@@ -2,7 +2,6 @@ package com.mortals.xhx.base.system.upload.web; ...@@ -2,7 +2,6 @@ package com.mortals.xhx.base.system.upload.web;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.exception.AppException; import com.mortals.framework.exception.AppException;
import com.mortals.framework.web.BaseController; import com.mortals.framework.web.BaseController;
import com.mortals.xhx.base.system.upload.service.UploadService; import com.mortals.xhx.base.system.upload.service.UploadService;
...@@ -16,12 +15,15 @@ import java.util.HashMap; ...@@ -16,12 +15,15 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
* 上传文件 * <p>Title: 上传文件</p>
* <p>Description: UploadfileController </p>
* <p>Copyright: Copyright &reg; </p>
* <p>Company: </p>
* *
* @author: zxfei * @author
* @date: 2021/11/30 10:06 * @version 1.0.0
*/ */
@RestController("uploadControler") @RestController
@RequestMapping("file") @RequestMapping("file")
public class UploadController extends BaseController { public class UploadController extends BaseController {
...@@ -29,8 +31,7 @@ public class UploadController extends BaseController { ...@@ -29,8 +31,7 @@ public class UploadController extends BaseController {
private UploadService uploadService; private UploadService uploadService;
@RequestMapping(value = "upload") @RequestMapping(value = "upload")
@UnAuth public String doFileUpload(HttpServletRequest request, HttpServletResponse response, UploadForm form) {
public String doFileUpload(HttpServletRequest request, UploadForm form) {
Map<String, Object> model = new HashMap<>(); Map<String, Object> model = new HashMap<>();
String jsonStr = ""; String jsonStr = "";
try { try {
...@@ -54,7 +55,6 @@ public class UploadController extends BaseController { ...@@ -54,7 +55,6 @@ public class UploadController extends BaseController {
@RequestMapping(value = "commonupload") @RequestMapping(value = "commonupload")
@UnAuth
public String doFileUpload(MultipartFile file, @RequestParam(value = "prePath",defaultValue = "file/fileupload") String prePath) { public String doFileUpload(MultipartFile file, @RequestParam(value = "prePath",defaultValue = "file/fileupload") String prePath) {
Map<String, Object> model = new HashMap<>(); Map<String, Object> model = new HashMap<>();
String jsonStr = ""; String jsonStr = "";
...@@ -99,38 +99,38 @@ public class UploadController extends BaseController { ...@@ -99,38 +99,38 @@ public class UploadController extends BaseController {
* *
* @param fileName 文件名称 * @param fileName 文件名称
*/ */
@GetMapping("preview/{fileName}") @GetMapping("preview/{prePath}/{fileName}")
public void preView(@PathVariable(value="fileName") String fileName, HttpServletResponse response) { public void preViewPath(@PathVariable(value="fileName") String fileName,@PathVariable(value="prePath") String prePath, HttpServletResponse response) {
try { try {
uploadService.preview(fileName, response); uploadService.preview(prePath+"/"+fileName, response);
} catch (Exception e) { } catch (Exception e) {
log.error("下载文件失败:", e); log.error("下载文件失败:", e);
} }
} }
/** /**
* 通用文件下载 * 图片预览 (PathVariable)
* *
* @param fileName 文件名称 * @param fileName 文件名称
*/ */
@GetMapping("fileupload/{fileName}") @GetMapping("preview/{fileName}")
public void fileupload(@PathVariable(value="fileName") String fileName, HttpServletResponse response) { public void preView(@PathVariable(value="fileName") String fileName, HttpServletResponse response) {
try { try {
uploadService.uploadDownload(fileName, response); uploadService.preview(fileName, response);
} catch (Exception e) { } catch (Exception e) {
log.error("下载文件失败:", e); log.error("下载文件失败:", e);
} }
} }
/** /**
* 通用文件删除 * 通用文件下载
* *
* @param fileName 文件名称 * @param fileName 文件名称
*/ */
@GetMapping("fileupload/{fileName}") @GetMapping("fileupload/{fileName}")
public void fileDel(@PathVariable(value="fileName") String fileName, HttpServletResponse response) { public void fileupload(@PathVariable(value="fileName") String fileName, HttpServletResponse response) {
try { try {
//uploadService.uploadDownload(fileName, response); uploadService.uploadDownload(fileName, response);
} catch (Exception e) { } catch (Exception e) {
log.error("下载文件失败:", e); log.error("下载文件失败:", e);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment