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.