Apache fileupload background processing method

Source: Internet
Author: User

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;
/*************************************** ***************************
** File name: Up. java
** Copyright (c) 2007-2011 dhdu@qq.com
** Creator: dhdu@qq.com
** Date:
** Modifier:
** Date:
** Description: upload Servlet
** Jsp page <form action = "u" method = "post" enctype = "multipart/form-data">
** Version:
**
** ===================================================== ======================================
**
** Modify records
**
** Version number modifier modification date modification description
** ===================================================== =====
* 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 {
// The file size is limited to 10 MB.
Int maxPostSize = 10*1024*1024;
// File factory type
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 );
}
 
/*
* ------ This is implemented using jsp in the background.
*
* <% 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 ("form parameter name:" + item. getFieldName ()
* + ", Form parameter value:" + item. getString ("UTF-8");} else
* {System. out. println ("Size of the uploaded file:" + item. getSize ());
* System. out. println ("Upload file type:" + item. getContentType ());
* System. out. println ("Name of the uploaded File:" + 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 ("uploaded successfully! ");
* </Script> <% }}} catch (Exception e) {e. printStackTrace (); %> <script
* Language = "javascript"> alert ("Upload Failed. please upload again! "); </Script> <%}
*/
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.