Initial use of jspSmartUpload, use of jspSmartUpload
Use SmartUpload in jsp to upload files
1. Download The jspSmartUpload component and decompress it. Copy the com directory to the WEB-INF \ classes directory of the application.
2. Pay special attention
(1) Add enctype = "multipart/form-data" to the file upload form, for example:
<Form action = "product_update_ OK .jsp? P_id = <% = p_id %> "method =" post "onSubmit =" return check_product (this); "enctype =" multipart/form-data ">
(2) For file upload forms, the value of form items cannot be obtained through the request object, which must be obtained through the Request class provided by the jspSmartUpload component, for example:
String p_name = su. getRequest (). getParameter ("p_name ");
The Code should be placed after the file upload code.
3. Main Code
// Create an upload object
SmartUpload su = new SmartUpload ();
// Upload Initialization
Su. initialize (pageContext );
// Set upload restrictions
// Limit the maximum length of each uploaded file.
// Su. setMaxFileSize (10000 );
// Limit the total length of uploaded data.
// Su. setTotalMaxFileSize (20000 );
// Use the extension limit to set files that can be uploaded. Only doc and txt files are allowed here.
// Su. setAllowedFilesList ("doc, txt ");
// Use the extension to restrict files that are not allowed to be uploaded. Do not upload files with the exe, bat, jsp, htm, html extension, or files without the extension.
// Su. setDeniedFilesList ("exe, bat, jsp, htm, html ,,");
// Implement File Upload
Su. upload ();
// Save all uploaded files to the specified directory. The images directory must exist in the application root folder.
// Int count = su. save ("/images", su. SAVE_VIRTUAL );
Int count = su. save ("/images ");
String p_image_temp = "";
If (count = 1) // re-upload the image
{
Com. jspsmart. upload. File file = su. getFiles (). getFile (0 );
P_image_temp = "images \" + file. getFileName ();
}
Else
P_image_temp = (String) session. getAttribute ("p_iamge"); // The image is not uploaded again.
// Obtain other form information
String p_id = su. getRequest (). getParameter ("p_id ");
String p_type = su. getRequest (). getParameter ("p_type ");
String p_name = su. getRequest (). getParameter ("p_name ");
String p_price = su. getRequest (). getParameter ("p_price ");
String p_quantity = su. getRequest (). getParameter ("p_quantity ");
String p_description = su. getRequest (). getParameter ("p_description ");
String p_time = String. valueOf (date. getMonth () + 1) + "-" + date. getDate () + "-20" + String. valueOf (date. getYear ()). substring (1 );