黑马程序员技术交流社区
标题:
//文件下载day04云计算
[打印本页]
作者:
tfy
时间:
2012-12-20 23:28
标题:
//文件下载day04云计算
package com.itheima.servlet;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//文件下载
public class ResponseHeaderServletDemo4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String file=getServletContext().getRealPath("/WEB-INF/ps.jpg");
//通知客户端以下载的方式打开
response.setHeader("Content-Disposition", "attachment;filename=ps.jpg");
InputStream in=new FileInputStream(file);
OutputStream out=response.getOutputStream();
int len=-1;
byte b[]=new byte[1024];
while((len=in.read(b))!=-1){
out.write(b,0,len);
}
in.close();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2