任何非压缩格式下载
package com. pisx. pd. eco. util ; import java. io. * ;
import java. util. Collections ;
import java. util. HashMap ;
import java. util. Map ; import javax. servlet. ServletOutputStream ;
import javax. servlet. http. HttpServletResponse ; import org. springframework. web. multipart. MultipartFile ; import com. pisx. pd. commom. utils. FileSizeUnitTransform ;
import com. pisx. pd. eco. config. FilePathConfig ; import lombok. extern. slf4j. Slf4j ; @Slf4j
public class FileUtils { public static String downloadFile ( HttpServletResponse response, String fileName, String filePath) { InputStream inStream = null ; FileInputStream fis = null ; ServletOutputStream servletOs = null ; try { File file = new File ( filePath, fileName) ; if ( file. exists ( ) ) { response. reset ( ) ; response. setContentType ( "application/x-msdownload" ) ; response. addHeader ( "Content-Disposition" , "attachment; filename=\"" + fileName + "\"" ) ; int fileLength = ( int ) file. length ( ) ; response. setContentLength ( fileLength) ; if ( fileLength != 0 ) { fis = new FileInputStream ( file) ; inStream = new BufferedInputStream ( fis) ; byte [ ] buf = new byte [ 4096 ] ; servletOs = response. getOutputStream ( ) ; int readLength; while ( ( ( readLength = inStream. read ( buf) ) != - 1 ) ) { servletOs. write ( buf, 0 , readLength) ; } } return "下载成功" ; } else { return "文件不存在" ; } } catch ( Exception e) { e. printStackTrace ( ) ; return "下载文件出错" ; } finally { if ( inStream != null ) { try { fis. close ( ) ; inStream. close ( ) ; } catch ( IOException e) { log. info ( e. getMessage ( ) ) ; } } if ( servletOs != null ) { try { servletOs. flush ( ) ; servletOs. close ( ) ; } catch ( IOException e) { log. info ( e. getMessage ( ) ) ; } } } } public static String downloadFile ( HttpServletResponse response, InputStream inputStream, String fileName) { ServletOutputStream servletOs = null ; try { response. reset ( ) ; response. setContentType ( "application/x-msdownload" ) ; response. addHeader ( "Content-Disposition" , "attachment; filename=\"" + fileName + "\"" ) ; byte [ ] buf = new byte [ 4096 ] ; servletOs = response. getOutputStream ( ) ; int readLength; while ( ( ( readLength = inputStream. read ( buf) ) != - 1 ) ) { servletOs. write ( buf, 0 , readLength) ; } return "下载成功" ; } catch ( Exception e) { e. printStackTrace ( ) ; return "下载文件出错" ; } finally { if ( servletOs != null ) { try { servletOs. flush ( ) ; servletOs. close ( ) ; } catch ( IOException e) { log. info ( e. getMessage ( ) ) ; } } } } public static Map < String , String > uploadFile ( MultipartFile file, String filePath, String fileName) { String fileUrl = filePath + fileName; String fileSize = FileSizeUnitTransform. GetFileSize ( file. getSize ( ) ) ; int index = fileName. lastIndexOf ( "." ) ; String fileFormat = fileName. substring ( index + 1 ) ; File filed = new File ( FilePathConfig . PATH + fileUrl) ; if ( ! filed. getParentFile ( ) . exists ( ) ) { boolean mkdirs = filed. getParentFile ( ) . mkdirs ( ) ; if ( Boolean . FALSE . equals ( mkdirs) ) { return Collections . emptyMap ( ) ; } } try { file. transferTo ( filed) ; } catch ( Exception ex) { log. error ( ex. getMessage ( ) ) ; } Map < String , String > map = new HashMap < > ( 3 ) ; map. put ( "fileUrl" , fileUrl) ; map. put ( "fileSize" , fileSize) ; map. put ( "fileFormat" , fileFormat) ; return map; } private FileUtils ( ) { throw new IllegalStateException ( "Utility class" ) ; }
}
压缩包格式下载
package com. pisx. pd. eco. util ; import com. pisx. pd. commom. utils. FileSizeUnitTransform ;
import com. pisx. pd. datasource. lib. entity. eco. CarbonMore ;
import com. pisx. pd. datasource. lib. entity. eco. MineFileDto ;
import com. pisx. pd. datasource. lib. entity. eco. StatementTemplate ;
import com. pisx. pd. eco. config. FilePathConfig ;
import org. springframework. web. multipart. MultipartFile ;
import javax. annotation. Nullable ;
import javax. servlet. http. HttpServletResponse ;
import java. io. * ;
import java. net. URLEncoder ;
import java. util. ArrayList ;
import java. util. List ;
import java. util. Map ;
import java. util. zip. ZipEntry ;
import java. util. zip. ZipOutputStream ; public class UploadFileUtil { static class FileContent { String fileUrl = null ; String fileName = null ; String filePath = null ; String fileSize = null ; String fileFormat = null ; public String getFileUrl ( ) { return fileUrl; } public void setFileUrl ( String fileUrl) { this . fileUrl = fileUrl; } public String getFileName ( ) { return fileName; } public void setFileName ( String fileName) { this . fileName = fileName; } public String getFilePath ( ) { return filePath; } public void setFilePath ( String filePath) { this . filePath = filePath; } public String getFileSize ( ) { return fileSize; } public void setFileSize ( String fileSize) { this . fileSize = fileSize; } public String getFileFormat ( ) { return fileFormat; } public void setFileFormat ( String fileFormat) { this . fileFormat = fileFormat; } public void setExceptFileFormatName ( String substring) { } } public static List < FileContent > uploadFileUtil ( MultipartFile [ ] files, String fileUrl) { if ( files != null && files. length > 0 ) { try { List < FileContent > list = new ArrayList < > ( ) ; for ( MultipartFile item : files) { FileContent f = new FileContent ( ) ; f. setFileName ( item. getOriginalFilename ( ) ) ; f. setFileSize ( FileSizeUnitTransform. GetFileSize ( item. getSize ( ) ) ) ; int index = item. getOriginalFilename ( ) . lastIndexOf ( "." ) ; f. setExceptFileFormatName ( item. getOriginalFilename ( ) . substring ( 0 , index) ) ; f. setFileName ( item. getOriginalFilename ( ) . substring ( index + 1 ) ) ; f. setFileUrl ( fileUrl) ; f. setFilePath ( fileUrl + item. getOriginalFilename ( ) ) ; list. add ( f) ; File file = new File ( fileUrl + item. getOriginalFilename ( ) ) ; if ( ! file. getParentFile ( ) . exists ( ) ) { file. getParentFile ( ) . mkdirs ( ) ; } } return list; } catch ( Exception e) { e. printStackTrace ( ) ; } } return null ; } public static byte [ ] getPackage ( MineFileDto file) { byte [ ] bag = null ; try { String filePath = file. getFile_path ( ) ; bag = getBytesByFile ( filePath) ; } catch ( Exception e) { e. printStackTrace ( ) ; } return bag; } public static byte [ ] getPackageCarbonMore ( CarbonMore file) { byte [ ] bag = null ; try { String filePath = file. getFile_path ( ) ; bag = getBytesByFile ( filePath) ; } catch ( Exception e) { e. printStackTrace ( ) ; } return bag; } public static byte [ ] getPackages ( String filePath) { byte [ ] bag = null ; try { bag = getBytesByFile ( filePath) ; } catch ( Exception e) { e. printStackTrace ( ) ; } return bag; } @Nullable private static byte [ ] getBytesByFile ( String filePath) { try { File file = new File ( FilePathConfig . PATH + filePath) ; FileInputStream fis = new FileInputStream ( file) ; ByteArrayOutputStream bos = new ByteArrayOutputStream ( 1024 ) ; byte [ ] b = new byte [ 1024 ] ; int n; while ( ( n = fis. read ( b) ) != - 1 ) { bos. write ( b, 0 , n) ; } fis. close ( ) ; byte [ ] data = bos. toByteArray ( ) ; bos. close ( ) ; return data; } catch ( Exception e) { e. printStackTrace ( ) ; } return null ; } public static void downloadBatchByFile ( HttpServletResponse response, Map < String , byte [ ] > files, String zipName) { try { response. setContentType ( "application/x-msdownload" ) ; response. setHeader ( "content-disposition" , "attachment;filename=" + URLEncoder . encode ( zipName, "UTF-8" ) ) ; ZipOutputStream zos = new ZipOutputStream ( response. getOutputStream ( ) ) ; BufferedOutputStream bos = new BufferedOutputStream ( zos) ; for ( Map. Entry < String , byte [ ] > entry : files. entrySet ( ) ) { String fileName = entry. getKey ( ) ; byte [ ] file = entry. getValue ( ) ; BufferedInputStream bis = new BufferedInputStream ( new ByteArrayInputStream ( file) ) ; zos. putNextEntry ( new ZipEntry ( fileName) ) ; int len = 0 ; byte [ ] buf = new byte [ 10 * 1024 ] ; while ( ( len = bis. read ( buf, 0 , buf. length) ) != - 1 ) { bos. write ( buf, 0 , len) ; } bis. close ( ) ; bos. flush ( ) ; } bos. close ( ) ; } catch ( Exception e) { e. printStackTrace ( ) ; } }
}