apache fileupload後臺處理方式 – JAVA編程語言程序開發技術文章

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> <% }
  */
}

發佈留言