JSP servlet implements file upload, download, and delete, and servlet File Upload

Source: Internet
Author: User

JSP servlet implements file upload, download, and delete, and servlet File Upload

The examples in this article share with you the specific code of the Android jiugongge image for your reference. The specific content is as follows:

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> 

Add the following lines to web. xml:

<servlet>  <servlet-name>Upload</servlet-name>  <servlet-class>am.demo.Upload</servlet-class>  </servlet>  <servlet-mapping>  <servlet-name>Upload</servlet-name>  <url-pattern>/fileupload</url-pattern>  </servlet-mapping> 

Create the File Upload. java in the src directory:

Package am. demo; import java. io. file; import java. io. IOException; import java. util. iterator; import java. util. list; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. commons. fileupload. fileItem; import org. apache. commons. fileupload. disk. diskFileItemFactory; impo Rt org. apache. commons. fileupload. servlet. servletFileUpload; @ SuppressWarnings ("serial") public class Upload extends HttpServlet {private String uploadPath = "d: // temp"; // the directory for uploading files @ SuppressWarnings ("unchecked ") public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {try {// Create a factory for disk-based file items DiskFileItemFactory Factory = new DiskFileItemFactory (); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload (factory); List <FileItem> items = upload. parseRequest (request); // obtain all the files Iterator <FileItem> I = items. iterator (); while (I. hasNext () {FileItem fi = (FileItem) I. next (); String fileName = fi. getName (); if (fileName! = Null) {File fullFile = new File (fi. getName (); File savedFile = new File (uploadPath, fullFile. getName (); fi. write (savedFile) ;}} response. setContentType ("text/html; charset = GBK"); response. getWriter (). print ("<mce: script language = 'javascript '> <! -- Alert ('upload succeeded '); window. location. href = 'index. jsp '; // --> </mce: script> ");} catch (Exception e) {// you can jump to error page e. printStackTrace () ;}} public void init () throws ServletException {File uploadFile = new File (uploadPath); if (! UploadFile. exists () {uploadFile. mkdirs ();}}}

Download Downlaod. java again:

Package am. demo; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. outputStream; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; @ SuppressWarnings ("serial") public class Download extends HttpServlet {public void doGet (HttpServletRe Quest request, HttpServletResponse response) throws IOException, ServletException {String aFileName = new String (request. getParameter ("name "). getBytes ("iso8859-1"), "gbk"); File fileLoad = new File ("d:/temp", aFileName); FileInputStream in = null; // input stream OutputStream out = response. getOutputStream (); byte B [] = new byte [1024]; try {response. setContentType ("application/x-msdownload;"); response. SetHeader ("Content-disposition", "attachment; filename =" + new String (aFileName. getBytes ("GBK"), "ISO-8859-1"); // download the file. in = new FileInputStream (fileLoad); int n = 0; while (n = in. read (B ))! =-1) {out. write (B, 0, n) ;}} catch (Throwable e) {e. printStackTrace ();} finally {try {in. close (); out. close ();} catch (Throwable e) {e. printStackTrace () ;}} public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {doGet (request, response );}}

Delete. java:

Package am. demo; import java. io. file; import java. io. fileNotFoundException; import java. io. IOException; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; @ SuppressWarnings ("serial") public class Delete extends HttpServlet {public void doGet (HttpServletRequest request, HttpServle TResponse response) throws FileNotFoundException, IOException {String aFileName = new String (request. getParameter ("name "). getBytes ("iso8859-1"), "gbk"); File file = new File ("d:/temp", aFileName); response. setContentType ("text/html; charset = GBK"); if (! File. isDirectory () {file. delete (); response. getWriter (). print ("<mce: script language = 'javascript '> <! -- Alert ('deleted successfully'); window. location. href = 'index. jsp '; // --> </mce: script> ");} else {}} public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {doGet (request, response );}}

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 us.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.