1、单文件下载
@GetMapping("/download")
public void fileDownload(HttpServletResponse response){
String filePath = "D:\\文件下载测试\\实验6打印.docx";
String fileName = "实验6.docx";
File file = new File(filePath);
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);
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()) {
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();
}
}
}
}