Struts2文件上传(一):Common-FileUpload
2009-09-18 00:00:00 来源:WEB开发网UploadServlet
Java代码
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置临时缓存大小,文件大于这个值的部分保存到临时目录
factory.setSizeThreshold(1024 * 128);
//设置文件保存临时目录
factory.setRepository(new File(
"C:\\Documents and Settings\\bond\\temp"));
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置上传内容大小
upload.setSizeMax(1024 * 1024*512);
List fileItems = upload.parseRequest(request);
for (FileItem fi : fileItems) {
if (!fi.isFormField()) {
fi
.write(new File("D:\\bond\\upload", fi
.getName()));
}
}
request.getRequestDispatcher("success.jsp").forward(request,
response);
} catch (Exception e) {
e.printStackTrace();
request.getRequestDispatcher("failure.jsp").forward(request,
response);
}
更多精彩
赞助商链接