文件下载关键代码
String filename = request.getParameter("filename");
//得到想客服端输出的输出流
OutputStream outputStream = response.getOutputStream();
//输出文件用的字节数组,每次向输出流发送600个字节
byte b[] = new byte[600];
//要下载的文件
File fileload=new File("C://ExportExecl/"+filename+"");
//客服端使用保存文件的对话框
response.addHeader("content-disposition", "attachment;filename="
+ java.net.URLEncoder.encode(filename, "UTF-8"));
//通知客服文件的MIME类型
response.setContentType(getServletContext().getMimeType(filename));
//通知客服文件的长度
long fileLength = fileload.length();
String length = String.valueOf(fileLength);
response.setHeader("Content_length", length);
//读取文件,并发送给客服端下载
FileInputStream inputStream = new FileInputStream(fileload);
int n = 0;
while((n=inputStream.read(b))!=-1){
outputStream.write(b,0,n);
}
//清除缓存
if(fileload.exists()){
fileload.delete();
System.out.println("已清除服务器缓存!");
}