protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = request.getParameter("filename");
ServletContext context = request.getServletContext();
String realPath = context.getRealPath("/img/"+filename);
FileInputStream fis = new FileInputStream(realPath);
String mimeType = context.getMimeType(filename);
response.setHeader("content-type",mimeType);
response.setHeader("content-disposition","attachment;filename="+filename);
ServletOutputStream sos = response.getOutputStream();
int len=0 ;
byte[] bytes = new byte[1024];
while ((len = fis.read())!=-1){
sos.write(bytes,0,len);
}
fis.close();
} |
|