SmartUpload implements file upload and smartupload
(1) Introduction to the SmartUpload component
The SmartUpload component is a free component used to upload and download files. (2) The SmartUpload component features easy to use: Write a small amount of code, the upload/download function controls the size and type of the uploaded files. Disadvantages: The Update Service has been stopped.
(3) Application Conditions of the SmartUpload component
File class and Method
SmartUpload class and Method
File Upload implementation
Control the types of uploaded files
Control the size of uploaded files
Note:
(I) Form requirements
There are two requirements for the FORM for uploading files:
1. Apply POST to METHOD, that is, METHOD = "POST ".
2. Add attributes: ENCTYPE = "multipart/form-data"
Case: The following is an example of a FORM used to upload files:
Form page indes. jsp <% @ page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
Process a single table page
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ page import =" com. jspsmart. upload. smartUpload "%> <% @ page import =" com. jspsmart. upload. file "%> <% // ---- process uploaded files/* use smartupload 1, add the component jar package to the WEB-INF/lib package 2, set form submission type to binary type * /// create the smartupload component SmartUpload su = new SmartUpload (); // initialize the component context su. initialize (pageContext); // sets the encoding su. setCharset ("UTF-8"); try {// file type that can be uploaded // su. setAllowedF IlesList ("doc"); // restrict the type of the uploaded file // su. setDeniedFilesList ("bat"); // restrict the size of the uploaded file. // su. setMaxFileSize (2000); // Upload File su. upload () ;}catch (Exception e) {%> <script type = "text/javascript"> alert ("upload not allowed: bat, exe, jsp, html, htm, java, class file type "); location. href = "index. jsp "; </script> <%} out. println ("Upload" + su. save ("upload \") + "files succeeded !!! "); // Obtain the form item String name = su. getRequest (). getParameter ("username"); String pwd = su. getRequest (). getParameter ("pwd");/* implement Multifile upload */for (int I = 0; I <su. getFiles (). getCount (); I ++) {// obtain a single File file File = su. getFiles (). getFile (I); // determines whether a file is selected for the current file; if not, continue if (file. isMissing () continue; // set the upload server location String path = "upload \"; // obtain the name of the uploaded file: String filename = file. getFileName (); // path of the uploaded file = path + filename; // save the file. saveAs (path, SmartUpload. SAVE_VIRTUAL); out. println ("<table border = 1>"); out. println ("<tr> <td> name: </td> <td>" + name + "</td> </tr>"); out. println ("<tr> <td> password: </td> <td>" + pwd + "</td> </tr>"); out. println ("<tr> <td> name of the uploaded file (filename): </td> <td>" + filename + "</td> </tr> "); out. println ("<tr> <td> name of the uploaded file form item (fliedname): </td> <td>" + file. getFieldName () + "</td> </tr>"); out. println ("<tr> <td> size (length) of the uploaded file: </td> <td>" + file. getSize () + "</td> </tr>"); out. println ("<tr> <td> file extension (ext): </td> <td>" + file. getFileExt () + "</td> </tr>"); out. println ("</table> <br/>") ;}%>