【文件属性】:
文件名称:ognl源码包值得用一用
文件大小:2.75MB
文件格式:EXE
更新时间:2012-03-22 13:59:57
ognl
很好的xwork的资源包public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
DiskFileItemFactory factory = new DiskFileItemFactory();
String path = request.getRealPath("/upload");
factory.setRepository(new File(path));
// 1mb
factory.setSizeThreshold(1024 * 1024);
// 实现文件上传的核心
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List list = upload.parseRequest(request);
for (FileItem item : list) {
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString("GBK");
System.out.println("name = " + name);
request.setAttribute(name, value);
} else {
// 文件名的处理
String name = item.getFieldName();
String value = item.getString("GBK");
int start = value.lastIndexOf("\\");
String fileName = value.substring(start, start + 1);
request.setAttribute(name, fileName);
OutputStream os = new FileOutputStream(new File(fileName));
InputStream in = item.getInputStream();
// 写入内存缓冲区
byte[] buf = new byte[1024];
int length = 0;
while ((length = in.read(buf)) > 0) {
os.write(buf, 0, length);
}
os.close();
in.close();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
request.getRequestDispatcher("upload/result3.jsp").forward(request, response);