Use Diskfileitemfactory to get the content of the JSP form in action (used in a form with a picture upload feature) __jsp

Source: Internet
Author: User

1. Enctype= "Multipart/form-data" attribute must be added to the <form> label

For example: <form action= "Successcase.do?method=modify2" method= "post" enctype= "Multipart/form-data" onsubmit= "return Check () ">

2. You must have the name attribute in the <input> label

For example: <input type= "text" name= "title" Size= "" id= "title"/>

3. Code in Action:

Boolean p=true;//whether to use the default picture

Diskfileitemfactory factory = new Diskfileitemfactory ();
Servletfileupload upload = new Servletfileupload (factory);
list<fileitem> items = upload.parserequest (request);
Iterator<fileitem> it = Items.iterator ();
while (It.hasnext ()) {
Fileitem Tempitem = It.next ();//Get an element from the form
String itemname = Tempitem.getfieldname ();//Get the Name property value of the input label
if (Tempitem.isformfield ()) {//To determine whether this element type is Text,fckediter also returns true
String content = tempitem.getstring ("GBK");
if (Itemname.equals ("title")) {
Sc.settitle (content);
else if (itemname.equals ("content")) {
Sc.setcontent (content);
}
}else {//type is file, uploaded files
if (tempitem.getname () = "") {//If no upload file, use default file
Continue
}
if (! Filedocumentutil.existdocument (Request
. Getrealpath ("/") + "successimages//")) {
Filedocumentutil.createdocument (Request
. Getrealpath ("/") + "successimages//");
}
File tempfile= new file (Request.getrealpath ("/") + "successimages//" +new file (Tempitem.getname ()). GetName ());
Tempitem.write (Tempfile);
Sc.setpicture (Tempfile.getcanonicalpath ());
P=false;
}
}
if (p) {
Sc.setpicture (Request.getrealpath ("/") + "images//logo1.jpg");
}

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.