JSP page/apache The method of passing other parameters while the FileUpload component uploads the file

Source: Internet
Author: User

Spring3.0+apache tomcatv7.0+jdk1.7.0_u45+j2ee1.4 validation passed.

Requirement: When uploading a file using a JSP page, you need to pass the relevant parameters as well.

1, need to set the form to Enctype= "Multipart/form-data" method= "post" parameters;

2, because the URL parameter can not be obtained through request.getparameter (), so in order to let the @RequestMapping set the params="command=upload" condition to take effect, You need to add parameters directly to the action in the form action="Item.do?command=upload";

3. When the controller is processing, use item. Isformfield (),item. GetFieldName (); item. getString () To get the form field property name and parameter;

JAVA class

import org.apache.commons.fileupload.DiskFileUpload;

import Org.apache.commons.fileupload.FileItem;

@RequestMapping (Value = "Item", params = "Command=upload", method = Requestmethod.) POST)

Public String Upload (itemactionformform, httpservletrequest request,

HttpServletResponse response) throws Exception {

String fullfilename =null;

Get Real Absolute path

String Realpath = request. GetSession (). Getservletcontext ()

. Getrealpath ("/images");

System. out. println ("realpath=" +Realpath);

Diskfileupload Disk =new diskfileupload ();

Try {

list<fileitem> Files =(list<fileitem>) disk. Parserequest (request) ;

for (FileitemItem : files) {

//Parse the parameter

if (item. Isformfield ()) {

System. out. println ("Item.isformfield ()");

String Field =Item. GetFieldName ();

if (field ! =null) {

if (field. Equalsignorecase ("ItemNo")) {

ItemNo = item. getString ();

System. out. println ("itemno=" +ItemNo);

}

}

}

//Parse the file data

fullfilename =item. GetName ();

if (fullfilename = =null) {

System. out. println ("Fullfilename not existed.");

continue;

}

Retrive the filename from full pathname.

String fileName1 =fullfilename. substring (fullfilename

. lastIndexOf ("\ \") + 1);

File file1 =new file (realpath,fileName1);

file1. Getparentfile (). Mkdirs ();

file1. CreateNewFile ();

InputStream INS =item. getInputStream ();

OutputStream ous =new fileoutputstream (file1);

Try {

byte []buffer = newbyte[1024];

int len = 0;

while (len =ins. Read (buffer)) >-1)

OUs. Write (buffer, 0,Len);

} finally {

OUs. Close ();

ins. Close ();

}

}

} Catch (Exceptione) {

// TODO auto-generated Catch block

e. Printstacktrace ();

}

return "Redirect:item.do";

}

JSP page:

< form name = "Itemform" id = " Itemform " Action = "item.do?command=upload" Enctype = "Multipart/form-data" Method = "post" >

      < Input type = "hidden" name = "ItemNo" value = " ${item.itemno} " >

< input name="Itemfile"type="File"class="Text1"size= "Max" maxlength=">" (Must be. GIF or.jpg file)


JSP page/apache The method of passing other parameters while the FileUpload component uploads the file

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.