文件上传有SmartUpload、Apache的Commons fileupload.我们今天介绍Commons fileupload的用法。
1.commons-fileupload-1.3.1.jar
下载地址:http://xiazai.jb51.net/201608/yuanma/javawebfileupload(jb51.net).rar
2.web.xml
UploadServlet com.servlet.UploadServlet filePath F:\myeclipse\Workspaces\King\WebRoot\uploadfile\ 3.UploadFile.jsp UploadServlet /servlet/UploadServlet
3.servlet
package com.servlet; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; 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.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import sun.swing.FilePane; public class UploadServlet extends HttpServlet { private String filePath; /** * Destruction of the servlet.
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.getWriter().println("请以POST方式上传文件"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { File file1 = null, file2 = null; String description1 = null, description2 = null; response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); DiskFileUpload diskFileUpload = new DiskFileUpload();//解析request try { ListfileList = diskFileUpload.parseRequest(request);//将结果放置到List中 out.println("遍历所有的FileItem..."); this.log("遍历所有的FileItem..."); for(FileItem fileItem : fileList){ if(fileItem.isFormField()){//如果是文本域 if("description1".equals(fileItem.getFieldName())){ out.println("遍历到description1..."); description1 = new String(fileItem.getString().getBytes(),"UTF-8"); } if("description2".equals(fileItem.getFieldName())){ out.println("遍历到description2..."); description2 = new String(fileItem.getString().getBytes(),"UTF-8"); } }else{//否则为文件域 if("file1".equals(fileItem.getFieldName())){ File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8")); out.println("遍历到file1..."); out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+""); //服务器端文件,放在uploadfile文件夹下 file1 = new File(filePath,remoteFile.getName()); file1.getParentFile().mkdirs();//创建文件夹路径 file1.createNewFile();//创建新文件 InputStream ins = fileItem.getInputStream();//FileItem的内容 OutputStream ous = new FileOutputStream(file1);//输出到文件中 try{ byte[] buffer = new byte[1024];//字节缓存 int len = 0;//实际缓存长度 while((len=ins.read(buffer)) > -1){//循环读入缓存 ous.write(buffer,0,len); } out.println("已保存文件"+file1.getAbsolutePath()+""); } finally{ ous.close(); ins.close(); } } if("file2".equals(fileItem.getFieldName())){ File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8")); out.println("遍历到file2..."); out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+""); //服务器端文件,放在uploadfile文件夹下 file2 = new File(filePath,remoteFile.getName()); file2.getParentFile().mkdirs();//创建文件夹路径 file2.createNewFile();//创建新文件 InputStream ins = fileItem.getInputStream();//FileItem的内容 OutputStream ous = new FileOutputStream(file2);//输出到文件中 try{ byte[] buffer = new byte[1024];//字节缓存 int len = 0;//实际缓存长度 while((len=ins.read(buffer)) > -1){//循环读入缓存 ous.write(buffer,0,len); } out.println("已保存文件"+file2.getAbsolutePath()+""); } finally{ ous.close(); ins.close(); } } } } out.println("Request 解析完毕"); } catch (FileUploadException e) { // TODO 自动生成的 catch 块 out.println(""); out.println(""); out.println(" "); out.println(" "); if(file1 != null){ out.println(" "); out.println(""); } if(file2 != null){ out.println("file1;"); out.println(""+file1.getName()+""); out.println(""); out.println(""); out.println(""); } out.println("file2;"); out.println(""+file2.getName()+""); out.println(""); out.println(""); out.println(""); out.println("description1:"); out.println(""); out.println(description1); out.println(""); out.println(""); out.println(""); out.println(" "); out.println(""); out.flush(); out.close(); } } public String getServletInfo() { return "This is my default servlet created by Eclipse"; } public void init() throws ServletException { // Put your code here filePath = getServletConfig().getInitParameter("filePath"); } }description2:"); out.println(""); out.println(description2); out.println(""); out.println("
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。