单/多文件下载(这个是传统上的下载,是基础用于理解更高层的知识)
package com.poi.testpoi.controller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class FileDown {
@RequestMapping("/singleFileDownload")
@ResponseBody
public String singleFileDownload(HttpServletResponse response) throws FileNotFoundException {
// 单个要下载的文件
File inputFile = new File("C:\\Users\\zy962\\Desktop\\tip说明.txt");
String fileName = "test.txt";
String absolute = "C:\\Users\\zy962\\Desktop\\";
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(inputFile));
bos = new BufferedOutputStream(new FileOutputStream(absolute+fileName));
byte[] buffer = new byte[1024];
int length = 0;
while ((length = bis.read(buffer)) != -1) {
bos.write(buffer, 0, length);
}
bos.flush();
bos.close();
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
return "下载完成,请去相应目录查看";
}
@RequestMapping("/mutliFileDownload")
@ResponseBody
public String mutliFileDownload(HttpServletResponse response) {
File