|
|
@ -1,13 +1,16 @@ |
|
|
|
package com.kms.web.controller.common; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
|
|
|
import com.shuili.common.utils.FileUploadUtils; |
|
|
|
import cn.dev33.satoken.annotation.SaCheckLogin; |
|
|
|
import cn.dev33.satoken.stp.StpUtil; |
|
|
|
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.ApiOperation; |
|
|
|
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.multipart.MultipartFile; |
|
|
|
|
|
|
|
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.StringUtils; |
|
|
|
import com.shuili.common.utils.file.FileUtils; |
|
|
|
|
|
|
|
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 java.io.IOException; |
|
|
|
|
|
|
|
/** |
|
|
|
* 通用请求处理 |
|
|
@ -37,8 +34,7 @@ import cn.dev33.satoken.stp.StpUtil; |
|
|
|
*/ |
|
|
|
@Api(tags = "通用上传接口") |
|
|
|
@RestController |
|
|
|
public class CommonController |
|
|
|
{ |
|
|
|
public class CommonController { |
|
|
|
private static final Logger log = LoggerFactory.getLogger(CommonController.class); |
|
|
|
|
|
|
|
|
|
|
@ -53,15 +49,12 @@ public class CommonController |
|
|
|
* 通用下载请求 |
|
|
|
* |
|
|
|
* @param fileName 文件名称 |
|
|
|
* 必须登录才可以下载 |
|
|
|
* |
|
|
|
* 必须登录才可以下载 |
|
|
|
*/ |
|
|
|
@SaCheckLogin |
|
|
|
@GetMapping("common/download") |
|
|
|
public void fileDownload(String fileUrl,String fileName, HttpServletResponse response, HttpServletRequest request) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
public void fileDownload(String fileUrl, String fileName, HttpServletResponse response, HttpServletRequest request) { |
|
|
|
try { |
|
|
|
|
|
|
|
String uploadType = ShuiliConfig.getUploadType(); |
|
|
|
|
|
|
@ -84,15 +77,15 @@ public class CommonController |
|
|
|
response.setContentType("multipart/form-data"); |
|
|
|
response.setHeader("Content-Disposition", |
|
|
|
"attachment;fileName=" + fileName); |
|
|
|
ServletOutputStream outputStream=null; |
|
|
|
ServletOutputStream outputStream = null; |
|
|
|
try { |
|
|
|
outputStream = response.getOutputStream(); |
|
|
|
byte[] bytes = fastdfs.downloadFile(fileUrl); |
|
|
|
IOUtils.write(bytes,response.getOutputStream()); |
|
|
|
} catch (Exception e){ |
|
|
|
IOUtils.write(bytes, response.getOutputStream()); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new BaseException("文件下载失败"); |
|
|
|
}finally { |
|
|
|
if (outputStream!=null) { |
|
|
|
} finally { |
|
|
|
if (outputStream != null) { |
|
|
|
try { |
|
|
|
outputStream.close(); |
|
|
|
} catch (IOException e) { |
|
|
@ -118,9 +111,7 @@ public class CommonController |
|
|
|
{ |
|
|
|
FileUtils.deleteFile(filePath); |
|
|
|
}*/ |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("下载文件失败", e); |
|
|
|
} |
|
|
|
} |
|
|
@ -131,14 +122,11 @@ public class CommonController |
|
|
|
*/ |
|
|
|
@ApiOperation("通用上传请求") |
|
|
|
@PostMapping("/common/upload") |
|
|
|
public AjaxResult uploadFile(MultipartFile file) throws Exception |
|
|
|
{ |
|
|
|
public AjaxResult uploadFile(MultipartFile file) throws Exception { |
|
|
|
String uploadType = ShuiliConfig.getUploadType(); |
|
|
|
try |
|
|
|
{ |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (uploadType.equals("FastDFS")) { |
|
|
|
if ("FastDFS".equals(uploadType)) { |
|
|
|
String fileName = file.getOriginalFilename(); |
|
|
|
|
|
|
|
// 上传并返回文件名称
|
|
|
@ -160,20 +148,23 @@ public class CommonController |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
catch( |
|
|
|
Exception e) |
|
|
|
|
|
|
|
return AjaxResult.error(e.getMessage()); |
|
|
|
} |
|
|
|
{ |
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
return AjaxResult.error(e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 本地资源通用下载 |
|
|
|
* 本地资源通用下载 |
|
|
|
*/ |
|
|
|
@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(); |
|
|
|
// 数据库资源地址
|
|
|
@ -191,33 +182,33 @@ public class CommonController |
|
|
|
|
|
|
|
@PostMapping("/logout") |
|
|
|
@SaCheckLogin |
|
|
|
public AjaxResult logout() { |
|
|
|
public AjaxResult logout() { |
|
|
|
|
|
|
|
// 登录保存缓存
|
|
|
|
StpUtil.logout(); |
|
|
|
//删除缓存
|
|
|
|
return AjaxResult.success(); |
|
|
|
} |
|
|
|
// 登录保存缓存
|
|
|
|
StpUtil.logout(); |
|
|
|
//删除缓存
|
|
|
|
return AjaxResult.success(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 从fastDfs 使用文件路径 |
|
|
|
*/ |
|
|
|
@GetMapping("/download/fastDfs") |
|
|
|
@SaCheckLogin |
|
|
|
public void downFile(String fileUrl,String fileName,HttpServletResponse response){ |
|
|
|
public void downFile(String fileUrl, String fileName, HttpServletResponse response) { |
|
|
|
response.setCharacterEncoding("utf-8"); |
|
|
|
response.setContentType("multipart/form-data"); |
|
|
|
response.setHeader("Content-Disposition", |
|
|
|
"attachment;fileName=" + fileName); |
|
|
|
ServletOutputStream outputStream=null; |
|
|
|
ServletOutputStream outputStream = null; |
|
|
|
try { |
|
|
|
outputStream = response.getOutputStream(); |
|
|
|
byte[] bytes = fastdfs.downloadFile(fileUrl); |
|
|
|
IOUtils.write(bytes,response.getOutputStream()); |
|
|
|
} catch (Exception e){ |
|
|
|
IOUtils.write(bytes, response.getOutputStream()); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new BaseException("文件下载失败"); |
|
|
|
}finally { |
|
|
|
if (outputStream!=null) { |
|
|
|
} finally { |
|
|
|
if (outputStream != null) { |
|
|
|
try { |
|
|
|
outputStream.close(); |
|
|
|
} catch (IOException e) { |
|
|
|