public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String name = request.getParameter("name");
String type = request.getParameter("type");
String dir = request.getParameter("dir");
if (name == null name.length() < 2 dir == null dir.length() < 1 type == null type.length() < 1) {
throw new ServletException("Sorry,error occured");
}
char ch = dir.charAt(dir.length() - 1);
if (ch != '/' ch != '/')
dir = dir + "/";
ServletOutputStream os = null;
BufferedInputStream bis = null;
try {
File file = new File(dir + name);
if (!file.exists() file.length() >= Integer.MAX_VALUE) {
logger.error("Invalid file or file to large,file: " + name);
throw new ServletException(
"Invalid file or file to large,file: " + name);
}
response.setContentType("
application/" + type);
response.addHeader("Content-Disposition", "attachment; filename="+ name);
response.setContentLength((int) file.length());
os = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(file));
int size = -1;
while ((size = bis.read()) != -1)
os.write(size);
} catch (IOException ioe) {
throw new ServletException(ioe.getMessage());
} finally {
if (os != null)
os.close();
if (bis != null)
bis.close();
}
}