package com.springmvc.demo;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 下载
* @author Lenovo
*
*/
@Controller
public class DownLoadDemo {
@RequestMapping("/downLoadInfo")
public void downLoadInfo(String luohaoFile,HttpServletResponse response,HttpServletRequest request) throws IOException {
System.out.println("luohaoFile:"+luohaoFile);
//设置响应头信息,以“附件(附带的文件)”的方式下载文件
response.setHeader("Content-Disposition", "attachment;filename="+luohaoFile);
ServletContext sc = request.getServletContext();
String path = sc.getRealPath("lilonglongFile");
System.out.println("path:"+path);
File file = new File(path, luohaoFile);
//用一个工具类,完成下载功能
byte[] bytes = FileUtils.readFileToByteArray(file);
//用文件流的方式输出到浏览器
ServletOutputStream os = response.getOutputStream();
os.write(bytes);
os.flush();
os.close();
}
}