SpringBoot实现文件下载,多文件打包下载
【代码】SpringBoot实现文件下载,多文件打包下载。
·
1、单文件下载
//文件下载测试
@GetMapping("/download")
public void fileDownload(HttpServletResponse response){
//文件路径
String filePath = "D:\\文件下载测试\\实验6打印.docx";
//文件名
String fileName = "实验6.docx";
File file = new File(filePath);
//设置headers
response.setHeader("Content-Type","application/octet-stream");
//设置下载文件的名称,解决中文文件名乱码问题
response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName, StandardCharsets.UTF_8));
//创建流
FileInputStream is = null;
BufferedInputStream bs = null;
ServletOutputStream os = null;
//流到客户端
try{
is = new FileInputStream(file);
bs =new BufferedInputStream(is);
os = response.getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = bs.read(buffer)) != -1){
os.write(buffer,0,len);
}
}catch (IOException ex){
ex.printStackTrace();
}finally {
try{
if(is != null){
is.close();
}
if( bs != null ){
bs.close();
}
if( os != null){
os.flush();
os.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
2、多文件下载
//文件打包下载测试
@GetMapping("/download/package")
public void compressedDownload(HttpServletResponse response){
//伪造文件名数组
String[] names = {"10.png","实验报告.docx"};
//伪造文件所在路径数组
String[] paths = {"D:\\文件下载测试\\6z10.png","D:\\文件下载测试\\实验6打印.docx"};
//创建临时目录以存放生成的压缩包文件
String directory = "D:\\文件压缩包临时目录";
File directoryFile = new File(directory);
//设置最终输出zip文件的目录+文件名
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
String zipFileName = formatter.format(new Date())+".zip";
String strZipPath = directory+"\\"+zipFileName;
//创建流
ZipOutputStream zipStream = null;
FileInputStream zipSource = null;
BufferedInputStream bufferStream = null;
File zipFile = new File(strZipPath);
try{
//构造最终压缩包的输出流
zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
for(int i = 0; i<paths.length ;i++){
//解码获取真实路径与文件名
String realFileName = java.net.URLDecoder.decode(names[i],"UTF-8");
String realFilePath = java.net.URLDecoder.decode(paths[i],"UTF-8");
File file = new File(realFilePath);
if(file.exists()){
zipSource = new FileInputStream(file);//将需要压缩的文件格式化为输入流
ZipEntry zipEntry = new ZipEntry(realFileName);//在压缩目录中文件的名字
zipStream.putNextEntry(zipEntry);//定位该压缩条目位置,开始写入文件到压缩包中
bufferStream = new BufferedInputStream(zipSource, 1024 * 10);
int read = 0;
byte[] buf = new byte[1024 * 10];
while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1){
zipStream.write(buf, 0, read);
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally {
//关闭流
try{
if(null != bufferStream) bufferStream.close();
if(null != zipStream){
zipStream.flush();
zipStream.close();
}
if(null != zipSource) zipSource.close();
}catch (IOException exception){
exception.printStackTrace();
}
}
if(zipFile.exists()){
downImg(response,zipFileName,strZipPath);
zipFile.delete();
}
}
public void downImg(HttpServletResponse response,String filename,String path ){
if (filename != null) {
FileInputStream is = null;
BufferedInputStream bs = null;
OutputStream os = null;
try {
File file = new File(path);
if (file.exists()) {
//设置Headers
response.setHeader("Content-Type","application/octet-stream");
//设置下载的文件的名称-该方式已解决中文乱码问题
response.setHeader("Content-Disposition","attachment;filename=" + new String( filename.getBytes("gb2312"), "ISO8859-1" ));
is = new FileInputStream(file);
bs =new BufferedInputStream(is);
os = response.getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while((len = bs.read(buffer)) != -1){
os.write(buffer,0,len);
}
}else{
String error = Base64Util.encode("下载的文件资源不存在");
response.sendRedirect("/imgUpload/imgList?error="+error);
}
}catch(IOException ex){
ex.printStackTrace();
}finally {
try{
if(is != null){
is.close();
}
if( bs != null ){
bs.close();
}
if( os != null){
os.flush();
os.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}
更多推荐
已为社区贡献5条内容
所有评论(0)