Java开发之文件上传
文件上传有SmartUpload、Apache的Commons fileupload.我们今天介绍Commons fileupload的用法。
1.commons-fileupload-1.3.1.jar
下载地址:http://pan.baidu.com/s/1kVtYMzH
2.web.xml
1 <!-- 上传文件 服务器端 --> 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><!-- 文件存储路径 --> 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>上传文件</legend><br/> 5 <div align="left">上传文件1</div> 6 <div align="left"> 7 <input type="file" name="file1"/> 8 </div> 9 <div align="left">上传文件2</div> 10 <div align="left"> 11 <input type="file" name="file2"/> 12 </div> 13 <div> 14 <div align='left'>上传文件说明1</div> 15 <div align='left'><input type="text" name="description1"/></div> 16 </div> 17 <div> 18 <div align='left'>上传文件说明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="上传文件"/> 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("请以POST方式上传文件"); 42 } 43 44 45 public void doPost(HttpServletRequest request, HttpServletResponse 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();//解析request 55 56 try { 57 List<FileItem> fileList = diskFileUpload.parseRequest(request);//将结果放置到List中 58 out.println("遍历所有的FileItem...</br>"); 59 this.log("遍历所有的FileItem..."); 60 for(FileItem fileItem : fileList){ 61 if(fileItem.isFormField()){//如果是文本域 62 if("description1".equals(fileItem.getFieldName())){ 63 out.println("遍历到description1...</br>"); 64 description1 = new String(fileItem.getString().getBytes(),"UTF-8"); 65 } 66 if("description2".equals(fileItem.getFieldName())){ 67 out.println("遍历到description2...</br>"); 68 description2 = new String(fileItem.getString().getBytes(),"UTF-8"); 69 } 70 }else{//否则为文件域 71 if("file1".equals(fileItem.getFieldName())){ 72 File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8")); 73 out.println("遍历到file1...</br>"); 74 out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"</br>"); 75 //服务器端文件,放在uploadfile文件夹下 76 file1 = new File(filePath,remoteFile.getName()); 77 file1.getParentFile().mkdirs();//创建文件夹路径 78 file1.createNewFile();//创建新文件 79 InputStream ins = fileItem.getInputStream();//FileItem的内容 80 OutputStream ous = new FileOutputStream(file1);//输出到文件中 81 try{ 82 byte[] buffer = new byte[1024];//字节缓存 83 int len = 0;//实际缓存长度 84 while((len=ins.read(buffer)) > -1){//循环读入缓存 85 ous.write(buffer,0,len); 86 } 87 out.println("已保存文件"+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("遍历到file2...</br>"); 97 out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"</br>"); 98 //服务器端文件,放在uploadfile文件夹下 99 file2 = new File(filePath,remoteFile.getName()); 100 file2.getParentFile().mkdirs();//创建文件夹路径 101 file2.createNewFile();//创建新文件 102 InputStream ins = fileItem.getInputStream();//FileItem的内容 103 OutputStream ous = new FileOutputStream(file2);//输出到文件中 104 try{ 105 byte[] buffer = new byte[1024];//字节缓存 106 int len = 0;//实际缓存长度 107 while((len=ins.read(buffer)) > -1){//循环读入缓存 108 ous.write(buffer,0,len); 109 } 110 out.println("已保存文件"+file2.getAbsolutePath()+"</br>"); 111 } 112 finally{ 113 ous.close(); 114 ins.close(); 115 } 116 } 117 } 118 } 119 out.println("Request 解析完毕"); 120 } catch (FileUploadException e) { 121 // TODO 自动生成的 catch 块 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 here 172 filePath = getServletConfig().getInitParameter("filePath"); 173 } 174 175 }
6.