diff --git a/src/main/java/com/huoran/iasf/common/config/NonStaticResourceHttpRequestConfig.java b/src/main/java/com/huoran/iasf/common/config/NonStaticResourceHttpRequestConfig.java new file mode 100644 index 0000000..aeca4ee --- /dev/null +++ b/src/main/java/com/huoran/iasf/common/config/NonStaticResourceHttpRequestConfig.java @@ -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); + } + +} \ No newline at end of file diff --git a/src/main/java/com/huoran/iasf/controller/SysFilesController.java b/src/main/java/com/huoran/iasf/controller/SysFilesController.java index 803cff2..a7d7f64 100644 --- a/src/main/java/com/huoran/iasf/controller/SysFilesController.java +++ b/src/main/java/com/huoran/iasf/controller/SysFilesController.java @@ -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); } diff --git a/src/main/java/com/huoran/iasf/service/SysFilesService.java b/src/main/java/com/huoran/iasf/service/SysFilesService.java index 8bef113..399ceb2 100644 --- a/src/main/java/com/huoran/iasf/service/SysFilesService.java +++ b/src/main/java/com/huoran/iasf/service/SysFilesService.java @@ -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 { void removeByIdsAndFiles(List 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); } diff --git a/src/main/java/com/huoran/iasf/service/impl/SysFilesServiceImpl.java b/src/main/java/com/huoran/iasf/service/impl/SysFilesServiceImpl.java index 6b17218..d1a31d5 100644 --- a/src/main/java/com/huoran/iasf/service/impl/SysFilesServiceImpl.java +++ b/src/main/java/com/huoran/iasf/service/impl/SysFilesServiceImpl.java @@ -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