Servlet supports uploading multiple images

Source: Internet
Author: User

The first front-end form is this form:

<form target= "_self" method= "post" action= "Fileuploadmultyservlet" enctype= "Multipart/form-data" >

<label> upload material <input type= "file" id= "image" Name= ' image ' ></label>
<label> upload material <input type= "file" id= "Image1" Name= ' Image1 ' ></label>
<label> upload material <input type= "file" id= "Image2" Name= ' Image2 ' ></label>
<input type= "Submit" class= "Btn-u" value= "Commit" >

</form>

Then upload the image with the servlet code as follows:

public class Fileuploadmultyservlet extends HttpServlet {

Private static final long serialversionuid = 1L;
Private File Uploadpath;
Private File TempPath;

@Override
public void Init () throws Servletexception {

Uploadpath = new File (Getservletcontext (). Getrealpath ("upload"));
System.out.println ("uploadpath=====" + Uploadpath);
If the directory does not exist
if (!uploadpath.exists ()) {
Create a Directory
Uploadpath.mkdir ();
}

Temp directory
File Tempfile = new file (Item.getname ()) constructs a temporary object
TempPath = new File (Getservletcontext (). Getrealpath ("temp"));
if (!temppath.exists ()) {
Temppath.mkdir ();
}

If you do not show the call to the parent class method, there will be no itemmanager instance, so it will cause a null pointer
Super.init ();

}

public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
DoPost (request, response);
}

@SuppressWarnings ("deprecation")
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

Fetch data from item_upload.jsp, because the encoding format of the upload page is different from the general one, using enctype= "Multipart/form-data"
form submission using Multipart/form-data, unable to obtain data using Req.getparameter ()
String ItemNo = Req.getparameter ("ItemNo");
System.out.println ("itemno======" + ItemNo);

/******************************** using the FileUpload component to parse the form ********************/
Boolean ismultipart = servletfileupload.ismultipartcontent (request);
Diskfileitemfactory factory = new Diskfileitemfactory ();
Factory.setsizethreshold (4096);
Factory.setrepository (TempPath);
Servletfileupload upload = new Servletfileupload (factory);
Upload.setheaderencoding ("Utf-8");
Maximum size before a fileuploadexception'll be thrown
Upload.setsizemax (1000000 * 20);

Try {
list<?> items = upload.parserequest (request);
String data = "";
int i = 0;
String ItemNo = "";
String fileName = "";
Iterator iter = Items.iterator ();
while (Iter.hasnext ()) {
Fileitem item = (Fileitem) iter.next ();
if (Item.isformfield ()) {
String name = Item.getfieldname ();
i++;
data = item.getstring ();
if ("ItemNo". Equals (Item.getfieldname ())) {
ItemNo = item.getstring ();
}
}
//Is input= "type" input domain
Else if (!item.isformfield ()) {
//upload file name and full path
FileName = Item.getname ();
System.out.println ("filename===" +filename);
Long size = Item.getsize ();
//Determines whether the file is selected
if (fileName = = NULL | | filename.equals (")) && size = = 0) {
continue;
} else{
FileName = system.currenttimemillis () + ". jpg";
}
//intercept string such as: C:\WINDOWS\Debug\PASSWD. LOG
FileName = filename.substring (
filename.lastindexof ("\ \") + 1, filename.length ());

Save the file on the server's physical disk: The first parameter is: Full path (excluding file name) the second parameter is: file name
Item.write (file);
The modified file name and item name are consistent, and you have forcibly modified the filename extension gif
Item.write (New File (Uploadpath, ItemNo + ". gif"));
Save the file to the directory without modifying the filename
Item.write (New File (Uploadpath, fileName));
}
}
Response.sendredirect (Request.getcontextpath () + "/people_require.html");
} catch (Exception e) {
E.printstacktrace ();
}

}
}

Finally, you can see three images in the upload under your Tomcat project path, with the following effects:

Servlet supports uploading multiple images

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.