diff --git a/udf-starter-file/src/main/java/com/egridcloud/udf/file/service/FileService.java b/udf-starter-file/src/main/java/com/egridcloud/udf/file/service/FileService.java index ab3ac24..92cc54a 100644 --- a/udf-starter-file/src/main/java/com/egridcloud/udf/file/service/FileService.java +++ b/udf-starter-file/src/main/java/com/egridcloud/udf/file/service/FileService.java @@ -8,6 +8,7 @@ package com.egridcloud.udf.file.service; import java.io.File; import java.io.IOException; +import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Date; @@ -84,6 +85,8 @@ public class FileService { //获得base64文件ID(通过文件相对路径计算获得) String fileId = new String( Base64.encodeBase64(fileInfo.getRelativePath().getBytes(applicationConfig.getEncoding()))); + //使用url转码处理特殊字符 + fileId = URLEncoder.encode(fileId, applicationConfig.getEncoding()); fileInfo.setId(fileId); //获得文件对象(目录) File dest = new File(absolutePath); diff --git a/udf-starter-file/src/main/java/com/egridcloud/udf/file/web/FileController.java b/udf-starter-file/src/main/java/com/egridcloud/udf/file/web/FileController.java index abb5faa..44a71cb 100644 --- a/udf-starter-file/src/main/java/com/egridcloud/udf/file/web/FileController.java +++ b/udf-starter-file/src/main/java/com/egridcloud/udf/file/web/FileController.java @@ -6,13 +6,22 @@ */ package com.egridcloud.udf.file.web; +import java.io.BufferedInputStream; import java.io.IOException; +import java.io.OutputStream; +import javax.activation.FileTypeMap; +import javax.servlet.http.HttpServletResponse; + +import org.bouncycastle.util.encoders.Base64; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.FileSystemResource; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; +import com.egridcloud.udf.core.ApplicationConfig; import com.egridcloud.udf.core.RestResponse; +import com.egridcloud.udf.file.FileException; import com.egridcloud.udf.file.FileProperties; import com.egridcloud.udf.file.domain.FileInfo; import com.egridcloud.udf.file.service.FileService; @@ -26,6 +35,12 @@ import com.egridcloud.udf.file.service.FileService; @RestController public class FileController implements IFileController { + /** + * 描述 : applicationConfig + */ + @Autowired + private ApplicationConfig applicationConfig; + /** * 描述 : fileProperties */ @@ -49,4 +64,60 @@ public class FileController implements IFileController { return new RestResponse<>(fileProperties); } + @Override + public void load(String fileId, HttpServletResponse response) throws IOException { + this.loadFile(fileId, response); + } + + /** + * 描述 : 加载文件 + * + * @param fileId 相对路径 + * @param response 响应对象 + * @throws IOException IOException + */ + private void loadFile(String fileId, HttpServletResponse response) throws IOException { + //获得相对路径 + String relativeFilePath = + new String(Base64.decode(fileId.getBytes(applicationConfig.getEncoding()))); + //获得rootPath + String rootPath = fileProperties.getRootPath(); + //拼接绝对路径(目录) + String absolutePath = rootPath + relativeFilePath; + //获得资源对象 + FileSystemResource fsr = new FileSystemResource(absolutePath); + //判断资源是否存在 + if (!fsr.exists()) { + throw new FileException("file :" + relativeFilePath + " does not exist"); + } + //获得contentType + String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(fsr.getFile()); + //设置response + response.setContentLengthLong(fsr.contentLength()); + response.setCharacterEncoding(applicationConfig.getEncoding()); + response.setContentType(contentType); + //输出文件 + final int buffInt = 1024; + byte[] buff = new byte[buffInt]; + OutputStream os = null; + BufferedInputStream bis = null; + try { //NOSONAR + os = response.getOutputStream(); + bis = new BufferedInputStream(fsr.getInputStream()); + int i = bis.read(buff); + while (i != -1) { + os.write(buff, 0, buff.length); + os.flush(); + i = bis.read(buff); + } + } finally { + if (bis != null) { + bis.close(); + } + if (bis != null) { + bis.close(); + } + } + } + } diff --git a/udf-starter-file/src/main/java/com/egridcloud/udf/file/web/IFileController.java b/udf-starter-file/src/main/java/com/egridcloud/udf/file/web/IFileController.java index fb2e09c..50c4e05 100644 --- a/udf-starter-file/src/main/java/com/egridcloud/udf/file/web/IFileController.java +++ b/udf-starter-file/src/main/java/com/egridcloud/udf/file/web/IFileController.java @@ -8,6 +8,8 @@ package com.egridcloud.udf.file.web; import java.io.IOException; +import javax.servlet.http.HttpServletResponse; + import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; @@ -69,8 +71,28 @@ public interface IFileController { dataType = "string"), @ApiImplicitParam(paramType = "form", name = "file", value = "文件", required = true, dataType = "__file") }) - @RequestMapping(value = "upload", method = RequestMethod.POST) + @RequestMapping(method = RequestMethod.POST) public RestResponse upload(String pathCode, MultipartFile file) throws IllegalStateException, IOException; + /** + * 描述 : 通过文件ID访问文件 + * + * @param fileId 文件ID + * @param response 响应对象 + * @throws IOException 异常 + */ + @ApiOperation(value = "FILE_3", notes = "通过相对路径访问文件") + @ApiImplicitParams({ + @ApiImplicitParam(paramType = "query", name = "rmsApplicationName", value = "rms应用名称", + required = true, dataType = "string"), + @ApiImplicitParam(paramType = "query", name = "rmsSign", value = "rms认证秘钥", required = true, + dataType = "string"), + @ApiImplicitParam(paramType = "query", name = "rmsServiceCode", value = "rms接口编号", + required = true, dataType = "string"), + @ApiImplicitParam(paramType = "query", name = "fileId", value = "文件ID", required = true, + dataType = "string") }) + @RequestMapping(method = RequestMethod.GET) + public void load(String fileId, HttpServletResponse response) throws IOException; + } diff --git a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsAuthHandlerInterceptor.java b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsAuthHandlerInterceptor.java index 5e3449c..6c6110b 100644 --- a/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsAuthHandlerInterceptor.java +++ b/udf-starter-rms/src/main/java/com/egridcloud/udf/rms/RmsAuthHandlerInterceptor.java @@ -58,16 +58,26 @@ public class RmsAuthHandlerInterceptor implements HandlerInterceptor { Object handler) { //获取认证信息 String rmsApplicationName = request.getHeader(Constant.HEADER_RMS_APPLICATION_NAME_CODE); + if (StringUtils.isBlank(rmsApplicationName)) { + rmsApplicationName = request.getParameter(Constant.HEADER_RMS_APPLICATION_NAME_CODE); + } String rmsSign = request.getHeader(Constant.HEADER_RMS_SIGN_CODE); + if (StringUtils.isBlank(rmsSign)) { + rmsSign = request.getParameter(Constant.HEADER_RMS_SIGN_CODE); + } String rmsServiceCode = request.getHeader(Constant.HEADER_SERVICE_CODE_CODE); + if (StringUtils.isBlank(rmsServiceCode)) { + rmsServiceCode = request.getParameter(Constant.HEADER_SERVICE_CODE_CODE); + } String url = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString(); String method = request.getMethod(); - LOGGER.info("rmsApplicationName:{},rmsSign:{},rmsServiceCode:{},url:{},method:{}", rmsApplicationName, rmsSign, - rmsServiceCode, url, method); + LOGGER.info("rmsApplicationName:{},rmsSign:{},rmsServiceCode:{},url:{},method:{}", + rmsApplicationName, rmsSign, rmsServiceCode, url, method); //判断是否缺少认证信息 if (StringUtils.isBlank(rmsApplicationName) || StringUtils.isBlank(rmsSign) || StringUtils.isBlank(rmsServiceCode)) { - throw new AuthException("missing required authentication parameters (rmsApplicationName , rmsSign)"); + throw new AuthException( + "missing required authentication parameters (rmsApplicationName , rmsSign)"); } //判断环境(开发环境无需校验) if (!DEV_PROFILES.equals(env.getProperty("spring.profiles.active"))) { @@ -121,8 +131,8 @@ public class RmsAuthHandlerInterceptor implements HandlerInterceptor { } @Override - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) - throws Exception { + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, + Object handler, Exception ex) throws Exception { LOGGER.debug("SystemTagAuthHandlerInterceptor.afterCompletion"); }