Unable to get picture items when uploading pictures using Jsp+servlet+commons-fileupload __js

Source: Internet
Author: User

In the process of doing a small project need to use the image upload function, so use a small file upload commons-fileupload components.

The response process is this: in the JSP page through the input tag to get picture file ==> sent to the servlet processing ==>servlet in the validation size, format, etc., and finally write the hard drive and path saved to the Database ==> control page Jump .

Servlet in the processing code I do not mention, and the major sites on the treatment of the same way, nothing more than follow the following steps:

1, the content of the form containing binary stream is obtained through request object in Dopost method;

2, through the factory diskfileitemfactory to build upload object servletfileupload;

3, start their own needs of the business verification (I have verified the file size, whether direct path access, file is not a blank file, whether the specified format);

4, write hard disk/database records


The key code for the front-end JSP page is as follows:

<form action= "test" method= "POST" enctype= "Multipart/form-data" >
	<% session.setattribute
		("UID", " Test ");
	%>
	<input type= "text" name= "user_id" value= "test"/><br/> <input type= 
	"text" Name= "User_ Sex "value=" 1 "/><br/> 
	<input type=" text "name=" letter_anonymous "value=" 1 "/><br/> 
	<input type= "File" accept= "image/*"/><br/> <input type= "Submit" value= "OK"/><br/>
Form>
I do not know if you have found the problem from this code.

The text field in the Binary tab was found to be available in the course of the test, but the contents of the file field are always displayed as empty .

Not that I don't have a choice of files.

Later entangled for a long time did not find the problem where, compared to other people's code to find someone else's file tag added a name attribute (if you want the file can upload, you must define this name property).

The workaround is to define its name attribute in the <input type= "file"/> tag , and become:

<input type= "File" name= "1" accept= "image/*"/>

Principle I still do not understand, want to check.



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.