Browse Source

fix: 修复上传接口空指针问题

master_tdsql
hxh 1 year ago
parent
commit
07db896412
  1. 87
      shuili-admin/src/main/java/com/kms/web/controller/common/CommonController.java

87
shuili-admin/src/main/java/com/kms/web/controller/common/CommonController.java

@ -1,13 +1,16 @@
package com.kms.web.controller.common; package com.kms.web.controller.common;
import java.io.IOException; import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.shuili.common.utils.FileUploadUtils;
import com.kms.framework.config.ServerConfig; import com.kms.framework.config.ServerConfig;
import com.shuili.common.config.ShuiliConfig;
import com.shuili.common.constant.Constants;
import com.shuili.common.core.domain.AjaxResult;
import com.shuili.common.exception.BaseException;
import com.shuili.common.utils.FastDfsUtil;
import com.shuili.common.utils.FileUploadUtils;
import com.shuili.common.utils.StringUtils;
import com.shuili.common.utils.file.FileUtils;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
@ -19,16 +22,10 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import com.shuili.common.config.ShuiliConfig; import javax.servlet.ServletOutputStream;
import com.shuili.common.constant.Constants; import javax.servlet.http.HttpServletRequest;
import com.shuili.common.core.domain.AjaxResult; import javax.servlet.http.HttpServletResponse;
import com.shuili.common.exception.BaseException; import java.io.IOException;
import com.shuili.common.utils.FastDfsUtil;
import com.shuili.common.utils.StringUtils;
import com.shuili.common.utils.file.FileUtils;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
/** /**
* 通用请求处理 * 通用请求处理
@ -37,8 +34,7 @@ import cn.dev33.satoken.stp.StpUtil;
*/ */
@Api(tags = "通用上传接口") @Api(tags = "通用上传接口")
@RestController @RestController
public class CommonController public class CommonController {
{
private static final Logger log = LoggerFactory.getLogger(CommonController.class); private static final Logger log = LoggerFactory.getLogger(CommonController.class);
@ -54,14 +50,11 @@ public class CommonController
* *
* @param fileName 文件名称 * @param fileName 文件名称
* 必须登录才可以下载 * 必须登录才可以下载
*
*/ */
@SaCheckLogin @SaCheckLogin
@GetMapping("common/download") @GetMapping("common/download")
public void fileDownload(String fileUrl,String fileName, HttpServletResponse response, HttpServletRequest request) public void fileDownload(String fileUrl, String fileName, HttpServletResponse response, HttpServletRequest request) {
{ try {
try
{
String uploadType = ShuiliConfig.getUploadType(); String uploadType = ShuiliConfig.getUploadType();
@ -84,15 +77,15 @@ public class CommonController
response.setContentType("multipart/form-data"); response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", response.setHeader("Content-Disposition",
"attachment;fileName=" + fileName); "attachment;fileName=" + fileName);
ServletOutputStream outputStream=null; ServletOutputStream outputStream = null;
try { try {
outputStream = response.getOutputStream(); outputStream = response.getOutputStream();
byte[] bytes = fastdfs.downloadFile(fileUrl); byte[] bytes = fastdfs.downloadFile(fileUrl);
IOUtils.write(bytes,response.getOutputStream()); IOUtils.write(bytes, response.getOutputStream());
} catch (Exception e){ } catch (Exception e) {
throw new BaseException("文件下载失败"); throw new BaseException("文件下载失败");
}finally { } finally {
if (outputStream!=null) { if (outputStream != null) {
try { try {
outputStream.close(); outputStream.close();
} catch (IOException e) { } catch (IOException e) {
@ -118,9 +111,7 @@ public class CommonController
{ {
FileUtils.deleteFile(filePath); FileUtils.deleteFile(filePath);
}*/ }*/
} } catch (Exception e) {
catch (Exception e)
{
log.error("下载文件失败", e); log.error("下载文件失败", e);
} }
} }
@ -131,14 +122,11 @@ public class CommonController
*/ */
@ApiOperation("通用上传请求") @ApiOperation("通用上传请求")
@PostMapping("/common/upload") @PostMapping("/common/upload")
public AjaxResult uploadFile(MultipartFile file) throws Exception public AjaxResult uploadFile(MultipartFile file) throws Exception {
{
String uploadType = ShuiliConfig.getUploadType(); String uploadType = ShuiliConfig.getUploadType();
try try {
{
if (uploadType.equals("FastDFS")) { if ("FastDFS".equals(uploadType)) {
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
// 上传并返回文件名称 // 上传并返回文件名称
@ -160,20 +148,23 @@ public class CommonController
} }
} }
catch (Exception e) }
catch(
Exception e)
{ {
e.printStackTrace(); e.printStackTrace();
return AjaxResult.error(e.getMessage()); return AjaxResult.error(e.getMessage());
} }
}
}
/** /**
* 本地资源通用下载 * 本地资源通用下载
*/ */
@GetMapping("/common/download/resource") @GetMapping("/common/download/resource")
public void resourceDownload(String name, HttpServletRequest request, HttpServletResponse response) throws Exception public void resourceDownload(String name, HttpServletRequest request, HttpServletResponse response) throws Exception {
{
// 本地资源路径 // 本地资源路径
String localPath = ShuiliConfig.getProfile(); String localPath = ShuiliConfig.getProfile();
// 数据库资源地址 // 数据库资源地址
@ -204,20 +195,20 @@ public class CommonController
*/ */
@GetMapping("/download/fastDfs") @GetMapping("/download/fastDfs")
@SaCheckLogin @SaCheckLogin
public void downFile(String fileUrl,String fileName,HttpServletResponse response){ public void downFile(String fileUrl, String fileName, HttpServletResponse response) {
response.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data"); response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", response.setHeader("Content-Disposition",
"attachment;fileName=" + fileName); "attachment;fileName=" + fileName);
ServletOutputStream outputStream=null; ServletOutputStream outputStream = null;
try { try {
outputStream = response.getOutputStream(); outputStream = response.getOutputStream();
byte[] bytes = fastdfs.downloadFile(fileUrl); byte[] bytes = fastdfs.downloadFile(fileUrl);
IOUtils.write(bytes,response.getOutputStream()); IOUtils.write(bytes, response.getOutputStream());
} catch (Exception e){ } catch (Exception e) {
throw new BaseException("文件下载失败"); throw new BaseException("文件下载失败");
}finally { } finally {
if (outputStream!=null) { if (outputStream != null) {
try { try {
outputStream.close(); outputStream.close();
} catch (IOException e) { } catch (IOException e) {

Loading…
Cancel
Save