Java Development file upload and java File Upload

Source: Internet
Author: User

Java Development file upload and java File Upload

Files are uploaded using SmartUpload and Apache Commons fileupload. We will introduce the usage of Commons fileupload today.

1.Commons-fileupload-1.3.1.jar

: Http://pan.baidu.com/s/1kVtYMzH

 

2. web. xml

1 <! -- Upload file to the server --> 2 <servlet> 3 <servlet-name> UploadServlet </servlet-name> 4 <servlet-class> com. servlet. uploadServlet </servlet-class> 5 <init-param> 6 <param-name> filePath </param-name> <! -- File storage path --> 7 <param-value> F: \ myeclipse \ Workspaces \ King \ WebRoot \ uploadfile \ </param-value> 8 </init-param> 9 </servlet> 10 11 <servlet-mapping> 12 <servlet- name> UploadServlet </servlet-name> 13 <url-pattern>/servlet/UploadServlet </url-pattern> 14 </servlet-mapping>

3. UploadFile. jsp

1 <form action = "servlet/UploadServlet" method = "post" enctype = "multipart/form-data"> 2 <div align = "center"> 3 <fieldset style =" width: 80% "> 4 <legend> Upload File </legend> <br/> 5 <div align =" left "> Upload File 1 </div> 6 <div align =" left "> 7 <input type =" file "name =" file1 "/> 8 </div> 9 <div align =" left "> Upload file 2 </div> 10 <div align = "left"> 11 <input type = "file" name = "file2"/> 12 </div> 13 <div> 14 <div align = 'left'> upload File description 1 </div> 15 <div align = 'left'> <input type = "text" name = "description1"/> </div> 16 </div> 17 <div> 18 <div align = 'left'> File Upload description 2 </div> 19 <div align = 'left'> <input type = "text" name =" description2 "/> </div> 20 </div> 21 <div> 22 <div align = 'left'> 23 <input type = 'submit 'value =" Upload File" /> 24 </div> 25 </div> 26 </fieldset> 27 </div> 28 </form>

4. servlet

