JSP/servlet for file upload, download, and deletion

Source: Internet
Author: User

Because a bucket is used to operate a stored file for general users, during the past two days, I have taken a special look at how to upload, download, and delete files using servlets in windows, the following is a detailed code description.

 

Upload:

With commons-fileupload-1.2.2.jar and commons-io-2.0.1.jar components, you can go to the Apache official website to download, and then put the webroot/WEB-INF/lib directory

 

Upload.html

 

<HTML> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = GBK "> <br/> <title> File Upload </title> <br/> </pead> <br/> <body> <br/> <! -- Action = "fileupload" corresponds to <URL-pattern> Settings in <servlet-mapping> in Web. xml. --> <br/> <! -- The form data must be multipart, can complete file data transfer --> <br/> <form name = "myform" Action = "fileupload" method = "Post" <br/> enctype = "multipart/form- data "> <br/> file: <br> <br/> <input type = "file" name = "myfile"> <br/> <input type = "Submit "Name =" Submit "value =" commit "> <br/> </form> <br/> </body> <br/> </ptml>

 

Add the following lines to Web. xml:

<Servlet> <br/> <servlet-Name> upload </servlet-Name> <br/> <servlet-class> AM. demo. upload </servlet-class> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> upload </servlet-Name> <br/> <URL-pattern>/fileupload </url-pattern> <br/> </servlet-mapping> </P> <p>

Create the File Upload. Java in the src directory:

Package AM. demo; </P> <p> Import Java. io. file; <br/> Import Java. io. ioexception; <br/> Import Java. util. iterator; <br/> Import Java. util. list; </P> <p> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> Import Org. apache. commons. fileupload. fileitem; <br/> Import Org. apa Che. commons. fileupload. disk. diskfileitemfactory; <br/> Import Org. apache. commons. fileupload. servlet. servletfileupload; </P> <p> @ suppresswarnings ("serial") <br/> public class upload extends httpservlet {<br/> private string uploadpath = "D: // Temp "; // directory of the uploaded file </P> <p> @ suppresswarnings (" unchecked ") <br/> Public void dopost (httpservletrequest request, httpservletresponse response) <br/> throws ioexception, S Ervletexception {<br/> try {<br/> // create a factory for disk-based file items <br/> diskfileitemfactory factory = new diskfileitemfactory (); </P> <p> // create a new file upload handler <br/> servletfileupload upload = new servletfileupload (factory ); </P> <p> List <fileitem> items = upload. parserequest (request); // obtain all files <br/> iterator <fileitem> I = items. iterator (); <br/> while (I. hasnext () {<br/> filei TEM Fi = (fileitem) I. Next (); <br/> string filename = Fi. getname (); <br/> If (filename! = NULL) {<br/> file fullfile = new file (Fi. getname (); <br/> file savedfile = new file (uploadpath, fullfile. getname (); <br/> fi. write (savedfile); <br/>}</P> <p> response. setcontenttype ("text/html; charset = GBK"); <br/> response. getwriter (). print (<br/> "<MCE: script language = 'javascript '> <! -- <Br/> alert ('upload succeeded '); window. location. href = 'index. JSP '; <br/> // --> </MCE: SCRIPT> "); <br/>} catch (exception E) {<br/> // you can jump to the error page <br/> E. printstacktrace (); <br/>}</P> <p> Public void Init () throws servletexception {<br/> file uploadfile = new file (uploadpath); <br/> If (! Uploadfile. exists () {<br/> uploadfile. mkdirs (); <br/>}< br/>

 

Download downlaod. Java again:

Package AM. demo; </P> <p> Import Java. io. file; <br/> Import Java. io. fileinputstream; <br/> Import Java. io. ioexception; <br/> Import Java. io. outputstream; </P> <p> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> @ suppresswarnings ("serial") <br/> public class download Extends httpservlet {<br/> Public void doget (httpservletrequest request, httpservletresponse response) <br/> throws ioexception, servletexception {</P> <p> string afilename = new string (request. getparameter ("name "). getbytes (<br/> "iso8859-1"), "GBK"); </P> <p> file fileload = new file ("D:/Temp", afilename ); </P> <p> fileinputstream in = NULL; // input stream <br/> outputstream out = response. getoutputstream (); <br/> Byte B [] = new byte [1024]; </P> <p> try {</P> <p> response. setcontenttype ("application/X-msdownload;"); </P> <p> response. setheader ("content-disposition", "attachment; filename =" <br/> + new string (afilename. getbytes ("GBK"), "ISO-8859-1"); </P> <p> // download the file. <br/> In = new fileinputstream (fileload); <br/> int n = 0; <br/> while (n = in. read (B ))! =-1) {<br/> out. write (B, 0, n); <br/>}</P> <p >}catch (throwable e) {<br/> E. printstacktrace (); <br/>}finally {<br/> try {<br/> in. close (); <br/> out. close (); <br/>}catch (throwable e) {<br/> E. printstacktrace (); <br/>}</P> <p> Public void dopost (httpservletrequest request, httpservletresponse response) <br/> throws ioexception, servletexception {<br/> doget (request, response); <br/>}</P> <p >}< br/>

Delete Delete. Java:

Package AM. demo; </P> <p> Import Java. io. file; <br/> Import Java. io. filenotfoundexception; <br/> Import Java. io. ioexception; </P> <p> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservlet; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> @ suppresswarnings ("serial") <br/> public class Delete extends httpservlet {</P> <p> Pu BLIC void doget (httpservletrequest request, httpservletresponse response) <br/> throws filenotfoundexception, ioexception {</P> <p> string afilename = new string (request. getparameter ("name "). getbytes (<br/> "iso8859-1"), "GBK"); </P> <p> file = new file ("D:/Temp", afilename ); </P> <p> response. setcontenttype ("text/html; charset = GBK"); </P> <p> If (! File. isdirectory () {<br/> file. delete (); <br/> response. getwriter (). print (<br/> "<MCE: script language = 'javascript '> <! -- <Br/> alert ('deleted successfully'); window. location. href = 'index. JSP '; <br/> // --> </MCE: SCRIPT> "); <br/>} else {</P> <p >}</P> <p> Public void dopost (httpservletrequest request, httpservletresponse response) <br/> throws ioexception, servletexception {<br/> doget (request, response); <br/>}< br/>

 

Because ubuntu Server is a black screen and I use Ubuntu server10.04, it is not convenient to use a browser to view files. Because there is no graphical interface and debugging is not convenient, I can test it on windows first, then copy the corresponding file to the Tomcat directory of ubuntu server. You can use Samba to upload the file. However, a problem occurs, that is, Chinese garbled characters, the English language is selected, and some methods on the Internet are used later. Chinese garbled characters are still not solved. If you know anything, please tell me

Related Article

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.