Servlet File Upload, httpservletrequestwrapper

Source: Internet
Author: User

Use some jar packages. Commons-fileupload-1.2.2.jar commons-io-1.4.jar

<form action="FileUpload" enctype="multipart/form-data" method="post">  <input type="text" name="name"><br>  <input type="file" name="fileName"><br>  <input type="submit">  </form> 

In this case, the value of name is not obtained because the server obtains an input stream.

System. Out. println (request. getparameter ("name"); // null
System. Out. println (request. getparameter ("FILENAME"); // null

Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html"); printwriter out = response. getwriter (); system. out. println (request. getparameter ("name"); system. out. println (request. getparameter ("FILENAME"); servletfileupload upload = new servletfileupload (); try {fileitemiterator iter = upload. getitemiterator (request); While (ITER. hasnext () {fileitemstream item = ITER. next (); string name = item. getfieldname (); // get the form field, which is an input stream inputstream stream = item. openstream (); // if it is a common form field if (item. isformfield () {string value = streams. asstring (Stream); system. out. println (name + "->" + value);} else {string filename = item. getname (); // obtain the File Upload name system. out. println ("file:" + filename) ;}} catch (fileuploadexception e) {e. printstacktrace ();} system. out. println (request. getinputstream (). available (); out. flush (); out. close ();}

Null
Null
Name-> fdsf
File: hello.zip
0

For normal uploads, you can use:

Streams. Copy (stream, new fileoutputstream (request. getrealpath ("Upload") + "\" + filename), true );
System. Out. println ("CPY:" + request. getrealpath ("Upload") + filename );

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.