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

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

@ -6,8 +6,11 @@ import com.huoran.iasf.vo.UEditorResultVO;
import com.huoran.iasf.vo.req.FileParameters; import com.huoran.iasf.vo.req.FileParameters;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List; import java.util.List;
/** /**
@ -23,7 +26,7 @@ public interface SysFilesService extends IService<SysFilesEntity> {
void removeByIdsAndFiles(List<String> ids); 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); 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.FileUtil;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huoran.iasf.common.config.FileUploadProperties; 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.BusinessException;
import com.huoran.iasf.common.exception.code.BaseResponseCode; import com.huoran.iasf.common.exception.code.BaseResponseCode;
import com.huoran.iasf.common.utils.DateUtils; 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.UEditorResultVO;
import com.huoran.iasf.vo.req.FileParameters; import com.huoran.iasf.vo.req.FileParameters;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -22,6 +25,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -29,6 +33,10 @@ import java.io.*;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.NetworkInterface; import java.net.NetworkInterface;
import java.net.URLEncoder; 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.Date;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
@ -49,6 +57,9 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt
@Resource @Resource
private FileUploadProperties fileUploadProperties; private FileUploadProperties fileUploadProperties;
@Autowired
private NonStaticResourceHttpRequestConfig nonStaticResourceHttpRequestConfig;
@Override @Override
public UEditorResultVO saveFile(MultipartFile file, FileParameters fileParameters) { public UEditorResultVO saveFile(MultipartFile file, FileParameters fileParameters) {
if (ObjectUtil.isEmpty(file) || file.getSize() <= 0) { if (ObjectUtil.isEmpty(file) || file.getSize() <= 0) {
@ -180,26 +191,45 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt
@Override @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) { if (id != null) {
SysFilesEntity entity = this.getById(id); SysFilesEntity entity = this.getById(id);
String path = entity.getFilePath(); String path = entity.getFilePath();
res.setContentLength(entity.getFileSize()); res.setContentLength(entity.getFileSize());
File file = new File(path);
byte[] buffer = FileUtil.readBytes(path); // if (entity.getFormat().equals("mp4")){
if (file.exists()) { //保存视频磁盘路径
try { Path filePath = Paths.get(path);
//设置contentType
res.setContentType(MediaType.ALL_VALUE); //获取视频的类型,比如是MP4这样
res.setCharacterEncoding("UTF-8"); File file = new File(path);
//获取outputStream String mimeType = Files.probeContentType(filePath);
ServletOutputStream outputStream = res.getOutputStream(); if (StrUtil.isNotEmpty(mimeType)) {
//输出 //判断类型,根据不同的类型文件来处理对应的数据
IoUtil.write(outputStream, true, buffer); res.setContentType(mimeType);
} catch (Exception e) { res.addHeader("Content-Length", "" + file.length());
log.error(e.getMessage(), e);
} }
} //转换视频流部分
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: D:/files/ #windows
#path: /data/files/ #linux #path: /data/files/ #linux
#文件预览、下载的url, 末尾请勿加 / #文件预览、下载的url, 末尾请勿加 /
url: http://localhost:10000/files url: :10000/iasf/sysFiles/preview/
knife4j: knife4j:
production: true #生成环境禁用查看文档 production: true #生成环境禁用查看文档

@ -6,9 +6,9 @@ spring:
datasource: datasource:
master: master:
username: root username: root
password: 123456 password: HuoRan@2021
driver-class-name: com.mysql.cj.jdbc.Driver 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: redis:
host: localhost # Redis服务器地址 host: localhost # Redis服务器地址
database: 0 # Redis数据库索引(默认为0) database: 0 # Redis数据库索引(默认为0)
@ -27,4 +27,4 @@ file:
path: D:/files/ #windows path: D:/files/ #windows
#path: /data/files/ #linux #path: /data/files/ #linux
#文件预览、下载的url, 末尾请勿加 / #文件预览、下载的url, 末尾请勿加 /
url: http://localhost:10000/files url: :10000/iasf/sysFiles/preview/

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

Loading…
Cancel
Save