apache fileupload后台处理方式
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.jivesoftware.openfire.plugin.db.DbManager;
/******************************************************************
** 文 件 名: Up.java
** Copyright (c) 2007-2011 dhdu@qq.com
** 创 建 人: dhdu@qq.com
** 日 期: 2011-05-04
** 修 改 人:
** 日 期:
** 描 述: upload Servlet
** jsp page <form action="u" method="post" enctype="multipart/form-data" >
** 版 本:
**
** ==============================================================
**
** 修改记录
**
** 版本号 修改编号 修改者 修改日期 修改说明
** ====== ======== ======== ========== ===========
** 0001
** 0002
**
********************************************************************/
public class Up extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void destroy() {
super.destroy();
}
private void up(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String p = "", filename = null;
Map map = new HashMap();
byte[] bytes = null;
try {
// 文件大小限制10M
int maxPostSize = 10 * 1024 * 1024;
// 文件工厂类
DiskFileItemFactory factory = new DiskFileItemFactory();
// factory.setSizeThreshold(4096);
ServletFileUpload upload = new ServletFileUpload(factory);
// upload.setHeaderEncoding("UTF-8");
// upload.setSizeMax(maxPostSize);
List fileItems = upload.parseRequest(request);
if (fileItems != null && fileItems.size() > 0) {
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
if (!"submit".equals(item.getFieldName())) {
p = item.getString("UTF-8");
System.out.println(item.getFieldName() + " :" + p);
map.put(item.getFieldName(), p);
}
} else {
bytes = item.get();
if (bytes.length > 0) {
filename = item.getName();
System.out.println(filename);
// item.write(new File("d:/"+filename));
}
}
}
}
if (bytes != null && bytes.length > 0)
DbManager.getInstance().saveFile(bytes, filename, map);
out.write("ok!!!" + p);
} catch (Exception e) {
e.printStackTrace();
out.write("Exception!!!");
} finally {
}
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
up(req, resp);
}
/*
* ------这个是后台用jsp实现
*
* <% DiskFileItemFactory factory = new DiskFileItemFactory();
* ServletFileUpload upload = new ServletFileUpload(factory); try {List
* items = upload.parseRequest(request);Iterator itr = items.iterator();
* while (itr.hasNext()) {FileItem item = (FileItem) itr.next();if
* (item.isFormField()) { System.out.println("表单参数名:" + item.getFieldName()
* + ",表单参数值:" + item.getString("UTF-8"));} else
* {System.out.println("上传文件的大小:" + item.getSize());
* System.out.println("上传文件的类型:" + item.getContentType());
* System.out.println("上传文件的名称:" + item.getName()); File tempFile = new
* File(item.getName()); File file = new File(request.getRealPath("/") +
* "photofiles", tempFile.getName()); item.write(file); String fname =
* item.getName(); %> <script language="javascript"> alert("上传成功!");
* </script> <% } } } catch (Exception e) { e.printStackTrace(); %> <script
* language="javascript"> alert("上传失败,请重新上传!"); </script> <% }
*/
}
补充:软件开发 , Java ,