Java component FileUpload File upload Demo_java

Source: Internet
Author: User

In our web development, there are many times when we need to upload some of our files to the Web server.

such as: A BBS system, when users use this is the system, can put some pictures of the machine, the document uploaded to the server. Then other users can download the files, so that we can programmatically upload the file, but the better way is to use some existing components to help us achieve this upload function.

Common upload Components :

The Commons fileupload of Apache

Javazoom's Uploadbean

Jspsmartupload

FileUpload Download Address :

http://commons.apache.org/fileupload/

Download: Commons-fileupload-1.2.2-bin.zip Get: Commons-fileupload-1.2.2.jar

http://commons.apache.org/io/

Download: Commons-io-1.4-bin.zip Get: Commons-io-1.4.jar

upload.jsp

Code

 <%@ page language= "java" contenttype= "text/html; charset=utf-8"%> 

Fileuploadservlet.java Code:

Package com.b510.example;
Import Java.io.File;
Import java.io.IOException;


Import java.util.*;
Import Javax.servlet.ServletConfig;
Import Javax.servlet.ServletContext;
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.FileUploadException;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;

Import Org.apache.commons.fileupload.servlet.ServletFileUpload; /** * * * * @author XHW * * * @date 2011-7-26 * * */public class Fileuploadservlet extends HttpServlet {private Stati
 C Final Long serialversionuid = -7744625344830285257l;
 Private ServletContext SC;

 Private String Savepath; public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {dopos
 T (request, response); } public void init (servletconfig config){///Savepath = Config.getinitparameter ("Savepath") of an initialization parameter set in Web.xml;
 sc = Config.getservletcontext ();
 public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
 Request.setcharacterencoding ("UTF-8");
 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 {if (item.getname ()!= null &&!item.getname (). Equals ("")) {System.out.println ("size of upload file:" + item.gets
  Ize ());
  System.out.println ("Type of File uploaded:" + Item.getcontenttype ());

  Item.getname () returns the uploaded file on the client's full path name System.out.println ("Upload file name:" + item.getname ());

  File Tempfile = new file (Item.getname ()); Save path to upload file FilE file = new file (Sc.getrealpath ("/") + Savepath, Tempfile.getname ());
  Item.write (file); Request.setattribute ("Upload.message", "Upload file success!")
  "); }else{Request.setattribute ("Upload.message"), "did not choose to upload files!"
  ");
 }}}catch (Fileuploadexception e) {e.printstacktrace ();
 catch (Exception e) {e.printstacktrace (); Request.setattribute ("Upload.message", "Upload file failed!")
 ");
 } request.getrequestdispatcher ("/uploadresult.jsp"). Forward (request, response);
 }
}

Uploadresult.jsp Code:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <!

DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
 
 

Xml

Code:

<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" xml Ns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http:// Java.sun.com/xml/ns/javaee/web-app_2_5.xsd ' > <servlet> <description>this is the description ' my Java EE c Omponent</description> <display-name>this is the display name of my Java EE component</display-name> < Servlet-name>fileuploadservlet</servlet-name> <servlet-class>com.b510.example.fileuploadservlet </servlet-class>! --Set initialization parameters--> <init-param> <param-name>savePath</param-name> <param-value>uploads</ param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>fileuploadservlet </servlet-name> <url-pattern>/servlet/fileServlet</url-pattern> </servlet-mapping> < Welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>uploadFile.jsp</welcome-file> </
 Welcome-file-list> </web-app>

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.