@ -44,12 +44,9 @@ public class CommonController
* @param delete 是 否 删 除
* @param delete 是 否 删 除
* /
* /
@GetMapping ( "/download" )
@GetMapping ( "/download" )
public void fileDownload ( String fileName , Boolean delete , HttpServletResponse response , HttpServletRequest request )
public void fileDownload ( String fileName , Boolean delete , HttpServletResponse response , HttpServletRequest request ) {
{
try {
try
if ( ! FileUtils . checkAllowDownload ( fileName ) ) {
{
if ( ! FileUtils . checkAllowDownload ( fileName ) )
{
throw new Exception ( StringUtils . format ( "文件名称({})非法,不允许下载。 " , fileName ) ) ;
throw new Exception ( StringUtils . format ( "文件名称({})非法,不允许下载。 " , fileName ) ) ;
}
}
String realFileName = System . currentTimeMillis ( ) + fileName . substring ( fileName . indexOf ( "_" ) + 1 ) ;
String realFileName = System . currentTimeMillis ( ) + fileName . substring ( fileName . indexOf ( "_" ) + 1 ) ;
@ -58,13 +55,10 @@ public class CommonController
response . setContentType ( MediaType . APPLICATION_OCTET_STREAM_VALUE ) ;
response . setContentType ( MediaType . APPLICATION_OCTET_STREAM_VALUE ) ;
FileUtils . setAttachmentResponseHeader ( response , realFileName ) ;
FileUtils . setAttachmentResponseHeader ( response , realFileName ) ;
FileUtils . writeBytes ( filePath , response . getOutputStream ( ) ) ;
FileUtils . writeBytes ( filePath , response . getOutputStream ( ) ) ;
if ( delete )
if ( delete ) {
{
FileUtils . deleteFile ( filePath ) ;
FileUtils . deleteFile ( filePath ) ;
}
}
}
} catch ( Exception e ) {
catch ( Exception e )
{
log . error ( "下载文件失败" , e ) ;
log . error ( "下载文件失败" , e ) ;
}
}
}
}
@ -73,10 +67,8 @@ public class CommonController
* 通 用 上 传 请 求 ( 单 个 )
* 通 用 上 传 请 求 ( 单 个 )
* /
* /
@PostMapping ( "/upload" )
@PostMapping ( "/upload" )
public AjaxResult uploadFile ( MultipartFile file ) throws Exception
public AjaxResult uploadFile ( MultipartFile file ) throws Exception {
{
try {
try
{
// 上传文件路径
// 上传文件路径
String filePath = HypConfig . getUploadPath ( ) ;
String filePath = HypConfig . getUploadPath ( ) ;
// 上传并返回新文件名称
// 上传并返回新文件名称
@ -88,9 +80,7 @@ public class CommonController
ajax . put ( "newFileName" , FileUtils . getName ( fileName ) ) ;
ajax . put ( "newFileName" , FileUtils . getName ( fileName ) ) ;
ajax . put ( "originalFilename" , file . getOriginalFilename ( ) ) ;
ajax . put ( "originalFilename" , file . getOriginalFilename ( ) ) ;
return ajax ;
return ajax ;
}
} catch ( Exception e ) {
catch ( Exception e )
{
return AjaxResult . error ( e . getMessage ( ) ) ;
return AjaxResult . error ( e . getMessage ( ) ) ;
}
}
}
}
@ -99,18 +89,15 @@ public class CommonController
* 通 用 上 传 请 求 ( 多 个 )
* 通 用 上 传 请 求 ( 多 个 )
* /
* /
@PostMapping ( "/uploads" )
@PostMapping ( "/uploads" )
public AjaxResult uploadFiles ( List < MultipartFile > files ) throws Exception
public AjaxResult uploadFiles ( List < MultipartFile > files ) throws Exception {
{
try {
try
{
// 上传文件路径
// 上传文件路径
String filePath = HypConfig . getUploadPath ( ) ;
String filePath = HypConfig . getUploadPath ( ) ;
List < String > urls = new ArrayList < String > ( ) ;
List < String > urls = new ArrayList < String > ( ) ;
List < String > fileNames = new ArrayList < String > ( ) ;
List < String > fileNames = new ArrayList < String > ( ) ;
List < String > newFileNames = new ArrayList < String > ( ) ;
List < String > newFileNames = new ArrayList < String > ( ) ;
List < String > originalFilenames = new ArrayList < String > ( ) ;
List < String > originalFilenames = new ArrayList < String > ( ) ;
for ( MultipartFile file : files )
for ( MultipartFile file : files ) {
{
// 上传并返回新文件名称
// 上传并返回新文件名称
String fileName = FileUploadUtils . upload ( filePath , file ) ;
String fileName = FileUploadUtils . upload ( filePath , file ) ;
String url = serverConfig . getUrl ( ) + fileName ;
String url = serverConfig . getUrl ( ) + fileName ;
@ -125,9 +112,7 @@ public class CommonController
ajax . put ( "newFileNames" , StringUtils . join ( newFileNames , FILE_DELIMETER ) ) ;
ajax . put ( "newFileNames" , StringUtils . join ( newFileNames , FILE_DELIMETER ) ) ;
ajax . put ( "originalFilenames" , StringUtils . join ( originalFilenames , FILE_DELIMETER ) ) ;
ajax . put ( "originalFilenames" , StringUtils . join ( originalFilenames , FILE_DELIMETER ) ) ;
return ajax ;
return ajax ;
}
} catch ( Exception e ) {
catch ( Exception e )
{
return AjaxResult . error ( e . getMessage ( ) ) ;
return AjaxResult . error ( e . getMessage ( ) ) ;
}
}
}
}
@ -137,12 +122,9 @@ public class CommonController
* /
* /
@GetMapping ( "/download/resource" )
@GetMapping ( "/download/resource" )
public void resourceDownload ( String resource , HttpServletRequest request , HttpServletResponse response )
public void resourceDownload ( String resource , HttpServletRequest request , HttpServletResponse response )
throws Exception
throws Exception {
{
try {
try
if ( ! FileUtils . checkAllowDownload ( resource ) ) {
{
if ( ! FileUtils . checkAllowDownload ( resource ) )
{
throw new Exception ( StringUtils . format ( "资源文件({})非法,不允许下载。 " , resource ) ) ;
throw new Exception ( StringUtils . format ( "资源文件({})非法,不允许下载。 " , resource ) ) ;
}
}
// 本地资源路径
// 本地资源路径
@ -154,9 +136,7 @@ public class CommonController
response . setContentType ( MediaType . APPLICATION_OCTET_STREAM_VALUE ) ;
response . setContentType ( MediaType . APPLICATION_OCTET_STREAM_VALUE ) ;
FileUtils . setAttachmentResponseHeader ( response , downloadName ) ;
FileUtils . setAttachmentResponseHeader ( response , downloadName ) ;
FileUtils . writeBytes ( downloadPath , response . getOutputStream ( ) ) ;
FileUtils . writeBytes ( downloadPath , response . getOutputStream ( ) ) ;
}
} catch ( Exception e ) {
catch ( Exception e )
{
log . error ( "下载文件失败" , e ) ;
log . error ( "下载文件失败" , e ) ;
}
}
}
}