1 package com. servlet; 2 3 import java. io. file; 4 import java. io. fileOutputStream; 5 import java. io. IOException; 6 import java. io. inputStream; 7 import java. io. inputStreamReader; 8 import java. io. outputStream; 9 import java. io. printWriter; 10 import java. util. list; 11 12 import javax. servlet. servletException; 13 import javax. servlet. http. httpServlet; 14 import javax. servlet. http. httpServletRequest; 15 import javax. servlet. http. httpServletResponse; 16 17 import org. apache. commons. fileupload. diskFileUpload; 18 import org. apache. commons. fileupload. fileItem; 19 import org. apache. commons. fileupload. fileUploadException; 20 21 import sun. swing. filePane; 22 23 public class UploadServlet extends HttpServlet {24 25 private String filePath; 26 27 28/** 29 * Destruction of the servlet. <br> 30 */31 Public void destroy () {32 super. destroy (); // Just puts "destroy" string in log 33 // Put your code here 34} 35 36 37 public void doGet (HttpServletRequest request, HttpServletResponse response) 38 throws ServletException, IOException {39 40 response. setCharacterEncoding ("UTF-8"); 41 response. getWriter (). println ("upload files in POST mode"); 42} 43 44 45 public void doPost (HttpServletRequest request, HttpSe RvletResponse response) 46 throws ServletException, IOException {47 File file1 = null, file2 = null; 48 String description1 = null, description2 = null; 49 response. setCharacterEncoding ("UTF-8"); 50 request. setCharacterEncoding ("UTF-8"); 51 response. setContentType ("text/html"); 52 PrintWriter out = response. getWriter (); 53 54 DiskFileUpload diskFileUpload = new DiskFileUpload (); // parse request 55 56 Try {57 List <FileItem> fileList = diskFileUpload. parseRequest (request); // place the result in the List 58 out. println ("traverse all FileItem... </br> "); 59 this. log ("traverse all fileitems... "); 60 for (FileItem fileItem: fileList) {61 if (fileItem. isFormField () {// if it is a text field 62 if ("description1 ". equals (fileItem. getFieldName () {63 out. println ("traverse to description1... </br> "); 64 description1 = new String (fileItem. getString (). getBytes (), "UTF -8 "); 65} 66 if (" description2 ". equals (fileItem. getFieldName () {67 out. println ("traverse to description2... </br> "); 68 description2 = new String (fileItem. getString (). getBytes (), "UTF-8"); 69} 70} else {// otherwise it is the file domain 71 if ("file1 ". equals (fileItem. getFieldName () {72 File remoteFile = new File (new String (fileItem. getName (). getBytes (), "UTF-8"); 73 out. println ("traverse to file1... </br> "); 74 out. println ("client file location:" + remot EFile. getAbsolutePath () + "</br>"); 75 // put the Server File In the uploadfile folder 76 file1 = new File (filePath, remoteFile. getName (); 77 file1.getParentFile (). mkdirs (); // create a folder path 78 file1.createNewFile (); // create a new file 79 InputStream ins = fileItem. getInputStream (); // FileItem content 80 OutputStream ous = new FileOutputStream (file1); // output to the file 81 try {82 byte [] buffer = new byte [1024]; // byte cache 83 int len = 0; // actual cache length 84 while (len = I Ns. read (buffer)>-1) {// read the cache 85 ous cyclically. write (buffer, 0, len); 86} 87 out. println ("saved files" + file1.getAbsolutePath () + "</br>"); 88} 89 finally {90 ous. close (); 91 ins. close (); 92} 93} 94 if ("file2 ". equals (fileItem. getFieldName () {95 File remoteFile = new File (new String (fileItem. getName (). getBytes (), "UTF-8"); 96 out. println ("traverse to file2... </br> "); 97 out. println ("client file location:" + remoteFile. getAbsolutePath () + "</Br>"); 98 // Server File, put in the uploadfile folder 99 file2 = new File (filePath, remoteFile. getName (); 100 file2.getParentFile (). mkdirs (); // create the folder path 101 file2.createNewFile (); // create a new file 102 InputStream ins = fileItem. getInputStream (); // FileItem content 103 OutputStream ous = new FileOutputStream (file2); // output to the file 104 try {105 byte [] buffer = new byte [1024]; // byte cache 106 int len = 0; // The actual cache length is 107 while (len = ins. read (buffer)>-1) {// Read 108 ous cached cyclically. write (buffer, 0, len); 109} 110 out. println ("saved files" + file2.getAbsolutePath () + "</br>"); 111} 112 finally {113 ous. close (); 114 ins. close (); 115} 116} 117} 118} 119 out. println ("Request parsed"); 120} catch (FileUploadException e) {121 // catch Block automatically generated by TODO 122 out. println ("<! Doctype html public \ "-// W3C // dtd html 4.01 Transitional // EN \"> "); 123 out. println ("<HTML>"); 124 out. println ("<HEAD> <TITLE> A Servlet </TITLE> </HEAD>"); 125 out. println ("<BODY>"); 126 127 if (file1! = Null) {128 out. println ("<div>"); 129 out. println ("<div align = 'left'> file1; </div>"); 130 out. println ("<div align = 'left'> <a href = '" + request. getContextPath () + 131 file1.getName () + "'target = _ blank>" + file1.getName () + "</a>"); 132 out. println ("</div>"); 133 out. println ("</div>"); 134} 135 if (file2! = Null) {136 out. println ("<div>"); 137 out. println ("<div align = 'left'> file2; </div>"); 138 out. println ("<div align = 'left'> <a href = '" + request. getContextPath () + 139 file2.getName () + "'target = _ blank>" + file2.getName () + "</a>"); 140 out. println ("</div>"); 141 out. println ("</div>"); 142} 143 out. println ("<div>"); 144 out. println ("<div align = 'left'> description1: </div>"); 145 out. println ("<div align = 'left'>"); 146 out. println (description1); 147 out. println ("</div>"); 148 out. println ("</div>"); 149 150 out. println ("<div>"); 151 out. println ("<div align = 'left'> description2: </div>"); 152 out. println ("<div align = 'left'>"); 153 out. println (description2); 154 out. println ("</div>"); 155 out. println ("</div>"); 156 157 out. println ("</BODY>"); 158 out. println ("</HTML>"); 159 out. flush (); 160 out. close (); 161} 162 163} 164 165 public String getServletInfo () {166 return "This is my default servlet created by Eclipse"; 167} 168 169 170 public void init () throws ServletException {171 // Put your code here172 filePath = getServletConfig (). getInitParameter ("filePath"); 173} 174 175}

6.

 

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.