ブラウザファイルのダウンロードを実現
946 ワード
// ( , , url )
public class ResponseDemo3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = this.getServletContext().getRealPath("/download/ .jpg");
String filename = path.substring(path.lastIndexOf("\\")+1);
// , , URL
response.setHeader("content-disposition","attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
FileInputStream in = new FileInputStream(path);
int len = 0;
byte buffer[] = new byte[1024];
OutputStream out = response.getOutputStream();
while((len=in.read(buffer))>0){
out.write(buffer, 0, len);
}
in.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}