public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String finame = "劳斯莱斯.jpg";
finame = URLEncoder.encode(finame, "utf-8");
response.setHeader("content-disposition", "attachment;filename="+ finame);
// getServletContext().getRealPath(相对于web应用的路径);此方法获得相对于web的绝对路径
/*
* String path = getServletContext().getRealPath("/a.jpg");
*
* InputStream in = new FileInputStream(path);
*/
// InputStream in =super.getServletContext().getResourceAsStream(相对于web应用的路径);此方法返回读取相对于该路径文件的IO流
InputStream in = super.getServletContext()
.getResourceAsStream("/a.jpg");
OutputStream out = response.getOutputStream();
int len;
byte[] buf = new byte[1024];
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
}