Commit d15be1c4 authored by 赵啸非's avatar 赵啸非

修改上传文件

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