1、vue代码
downloadF ( rowId, rowName) {
var id = rowId== null ? this . ids : rowId;
console. log ( id. length) ;
var name = rowName== null ? this . names : rowName;
if ( id. length > 1 ) name = "downLoad.zip" ;
downLoadFile ( id) . then ( function ( response) {
const blob= new Blob ( [ response] ) ;
var size = blob. size;
if ( size == 0 ) {
Vue . prototype. $message ( {
message : "没有对应文件!" ,
type: 'warning'
} ) ;
} else {
if ( 'download' in document. createElement ( 'a' ) ) {
const elink = document. createElement ( 'a' ) ;
elink. download = name;
elink. style. display = 'none' ;
elink. href = URL. createObjectURL ( blob) ;
document. body. appendChild ( elink) ;
elink. click ( ) ;
URL. revokeObjectURL ( elink. href) ;
document. body. removeChild ( elink) ;
} else {
navigator. msSaveBlob ( blob, name)
}
}
} , function ( err) {
} ) ;
}
2、接口
export function downLoadFile ( fileIds) {
return request ( {
url: '/eos/upload/download/fileId/' + fileIds,
method: 'get' ,
responseType: 'blob'
} )
}
3、controller层,分为单文件下载和多文件下载
import java. io. * ;
import java. math. BigDecimal ;
import cn. hutool. core. io. FileUtil ;
import cn. hutool. core. util. ZipUtil ;
import java. nio. file. Files ;
import java. nio. file. NoSuchFileException ;
import java. nio. file. Paths ;
import java. util. ArrayList ;
import java. util. Date ;
import java. util. List ;
import com. ruoyi. common. config. RuoYiConfig ;
import com. ruoyi. common. core. domain. entity. SysUser ;
import com. ruoyi. common. utils. DateUtils ;
import com. ruoyi. common. utils. StringUtils ;
import com. ruoyi. common. utils. file. FileUploadUtils ;
import com. ruoyi. common. utils. file. MimeTypeUtils ;
import com. ruoyi. system. service. ISysUserService ;
import org. apache. commons. io. FilenameUtils ;
import org. apache. commons. io. IOUtils ;
import org. apache. commons. lang3. time. DateFormatUtils ;
import org. slf4j. Logger ;
import org. slf4j. LoggerFactory ;
import org. springframework. core. io. ByteArrayResource ;
import org. springframework. http. * ;
import org. springframework. security. access. prepost. PreAuthorize ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. * ;
import com. ruoyi. common. annotation. Log ;
import com. ruoyi. common. core. controller. BaseController ;
import com. ruoyi. common. core. domain. AjaxResult ;
import com. ruoyi. common. enums. BusinessType ;
import com. ruoyi. eos. domain. upload. EosFileUpload ;
import com. ruoyi. eos. service. upload. IEosFileUploadService ;
import com. ruoyi. common. utils. poi. ExcelUtil ;
import com. ruoyi. common. core. page. TableDataInfo ;
import org. springframework. web. multipart. MultipartFile ;
import javax. servlet. http. HttpServletRequest ;
private String FILE_ROOT_PATH = "D:/zipFile" ;
private static String ZIP_PATH = "D:/test.zip" ;
private static int NUM = 1 ;
@GetMapping ( "/download/fileId/{fileIds}" )
public ResponseEntity < ByteArrayResource > downLoadFile ( @PathVariable ( "fileIds" ) ArrayList < Integer > fileIds ,
HttpServletRequest request)
throws Exception {
int size = fileIds. size ( ) ;
NUM = 1 ;
if ( size== 0 || fileIds == null ) {
throw new Exception ( "下载文件id数组为空!" ) ;
}
if ( size == 1 ) {
return downLoadOne ( fileIds. get ( 0 ) ) ;
} else {
FileUtil . del ( new File ( FILE_ROOT_PATH) ) ;
for ( int j = 0 ; j< size ; j++ ) {
EosFileUpload eosFileUpload = eosFileUploadService. selectEosFileUploadByFileId ( ( long ) fileIds. get ( j) ) ;
String fileName = eosFileUpload. getFileName ( ) ;
String saveName = eosFileUpload. getFileNameSaved ( ) ;
String filePath = lastThirdCharString ( eosFileUpload. getFilePath ( ) ) ;
String downPath = RuoYiConfig . getUploadPath ( ) + File . separator+ filePath+ File . separator+ saveName;
System . out. println ( downPath) ;
Long downloadCount= eosFileUpload. getDownloadCount ( ) ;
eosFileUpload. setDownloadDate ( new Date ( ) ) ;
eosFileUpload. setDownloadCount ( downloadCount+ 1 ) ;
byte [ ] data = Files . readAllBytes ( Paths . get ( downPath) ) ;
writeBytes ( data, FILE_ROOT_PATH, fileName) ;
eosFileUploadService. updateEosFileUpload ( eosFileUpload) ;
}
String zipPath = downZIP ( FILE_ROOT_PATH) ;
byte [ ] data = Files . readAllBytes ( Paths . get ( zipPath) ) ;
ByteArrayResource resource = new ByteArrayResource ( data) ;
FileUtil . del ( new File ( FILE_ROOT_PATH) ) ;
FileUtil . del ( new File ( ZIP_PATH) ) ;
return ResponseEntity . ok ( )
. header ( HttpHeaders . CONTENT_DISPOSITION)
. contentType ( MediaType . APPLICATION_OCTET_STREAM)
. contentLength ( data. length)
. body ( resource) ;
}
}
4、一些需要的方法
public static void writeBytes ( byte [ ] data, String uploadDir, String fileName) throws IOException
{
FileOutputStream fos = null ;
String pathName = "" ;
String fileSuffix = getSuffix ( fileName) ;
try
{
pathName = DateUtils . datePath ( ) + "/" + fileName;
File file = getAbsoluteFile ( uploadDir, pathName) ;
fos = new FileOutputStream ( file) ;
fos. write ( data) ;
}
finally
{
IOUtils . close ( fos) ;
}
}
public static String downZIP ( String s) {
File file = ZipUtil . zip ( s, ZIP_PATH) ;
return file. getAbsolutePath ( ) ;
}
public static String getSuffix ( String fileName) {
StringBuffer stringBuffer = new StringBuffer ( fileName) ;
int index = stringBuffer. indexOf ( "." ) ;
return stringBuffer. substring ( index, stringBuffer. length ( ) ) ;
}
public static String getPrefix ( String fileName) {
StringBuffer stringBuffer = new StringBuffer ( fileName) ;
int index = stringBuffer. indexOf ( "." ) ;
return stringBuffer. substring ( 0 , index) ;
}
public static final File getAbsoluteFile ( String uploadDir, String fileName) throws IOException
{
File desc = new File ( uploadDir + File . separator + fileName) ;
if ( ! desc. exists ( ) )
{
if ( ! desc. getParentFile ( ) . exists ( ) )
{
desc. getParentFile ( ) . mkdirs ( ) ;
}
} else {
desc = new File ( uploadDir + File . separator + getPrefix ( fileName) + "(" + NUM+ ")" + getSuffix ( fileName) ) ;
NUM++ ;
}
return desc;
}
5、单文件下载
public ResponseEntity < ByteArrayResource > downLoadOne ( int fileId) throws Exception {
EosFileUpload eosFileUpload = eosFileUploadService. selectEosFileUploadByFileId ( ( long ) fileId) ;
String fileName = eosFileUpload. getFileName ( ) ;
String saveName = eosFileUpload. getFileNameSaved ( ) ;
String filePath = lastThirdCharString ( eosFileUpload. getFilePath ( ) ) ;
String downPath = RuoYiConfig . getUploadPath ( ) + File . separator+ filePath+ File . separator+ saveName;
System . out. println ( downPath) ;
Long downloadCount= eosFileUpload. getDownloadCount ( ) ;
eosFileUpload. setDownloadDate ( new Date ( ) ) ;
eosFileUpload. setDownloadCount ( downloadCount+ 1 ) ;
try {
byte [ ] data = Files . readAllBytes ( Paths . get ( downPath) ) ;
ByteArrayResource resource = new ByteArrayResource ( data) ;
eosFileUploadService. updateEosFileUpload ( eosFileUpload) ;
return ResponseEntity . ok ( )
. header ( HttpHeaders . CONTENT_DISPOSITION)
. contentType ( MediaType . APPLICATION_OCTET_STREAM)
. contentLength ( data. length)
. body ( resource) ;
} catch ( NoSuchFileException e) {
return new ResponseEntity ( "" , HttpStatus . OK) ;
}
}