diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java index f4473209..fabcc209 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java @@ -66,6 +66,8 @@ public class CommonController { private static final String UTF8 ="UTF-8"; + private static final String FILE_VIEW_ERROR = "预览文件失败"; + /** * @Author 政辉 * @return @@ -95,8 +97,9 @@ public class CommonController { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; // 获取上传文件对象 MultipartFile file = multipartRequest.getFile("file"); - if(file==null) + if(file==null) { return result; + } // 文件类型是否处于黑名单 if(CommonUtils.limitFileSuffix(file.getOriginalFilename(),fileSuffixLimits)){ result.setMessage("该文件类型不允许上传"); @@ -150,64 +153,65 @@ public class CommonController { throw new JeroBootException("文件不存在.."); } String fileUrl = ossFile.getUrl(); - InputStream inputStream = null; - OutputStream outputStream = null; - try { - String fileName = ""; - if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ - //本地下载 - String filePath = uploadpath + File.separator + fileUrl; - File file = new File(filePath); - if(!file.exists()){ - response.setStatus(404); - throw new RuntimeException("文件不存在.."); - } - // 文件名称 - fileName = file.getName(); - inputStream = new BufferedInputStream(new FileInputStream(filePath)); - }else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){ - // minio 下载 - // 通过MinioUtil查询时 只需要桶后面的路径 - String minioUrl = MinioUtil.getMinioUrl(); - // Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径 - minioUrl = minioUrl + MinioUtil.getBucketName() + "/"; - String url = fileUrl.replace(minioUrl, ""); - // 文件名称 - fileName = ossFile.getFileName(); - inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url); + if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){ + //本地下载 + String filePath = uploadpath + File.separator + fileUrl; + File file = new File(filePath); + if(!file.exists()){ + response.setStatus(404); + throw new JeroBootException("文件不存在.."); } - response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes(StandardCharsets.UTF_8),StandardCharsets.ISO_8859_1)); - response.setContentType("application/force-download");// 设置强制下载不打开 - outputStream = response.getOutputStream(); - byte[] buf = new byte[1024]; - int len; - if(inputStream==null) - return; - while ((len = inputStream.read(buf)) > 0) { - outputStream.write(buf, 0, len); - } - response.flushBuffer(); - } catch (IOException e) { - log.error("预览文件失败" + e.getMessage()); - response.setStatus(404); - e.printStackTrace(); - } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - log.error(e.getMessage(), e); + // 文件名称 + String fileName = file.getName(); + setResponse(response, fileName); + try (InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath)); + OutputStream outputStream = response.getOutputStream() + ) { + byte[] buf = new byte[1024]; + int len; + while ((len = inputStream.read(buf)) > 0) { + outputStream.write(buf, 0, len); } + response.flushBuffer(); + }catch (Exception e){ + log.error(FILE_VIEW_ERROR + e.getMessage()); + response.setStatus(404); + e.printStackTrace(); } - if (outputStream != null) { - try { - outputStream.close(); - } catch (IOException e) { - log.error(e.getMessage(), e); + }else if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){ + // minio 下载 + // 通过MinioUtil查询时 只需要桶后面的路径 + String minioUrl = MinioUtil.getMinioUrl(); + // Linux/unix 系统下文件路径分隔符为"/" 获取minio与存储桶的路径 + minioUrl = minioUrl + MinioUtil.getBucketName() + "/"; + String url = fileUrl.replace(minioUrl, ""); + // 文件名称 + String fileName = ossFile.getFileName(); + setResponse(response, fileName); + try (InputStream inputStream = MinioUtil.getMinioFile(MinioUtil.getBucketName(), url); + OutputStream outputStream = response.getOutputStream() + ) { + byte[] buf = new byte[1024]; + int len; + if(inputStream==null) { + return; } + while ((len = inputStream.read(buf)) > 0) { + outputStream.write(buf, 0, len); + } + response.flushBuffer(); + }catch (Exception e){ + log.error(FILE_VIEW_ERROR + e.getMessage()); + response.setStatus(404); + e.printStackTrace(); } } + } + private void setResponse(HttpServletResponse response, String fileName) { + response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)); + // 设置强制下载不打开 + response.setContentType("application/force-download"); } /** @@ -246,11 +250,7 @@ public class CommonController { // 中转请求method、body HttpMethod method = httpRequest.getMethod(); JSONObject params; - try { - params = JSON.parseObject(JSON.toJSONString(httpRequest.getBody())); - } catch (Exception e) { - params = new JSONObject(); - } + params = getJsonObject(httpRequest); // 中转请求问号参数 JSONObject variables = JSON.parseObject(JSON.toJSONString(request.getParameterMap())); variables.remove("url"); @@ -267,14 +267,7 @@ public class CommonController { result.setCode(statusCode); result.setSuccess(statusCode == 200); String responseBody = response.getBody(); - try { - // 尝试将返回结果转为JSON - Object json = JSON.parse(responseBody); - result.setResult(json); - } catch (Exception e) { - // 转成JSON失败,直接返回原始数据 - result.setResult(responseBody); - } + getResult(result, responseBody); return result; } catch (Exception e) { log.debug("中转HTTP请求失败", e); @@ -282,6 +275,27 @@ public class CommonController { } } + private void getResult(Result result, String responseBody) { + try { + // 尝试将返回结果转为JSON + Object json = JSON.parse(responseBody); + result.setResult(json); + } catch (Exception e) { + // 转成JSON失败,直接返回原始数据 + result.setResult(responseBody); + } + } + + private JSONObject getJsonObject(ServletServerHttpRequest httpRequest) { + JSONObject params; + try { + params = JSON.parseObject(JSON.toJSONString(httpRequest.getBody())); + } catch (Exception e) { + params = new JSONObject(); + } + return params; + } + /** * 预览图片&下载文件 * 请求地址:http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg} @@ -296,24 +310,21 @@ public class CommonController { if(oConvertUtils.isEmpty(imgPath) || "null".equals(imgPath)){ return; } + imgPath = imgPath.replace("..", "").replace("../",""); + if (imgPath.endsWith(",")) { + imgPath = imgPath.substring(0, imgPath.length() - 1); + } + String filePath = uploadpath + File.separator + imgPath; + File file = new File(filePath); + if(!file.exists()){ + response.setStatus(404); + throw new JeroBootException("文件["+imgPath+"]不存在.."); + } + response.setContentType("application/force-download");// 设置强制下载不打开 + response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)); // 其余处理略 - InputStream inputStream = null; - OutputStream outputStream = null; - try { - imgPath = imgPath.replace("..", "").replace("../",""); - if (imgPath.endsWith(",")) { - imgPath = imgPath.substring(0, imgPath.length() - 1); - } - String filePath = uploadpath + File.separator + imgPath; - File file = new File(filePath); - if(!file.exists()){ - response.setStatus(404); - throw new RuntimeException("文件["+imgPath+"]不存在.."); - } - response.setContentType("application/force-download");// 设置强制下载不打开 - response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)); - inputStream = new BufferedInputStream(new FileInputStream(filePath)); - outputStream = response.getOutputStream(); + try (InputStream inputStream = new BufferedInputStream(new FileInputStream(filePath)); + OutputStream outputStream = response.getOutputStream()) { byte[] buf = new byte[1024]; int len; while ((len = inputStream.read(buf)) > 0) { @@ -321,26 +332,9 @@ public class CommonController { } response.flushBuffer(); } catch (IOException e) { - log.error("预览文件失败" + e.getMessage()); + log.error(FILE_VIEW_ERROR + e.getMessage()); response.setStatus(404); e.printStackTrace(); - } finally { - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - log.error(e.getMessage(), e); - } - } - if (outputStream != null) { - try { - outputStream.close(); - } catch (IOException e) { - log.error(e.getMessage(), e); - } - } } - } - }