实现文件批量下载
This commit is contained in:
@@ -4,6 +4,7 @@ import com.fjy.spring.domain.TbFile;
|
||||
import com.fjy.spring.domain.TbUser;
|
||||
import com.fjy.spring.enums.ResultEnum;
|
||||
import com.fjy.spring.exception.UserException;
|
||||
import com.fjy.spring.properties.ServerProperties;
|
||||
import com.fjy.spring.service.FileService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
@@ -16,12 +17,19 @@ import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static com.fjy.spring.constant.GlobalConstant.USER_SESSION_KEY;
|
||||
|
||||
@Controller
|
||||
public class DownLoadController {
|
||||
@Autowired
|
||||
private ServerProperties serverProperties;//服务器配置信息
|
||||
|
||||
@Autowired
|
||||
private FileService fileService;
|
||||
|
||||
@@ -112,4 +120,144 @@ public class DownLoadController {
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入课程名和文件夹名称,打包下载目录下所有文件
|
||||
* @param courseName
|
||||
* @param folder
|
||||
* @param response
|
||||
*/
|
||||
@GetMapping("/download/downloadzip")
|
||||
public void batDownload(@RequestParam(value = "courseName") String courseName,
|
||||
@RequestParam(value = "Folder")String folder, HttpServletResponse response){
|
||||
//获取文件夹名称
|
||||
String paths = serverProperties.getFilePath()+ "upload/"+courseName+"/"+folder;
|
||||
String zipPath = serverProperties.getFilePath();
|
||||
|
||||
List<String> pathList = new ArrayList<String>();
|
||||
pathList=getFileString(paths);
|
||||
//需要压缩的文件--包括文件地址和文件名
|
||||
String []path =(String[])pathList.toArray(new String[0]);
|
||||
// 要生成的压缩文件地址和文件名称
|
||||
String zipFileName=courseName+folder+".zip";
|
||||
String desPath = zipPath+"\\"+zipFileName;
|
||||
System.out.println("打包文件存储地址:"+desPath);
|
||||
|
||||
File zipFile = new File(desPath);
|
||||
ZipOutputStream zipStream = null;
|
||||
FileInputStream zipSource = null;
|
||||
BufferedInputStream bufferStream = null;
|
||||
try {
|
||||
//构造最终压缩包的输出流
|
||||
zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
|
||||
for(int i =0;i<path.length;i++){
|
||||
File file = new File(path[i]);
|
||||
//将需要压缩的文件格式化为输入流
|
||||
zipSource = new FileInputStream(file);
|
||||
//压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
|
||||
ZipEntry zipEntry = new ZipEntry(file.getName());
|
||||
//定位该压缩条目位置,开始写入文件到压缩包中
|
||||
|
||||
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.close();
|
||||
if(null != zipSource) zipSource.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
//将打包好的文件输出到客户端
|
||||
java.io.BufferedInputStream bis = null;
|
||||
java.io.BufferedOutputStream bos = null;
|
||||
try {
|
||||
long fileLength = new File(desPath).length();
|
||||
response.setContentType("application/x-msdownload;");
|
||||
response.setHeader("Content-disposition", "attachment; filename=" + new String(zipFileName.getBytes("utf-8"), "ISO8859-1"));
|
||||
response.setHeader("Content-Length", String.valueOf(fileLength));
|
||||
bis = new BufferedInputStream(new FileInputStream(desPath));
|
||||
bos = new BufferedOutputStream(response.getOutputStream());
|
||||
byte[] buff = new byte[2048];
|
||||
int bytesRead;
|
||||
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
|
||||
bos.write(buff, 0, bytesRead);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (bis != null)
|
||||
try {
|
||||
bis.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (bos != null)
|
||||
try {
|
||||
bos.close();
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取目录下所有文件的路径
|
||||
* @param fileDir
|
||||
* @return
|
||||
*/
|
||||
public List<String> getFileString(String fileDir) {
|
||||
List<File> fileList = new ArrayList<File>();
|
||||
File file = new File(fileDir);
|
||||
File[] files = file.listFiles();// 获取目录下的所有文件或文件夹
|
||||
List<String> path = new ArrayList<String>();
|
||||
List<String> name = new ArrayList<String>();
|
||||
int i = 0,j=0;
|
||||
if (files == null) {// 如果目录为空,直接退出
|
||||
path.add("空目录");
|
||||
return path;
|
||||
}
|
||||
// 遍历,目录下的所有文件
|
||||
for (File f : files) {
|
||||
if (f.isFile()) {
|
||||
fileList.add(f);
|
||||
} else if (f.isDirectory()) {
|
||||
System.out.println(f.getAbsolutePath());
|
||||
path.add(f.getAbsolutePath());
|
||||
getFileString(f.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
for (File f1 : fileList) {
|
||||
System.out.println(f1.getName());
|
||||
path.add(f1.getAbsolutePath());
|
||||
/*name[j]=f1.getName();j++;*/
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/* public static void main(String[] args) {
|
||||
List<String> path = new ArrayList<String>();
|
||||
path=getFileString("F:\\JAVA Workspace\\Temp\\upload\\信息安全\\第一次作业");
|
||||
Iterator<String> it1 = path.iterator();
|
||||
while(it1.hasNext()){
|
||||
System.out.println(it1.next());
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ server:
|
||||
serverproperties:
|
||||
port_num: 8080
|
||||
filePath: F:\JAVA Workspace\Temp\
|
||||
zipfilePath: F:\JAVA Workspace\Temp\zip\
|
||||
spring:
|
||||
thymeleaf:
|
||||
prefix: classpath:/templates/
|
||||
|
||||
@@ -109,6 +109,11 @@ var Main = {
|
||||
.catch(_ => {
|
||||
});
|
||||
},
|
||||
handleDownload(row) {
|
||||
/*var url = window.location.protocol+"://"+window.location.host+":"+window.location.port+"/"*/
|
||||
window.open(getRootPath_web()+"/download/downloadzip?courseName="
|
||||
+ row.courseName+"&Folder="+row.folder);
|
||||
},
|
||||
handleSelect(key, keyPath) {
|
||||
console.log(key, keyPath);
|
||||
},
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.fjy.spring.controller;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class DownLoadControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
@Test
|
||||
public void batDownload() throws Exception{
|
||||
mvc.perform(MockMvcRequestBuilders.get("/download/downloadzip")
|
||||
.param("courseName","信息安全").param("Folder","第一次作业"))
|
||||
.andExpect(MockMvcResultMatchers.status().isOk());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user