For example, this upload/download function uses the jspsmartupload component. To download the jar package online
First, let's start with a simple case program:
Upload:
Front-end:
<% @ Page contentType = "text/html; charset = UTF-8" %>JspSmartUpload component File Upload
JspSmartUpload component File Upload
Background:
Import java. io. IOException; import java. io. printWriter; import javax. servlet. servletConfig; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import com. jspsmart. upload. smartUpload; public class SmartUploadServlet extends HttpServlet {private ServletConfig config; // initialize Servlet final public void init (ServletConfig config) throws ServletException {this. config = config;} // process the GET request public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this. doPost (request, response);} // response POST request public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// get PrintWriter object PrintWriter out = response. getWriter (); out. println (""); Out. println (""); Out. println ("A Servlet"); Out. println ("
"); Out. println ("
JspSmartUpload "); out. println ("
"); // Create a new SmartUpload object SmartUpload mySmartUpload = new SmartUpload (); try {// upload and initialize mySmartUpload. initialize (config, request, response); // sets the maximum length of each uploaded file. mySmartUpload. setMaxFileSize (1*512*1024); // set the length of the total uploaded data mySmartUpload. setTotalMaxFileSize (1*1024*1024); // sets the type of the file to be uploaded. Only java, doc, and txt files can be uploaded. mySmartUpload. setAllowedFilesList ("java, doc, txt"); // sets the type of the file to be uploaded. Upload of files with exe and bat files with mySmartUpload is prohibited. setDeniedFilesLi St ("exe, bat"); // upload the file mySmartUpload. upload (); // save all uploaded files to the specified directory String name = new String (mySmartUpload. getRequest (). getParameter ("paramete"); System. out. println (name); int count = mySmartUpload. save ("/"); out. println (// obtain the parameter value using the Request object // String name = new String (request. getParameter ("name "). getBytes ("ISO-8859-1"), "UTF-8"); mySmartUpload. getRequest (). getParameter ("paramete") + ""); out. println (name ); // Display the processing result out. println ("" + count + "File Upload!
"); // Process each uploaded file for (int I = 0; I
"); // Print the File extension out. println (" file ExtName: "+ File. getFileExt () +"
"); // Print the file Path, including the directory out. println (" Path: "+ file. getFilePathName () +"
"); // Save it to the directory where the root directory of the Web application is the root directory of the file. // (Declaration: In Myeclipse, this directory is located under the project. metadata /. me_tcat/webapps/the project directory/upload/) file. saveAs ("/upload/" + file. getFileName (), mySmartUpload. SAVE_VIRTUAL) ;}}} catch (Exception e) {// Exception Handling // print custom Exception information out. println ("Unanable to upload the file.
"); Out. println (" Please Check The File Type ");}}}
Download:
Front-end:
<% @ Page contentType = "text/html; charset = UTF-8" %>JspSmartUpload
JspSmartUpload
Background:
Import java. io. IOException; import javax. servlet. servletConfig; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import com. jspsmart. upload. smartUpload; public class SmartDownloadServlet extends HttpServlet {private ServletConfig config; // initialize Servletfinal public void init (ServletConfig config) throws ServletException {this. config = config;} // process the GET request public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this. doPost (request, response);} // response POST request public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// create a new SmartUpload object SmartUpload mySmartUpload = new SmartUpload (); try {// upload and initialize mySmartUpload. initialize (config, request, response); // String name = new String (request. getParameter ("name "). getBytes ("ISO-8859-1"), "UTF-8"); String fileName = new String (request. getParameter ("fileName "). getBytes ("ISO-8859-1"), "UTF-8"); // System. out. println (fileName); // set contentDisposition to null to prohibit the browser from opening the file automatically // ensure that the file is downloaded after the link is clicked. If not set, mySmartUpload. setContentDisposition (null); // download file adding path "/upload/" mySmartUpload. downloadFile ("/upload/" + fileName);} catch (Exception e) {// Exception Handling }}}
Note that if this component is used, an attribute is added to the form to be uploaded: enctype = "multipart/form-data, if you want to pass other parameters at the same time, the previous request cannot be used for receiving in the background. getParamter (); instead, use the method in this component. For example, if you write the file name docName in the input file and want to pass this value, the backend must be in mySmartUpload. upload (); String docName = mySmartUpload. getRequest (). getParameter ("doNname"); note that it must be followed by that sentence.
Now the problem arises. This component is actually very simple to use and is very practical in some small projects, but he also has a very troublesome problem-Chinese garbled characters. There is no good solution to this problem, because the jspsmartupload component does not support Chinese characters. The only solution is to change its source code. You can try to find a way to change it on your own, which is very troublesome. Now I have solved this problem. The jar package that perfectly supports jspsmartupload Chinese upload and download and passing parameters is as follows:
Http://download.csdn.net/detail/u011250851/7200253 hope to help you.