File Upload-Servlet implementation

Source: Internet
Author: User

1 m = 1024*1024 bytes --> about 1000000, so 1 m is about 1000000 bytes.
10 m = 1024*1024*10 bytes

Note:
Set the HTML form: method = "Post ",
Enctype = "multipart/form-Data", you cannot use request. getparameter () in Java to obtain data.
Image Upload and storage problems:
1. The file name is the same as the item name.
2. Store the file name in the data table and the file to the disk.
3. store all the file content in the database table

Example: Save the image to the server disk and save the image name to the database.

Step 1: Servlet
Public class fileuploadservlet extends httpservlet {

Private file uploadpath;
Private file temppath;

Public void dopost (httpservletrequest req, httpservletresponse res)
Throws servletexception, ioexception {
// Multipart/form-data is used for form submission, and data cannot be obtained using Req. getparameter ()
// Diskfileitemfactory object
Diskfileitemfactory factory = new diskfileitemfactory ();
// Set the File Cache size
Factory. setsizethreshold (4096 );
// When the file size exceeds the cache size, it will be saved in the temporary directory.
Factory. setrepository (temppath );
// Servletfileupload object
Servletfileupload upload = new servletfileupload (factory );
// Maximum File Size
Upload. setsizemax (1000000*20 );
Try {
List fileitems = upload. parserequest (req); // servletfileupload object resolution request to get the list of uploaded files
String itemno = "";
For (iterator iter = fileitems. iterator (); ITER. hasnext ();){
Fileitem item = (fileitem) ITER. Next ();

// Is a common form input field
If (item. isformfield ()){
If ("itemno". Equals (item. getfieldname () {// item. getfieldname () Get the control whose name is itemno in form.
Itemno = item. getstring (); // obtain the text value of the itemno control.
} // Itemno is the item ID, because the image name to be uploaded and the corresponding item are saved in the table, so this value is obtained here.
}
// It is not a common form input field. This field indicates the upload control.
If (! Item. isformfield ()){
String filename = item. getname (); // The Name Of The uploaded image in JSP. The value of the request cannot be used here.
Long size = item. getsize ();
If (filename = NULL | filename. Equals ("") & size = 0 ){
Continue;
}
// Truncate a string such as c: \ windows \ debug \ passwd. Log
Filename = filename. substring (filename. lastindexof ("\") + 1, filename. Length ());
Item. Write (new file (uploadpath, filename); // create an image with the specified name in the specified upload directory
Itemmanager. uploaditemimage (itemno, filename); // upload the image name to the database, which has been implemented elsewhere
}
}
Res. sendredirect (req. getcontextpath () + "/servlet/item/searchitemservlet ");
} Catch (exception e ){
E. printstacktrace ();
Throw new applicationexception ("Upload Failed! ");
}
}

Public void Init () throws servletexception {
Uploadpath = new file (getservletcontext (). getrealpath ("Upload"); // obtain the absolute path based on the virtual directory in the application. The virtual path here is webroot/upload.
System. Out. println ("uploadpath ====" + uploadpath );
// If the directory does not exist
If (! Uploadpath. exists ()){
// Create a directory
Uploadpath. mkdir (); // create an upload directory
}

// Temporary directory
Temppath = new file (getservletcontext (). getrealpath ("Temp "));
If (! Temppath. exists ()){
Temppath. mkdir (); // create a temporary directory
}
// Display the init Method for calling the parent class
Super. INIT ();
}
}

Step 2: JSP:
<Form name = "itemform" target = "_ Self" id = "itemform" method = "Post" Action = "Servlet/item/fileuploadservlet" enctype = "multipart/form-Data ">
''<Input type =" hidden "name =" itemno "value =" <% = item. getitemno () %> ">
<HR width = "97%" align = "center" size = 0>
<Tr>
<TD Height = "29">
<Div align = "right">
Material code: & nbsp;
</Div>
</TD>
<TD>
<% = Item. getitemno () %>
</TD>
</Tr>
<Tr>
<TD Height = "74">
<Div align = "right">
Image: & nbsp;
</Div>
</TD>
<TD>

</TD>
</Tr>
<Tr>
<TD width = "22%" Height = "29">
<Div align = "right">
<Font color = "# ff0000"> * </font> select image: & nbsp;
</Div>
</TD>
& Lt; TD width = "78%" & gt;
<Input name = "FILENAME" type = "file" class = "text1" size = "40" maxlength = "40">
</TD>
</Tr>
<HR width = "97%" align = "center" size = 0>
<Div align = "center">
<Input name = "btn_upload" class = "button1" type = "Submit" id = "btn_upload" value = "Upload">
</Div>
</Form>

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.