• 上传文件


     1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     2 <html>
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     5 <title>upload.html</title>
     6 </head>
     7 <body>
     8     <form method="POST"  enctype="multipart/form-data"  action="upload">
     9         username : <input type="text" name="username" size=45 /> <br/>
    10         file1 : <input type="file" name="file1" size=45/><br/>
    11         file2 : <input type="file" name="file2" size=45/><br/>
    12         <input type="submit" name="submit" value="upload" />
    13     </form>
    14 </body>
    15 </html>

    upload.html

    username :
    file1 :
    file2 :
     1
      //UploadServlet.java,用于接收处理表单内容
    package com.demo; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 import java.util.Iterator; 7 import java.util.List; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import org.apache.commons.fileupload.FileItem; 15 import org.apache.commons.fileupload.FileUploadException; 16 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 17 import org.apache.commons.fileupload.servlet.ServletFileUpload; 18 19 20 public class UploadServlet extends HttpServlet{ 21 @Override 22 protected void doGet(HttpServletRequest req, HttpServletResponse resp) 23 throws ServletException, IOException { 24 doPost(req, resp); 25 } 26 @Override 27 protected void doPost(HttpServletRequest req, HttpServletResponse resp) 28 throws ServletException, IOException { 29 //1- 设置MIME类型,获取输出流 30 resp.setContentType("text/html;charset=utf-8"); 31 PrintWriter out = resp.getWriter(); 32 33 //2- 加载镜像文件工厂 34 DiskFileItemFactory factory = new DiskFileItemFactory(); 35 //2-1 设置缓冲区大小 36 factory.setSizeThreshold(2*1024); 37 //2-2 设置文件临时存放目录 38 factory.setRepository(new File("D:\temp")); 39 40 //3- 加载servletFileUpload方法,获取文件列表 41 ServletFileUpload upload = new ServletFileUpload(factory); 42 try { 43 List<FileItem> items = upload.parseRequest(req); 44 Iterator its = items.iterator(); 45 while (its.hasNext()) { 46 47 FileItem item = (FileItem) its.next(); 48 if (item.isFormField()) { 49 handleFormField(item,out); 50 }else { 51 handleUploadField(item,out); 52 } 53 54 } 55 } catch (FileUploadException e) { 56 e.printStackTrace(); 57 }finally { 58 out.close(); 59 } 60 } 61 //处理上传域的方法 62 private void handleUploadField(FileItem item, PrintWriter out) { 63 //1- 获取文件名 64 String fname = item.getName(); 65 //2- 判断文件名中是否含有文件路径 66 int index = -1; 67 if ((index = fname.lastIndexOf("\")) != -1) { 68 //若存在,则截取文件名 69 fname = fname.substring(index +1); 70 } 71 //3- 判断文件是否已经上传 72 long size = fname.length(); 73 if ("".equals(fname) && size==0) {//未上传,则什么都不做 74 return; 75 } 76 //上传了文件,则保存到本地磁盘中 77 File dest = new File("d:\store\"+fname); 78 try { 79 item.write(dest); 80 //4- 通知客户端,保存成功 81 out.print(fname +" 保存成功!"+"<br />"); 82 } catch (Exception e) { 83 e.printStackTrace(); 84 } 85 86 87 } 88 //处理表单控件域的方法 89 private void handleFormField(FileItem item, PrintWriter out) { 90 //1- 获取表单控件的name属性名称 91 String name = item.getName(); 92 //2- 获得表单控件的value属性值 93 String value = item.getString(); 94 out.println("---------------表单控件信息----------------"); 95 out.println(name + " = " +value ); 96 97 98 } 99 }
    1 
      //web.xml中的配置
    <servlet> 2 <servlet-name>upload</servlet-name> 3 <servlet-class>com.demo.UploadServlet</servlet-class> 4 </servlet> 5 <servlet-mapping> 6 <servlet-name>upload</servlet-name> 7 <url-pattern>/upload</url-pattern> 8 </servlet-mapping>

    附件:所需要的lib包

    • commons-fileupload-1.2.1.jar
    • commons-io-1.4.jar


  • 相关阅读:
    Java进阶知识32 mybatis(ibatis)入门CRUD操作【简单演示,只测DAO层】
    Java进阶知识31 SpringMVC+JDBC+Oracle 注解版整合实例
    Java进阶知识30 Struts2+Spring+Hibernate+Oracle XML版整合实例
    Java进阶知识29 Struts2+Spring+Hibernate+Oracle 注解版整合实例
    错误/异常:java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType 的解决方法
    kafka
    手动升级openssl
    解决Homebrew下载更新极慢的问题
    修改centos的源, 使其能访问阿里云服务器
    mycat学习
  • 原文地址:https://www.cnblogs.com/zqq3436/p/5334119.html
Copyright © 2020-2023  润新知