附件上传预览,配置限制更新

master
cheney 2 years ago
parent c72ce75137
commit b508e2c95b
  1. 22
      src/main/java/com/huoran/iasf/common/config/NonStaticResourceHttpRequestConfig.java
  2. 10
      src/main/java/com/huoran/iasf/controller/SysFilesController.java
  3. 5
      src/main/java/com/huoran/iasf/service/SysFilesService.java
  4. 60
      src/main/java/com/huoran/iasf/service/impl/SysFilesServiceImpl.java
  5. 2
      src/main/resources/application-prod.yml
  6. 6
      src/main/resources/application-test.yml
  7. 2
      src/main/resources/application.yml

@ -0,0 +1,22 @@
package com.huoran.iasf.common.config;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import javax.servlet.http.HttpServletRequest;
import java.nio.file.Path;
@Component
public class NonStaticResourceHttpRequestConfig extends ResourceHttpRequestHandler {
public final static String ATTR_FILE = "NON-STATIC-FILE";
@Override
protected Resource getResource(HttpServletRequest request) {
final Path filePath = (Path) request.getAttribute(ATTR_FILE);
return new FileSystemResource(filePath);
}
}

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huoran.iasf.common.config.NonStaticResourceHttpRequestConfig;
import com.huoran.iasf.common.utils.R;
import com.huoran.iasf.entity.SysFilesEntity;
import com.huoran.iasf.service.SysFilesService;
@ -15,14 +16,21 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
@ -82,7 +90,7 @@ public class SysFilesController {
@ApiOperation(value = "文件预览")
@GetMapping("/preview/{id}")
public void preview(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) {
public void preview(@PathVariable String id, HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
sysFilesService.preview(id, req, res);
}

@ -6,8 +6,11 @@ import com.huoran.iasf.vo.UEditorResultVO;
import com.huoran.iasf.vo.req.FileParameters;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
/**
@ -23,7 +26,7 @@ public interface SysFilesService extends IService<SysFilesEntity> {
void removeByIdsAndFiles(List<String> ids);
void preview(String id, HttpServletRequest req, HttpServletResponse res);
void preview(String id, HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException;
void download(String id, HttpServletRequest req, HttpServletResponse res);
}

@ -3,8 +3,10 @@ package com.huoran.iasf.service.impl;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huoran.iasf.common.config.FileUploadProperties;
import com.huoran.iasf.common.config.NonStaticResourceHttpRequestConfig;
import com.huoran.iasf.common.exception.BusinessException;
import com.huoran.iasf.common.exception.code.BaseResponseCode;
import com.huoran.iasf.common.utils.DateUtils;
@ -14,6 +16,7 @@ import com.huoran.iasf.service.SysFilesService;
import com.huoran.iasf.vo.UEditorResultVO;
import com.huoran.iasf.vo.req.FileParameters;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
@ -22,6 +25,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -29,6 +33,10 @@ import java.io.*;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
@ -49,6 +57,9 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt
@Resource
private FileUploadProperties fileUploadProperties;
@Autowired
private NonStaticResourceHttpRequestConfig nonStaticResourceHttpRequestConfig;
@Override
public UEditorResultVO saveFile(MultipartFile file, FileParameters fileParameters) {
if (ObjectUtil.isEmpty(file) || file.getSize() <= 0) {
@ -180,26 +191,45 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt
@Override
public void preview(String id, HttpServletRequest req, HttpServletResponse res) {
public void preview(String id, HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
if (id != null) {
SysFilesEntity entity = this.getById(id);
String path = entity.getFilePath();
res.setContentLength(entity.getFileSize());
// if (entity.getFormat().equals("mp4")){
//保存视频磁盘路径
Path filePath = Paths.get(path);
//获取视频的类型,比如是MP4这样
File file = new File(path);
byte[] buffer = FileUtil.readBytes(path);
if (file.exists()) {
try {
//设置contentType
res.setContentType(MediaType.ALL_VALUE);
res.setCharacterEncoding("UTF-8");
//获取outputStream
ServletOutputStream outputStream = res.getOutputStream();
//输出
IoUtil.write(outputStream, true, buffer);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
String mimeType = Files.probeContentType(filePath);
if (StrUtil.isNotEmpty(mimeType)) {
//判断类型,根据不同的类型文件来处理对应的数据
res.setContentType(mimeType);
res.addHeader("Content-Length", "" + file.length());
}
//转换视频流部分
req.setAttribute(NonStaticResourceHttpRequestConfig.ATTR_FILE, filePath);
nonStaticResourceHttpRequestConfig.handleRequest(req, res);
// }else {
// File file = new File(path);
// byte[] buffer = FileUtil.readBytes(path);
// if (file.exists()) {
// try {
// //设置contentType
// res.setContentType(MediaType.ALL_VALUE);
// res.setCharacterEncoding("UTF-8");
// //获取outputStream
// ServletOutputStream outputStream = res.getOutputStream();
// //输出
// IoUtil.write(outputStream, true, buffer);
// } catch (Exception e) {
// log.error(e.getMessage(), e);
// }
// }
// }
}
}

@ -27,7 +27,7 @@ file:
path: D:/files/ #windows
#path: /data/files/ #linux
#文件预览、下载的url, 末尾请勿加 /
url: http://localhost:10000/files
url: :10000/iasf/sysFiles/preview/
knife4j:
production: true #生成环境禁用查看文档

@ -6,9 +6,9 @@ spring:
datasource:
master:
username: root
password: 123456
password: HuoRan@2021
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/iasf?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2b8
url: jdbc:mysql://139.9.47.170:3306/iasf?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2b8
redis:
host: localhost # Redis服务器地址
database: 0 # Redis数据库索引(默认为0)
@ -27,4 +27,4 @@ file:
path: D:/files/ #windows
#path: /data/files/ #linux
#文件预览、下载的url, 末尾请勿加 /
url: http://localhost:10000/files
url: :10000/iasf/sysFiles/preview/

@ -19,7 +19,7 @@ spring:
# 文件大小限制
servlet:
multipart:
max-file-size: 10MB
max-file-size: 100MB
max-request-size: 100MB
# redis token信息
redis:

Loading…
Cancel
Save