Uploading files with common FileUpload

Source: Internet
Author: User
Tags create directory

recently in doing a DRP project, one of the functions is to implement file upload of files! I used to think of Itoo project, but that is . NET , although the idea is the same, but with the technology there are a lot of different! Want to write about it!

Because I have not used the Java upload before, so do not know what to do, and finally from the Internet to find a bit, there are a lot of uploads, and finally determined with Apache common fileUpload to achieve! This article uses the Jsp+servlet to implement the upload and store the files on the server side!

look at the layout of the project, you can see to refer to two files, Commons-fileupload-1.3.1.jar and Commons-io-1.3.2.jar, these two files directly to the official website can be downloaded, very good to find!

let 's take a look at Fileupload.jsp's page code! It is important to note that the form is submitted as a post and the encoding type is enctype="multipart/form-data" encoding format. This example is the implementation of the image upload, so the format of the image is verified, other file types of upload the same authentication method can be, but to change the file suffix name!

<span style= "FONT-SIZE:18PX;" ><%@ page language= "java" contenttype= "text/html; charset=gb18030 "pageencoding=" GB18030 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >


The working process on the servlet side is 1. Create a file resolution object and then make a limit on the size of the uploaded file, get the upload file, and store the file in the folder of the server! Take a look at the servlet code below and use the servlet to configure it in the configuration file! How to configure the details see Baidu! No more introductions here!

<span style= "FONT-SIZE:18PX;" >package com.tgb.test.servlet;import java.io.file;import Java.io.ioexception;import java.io.PrintWriter;import Java.util.iterator;import Java.util.list;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;public class Uploadhandleservlet extends HttpServlet { Private file Uploadpath;private file temppath;public void doget (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {//1 Create file Resolution object Diskfileitemfactory factory = new Diskfileitemfactory (); Factory.setsizethreshold (4096); Factory.setrepository (TempPath); Servletfileupload upload = new Servletfileupload (factory); Upload.setsizemax (1000000 *); try {//Get upload file List fileitems = Upload.parSerequest (Request); String ItemNo = ""; for (Iterator iter = Fileitems.iterator (); Iter.hasnext ();) {Fileitem item = (Fileitem) iter.next ();//is the normal form input domain if (Item.isformfield ()) {if ("ItemNo"). Equals (Item.getfieldname ())) {ItemNo = Item.getstring ();}} Is the input= "type" input field if (!item.isformfield ()) {String fileName = Item.getname (); Long size = Item.getsize (); if (fileName = = NULL | | Filename.equals ("")) && size = = 0) {continue;} Intercept strings such as: C:\WINDOWS\Debug\PASSWD. LogFileName = filename.substring (filename.lastindexof ("\ \") + 1, filename.length ());//Save the file to the specified file Item.write (new file Uploadpath, FileName));}} Response.sendredirect (Request.getcontextpath () + "/index.jsp?flag=" Success! '");} catch (Exception e) {e.printstacktrace ();}} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, response);} public void Init () throws servletexception {//itemmanager= new Itemmanagerimpl (); uploadpath = new File (getservletcontext (). Getrealpath ("UPload ")); System.out.println ("uploadpath=====" + uploadpath),//If the directory does not exist if (!uploadpath.exists ()) {//Create directory Uploadpath.mkdir ();} Temp directory TempPath = new File (Getservletcontext (). Getrealpath ("temp")), if (!temppath.exists ()) {Temppath.mkdir ();}}} </span>

The upload function for such a simple file is complete.  We can also upload multiple files to colleagues, and if you use spring MVC, he also encapsulates a simpler implementation method! You can check out the other blogs! File upload is actually the same implementation ideas! There is no novelty!


Uploading files with common FileUpload

Related Article

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.