Uploading images using Cos.jar in Jsp+servlet
upload.jsp
<formAction= "Fileservlet"Method= "POST" enctype= "Multipart/form-data" > <inputtype= "File"name= "MyFile"> <inputtype= "text"name= "CMT"value= "example Example"> <inputtype= "Submit"> </form>
The Enctype:html property, which specifies how the form data should be encoded before being sent to the server. "Pre-send Code"
download.jsp
<href= "Fileservlet? " Filename=1.xlsx">download</a>
Servlet:FileServlet.java
Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportCom.jspsmart.upload.File;Importcom.jspsmart.upload.Request;ImportCom.jspsmart.upload.SmartUpload;Importcom.jspsmart.upload.SmartUploadException; Public classFileservletextendsHttpServlet {@Overrideprotected voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {String strName= Request.getparameter ("filename"); if(StrName = =NULL) { return; } //new, please .Smartupload su =Newsmartupload (); //Initializesu.initialize (Getservletconfig (), request, response); //InitializeSu.setcontentdisposition (NULL); Try{su.downloadfile ("up/" +strName); } Catch(smartuploadexception e) {e.printstacktrace (); } //The download is not redirected, the response has been submitted and an error occurs://cannot call Sendredirect () after the response have been committed//response.sendredirect ("upload.jsp"); } Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//new, please .Smartupload su =Newsmartupload (); //Initializesu.initialize (Getservletconfig (), request, response); Try{su.upload (); File F= Su.getfiles (). GetFile (0); //save_physical Operating system root directory//save_virtual web App root directory//Save_auto Components choose for themselvesF.saveas ("up/" +f.getfilename (), smartupload.save_virtual); } Catch(smartuploadexception e) {e.printstacktrace (); } //↓↓↓ taking ParametersString SEVLETP = Request.getparameter ("CMT"); System.out.println ("Get parameters directly can not:" +SEVLETP); Request Request2=su.getrequest (); String SuP= Request2.getparameter ("CMT"); System.out.println ("Get parameters indirectly through Smartupload objects:" +SuP); //↑↑↑ taking Parameters//Go to download pageResponse.sendredirect ("download.jsp"); }}
Xml
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "3.0"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <Servlet-name>Fileservlet</Servlet-name> <Servlet-class>Fileservlet</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>Fileservlet</Servlet-name> <Url-pattern>/fileservlet</Url-pattern> </servlet-mapping> <welcome-file-list><welcome-file> upload.jsp</welcome-file></welcome-file-list > </Web-app>
Garbled problem: This component by default uses the encoding is gb2312, submits the file, the JSP uses the GBK to solve the file name garbled.
The folder is built first, and the folder is generated in the build project
Upload and download images using Jspsmartupload.jar in Jsp+servlet