zoukankan      html  css  js  c++  java
  • 单文件上传和多文件上传实例

    一、单文件上传

    1.上传页面

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <form action="../upLoadServlet" method="post" enctype="multipart/form-data">
    11 <label>请选择文件</label><br/>
    12 <input type="file" name="fileName"/><br/>
    13 <button type="submit">提交</button>
    14 </form>
    15 </body>
    16 </html>
    View Code

    2.服务器端

     1 package com.zdsofe.work;
     2 
     3 import java.io.File;
     4 import java.io.IOException;
     5 
     6 import javax.servlet.ServletConfig;
     7 import javax.servlet.ServletException;
     8 import javax.servlet.annotation.MultipartConfig;
     9 import javax.servlet.annotation.WebServlet;
    10 import javax.servlet.http.HttpServlet;
    11 import javax.servlet.http.HttpServletRequest;
    12 import javax.servlet.http.HttpServletResponse;
    13 import javax.servlet.http.Part;
    14 
    15 /**
    16  * Servlet implementation class upLoadServlet
    17  */
    18 @WebServlet("/upLoadServlet")
    19 @MultipartConfig(location="D://upFile",
    20                  fileSizeThreshold=1024*1024*20,
    21                  maxFileSize=1024*1024*20,
    22                  maxRequestSize=1024*1024*40        
    23         )
    24 public class upLoadServlet extends HttpServlet {
    25     private static final long serialVersionUID = 1L;
    26        
    27     /**
    28      * @see HttpServlet#HttpServlet()
    29      */
    30     public upLoadServlet() {
    31         super();
    32         // TODO Auto-generated constructor stub
    33     }
    34 
    35     /**
    36      * @see Servlet#init(ServletConfig)
    37      */
    38     public void init(ServletConfig config) throws ServletException {
    39         
    40         File file=new File("D://upFile");
    41         if(!file.exists()&&!file.isDirectory())
    42         {
    43             file.mkdir();
    44         }
    45     }
    46 
    47     /**
    48      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    49      */
    50     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    51         // TODO Auto-generated method stub
    52     }
    53 
    54     /**
    55      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    56      */
    57     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    58          //得到part对象
    59          Part part=request.getPart("fileName");
    60          if(part!=null)
    61          {  
    62             /* //获得文件的类型
    63              String type= part.getContentType();
    64              //获取文件的大小
    65              long size= part.getSize();*/
    66              //获取文件头信息
    67              String content= part.getHeader("content-disposition");
    68              //获取文件名所在的位置
    69              int a= content.indexOf("filename=");
    70              //获取完整的文件名
    71              String name=content.substring(a+10,content.lastIndexOf(""") );
    72              //讲上传的文件写到指定的位置
    73              part.write(name);
    74          }
    75     }
    76 
    77 }
    View Code

    二、多文件上传

    1、上传页面

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <form action="../upLoadServlet" method="post" enctype="multipart/form-data">
    11 <label>请选择文件</label><br/>
    12 <input type="file" name="fileName"/><br/>
    13 <input type="file" name="fileName"/><br/>
    14 <input type="file" name="fileName"/><br/>
    15 <button type="submit">提交</button>
    16 </form>
    17 </body>
    18 </html>
    View Code

    2.服务器端

      1 package com.zdsofe.work;
      2 
      3 import java.io.File;
      4 import java.io.IOException;
      5 import java.util.Collection;
      6 
      7 import javax.servlet.ServletConfig;
      8 import javax.servlet.ServletException;
      9 import javax.servlet.annotation.MultipartConfig;
     10 import javax.servlet.annotation.WebServlet;
     11 import javax.servlet.http.HttpServlet;
     12 import javax.servlet.http.HttpServletRequest;
     13 import javax.servlet.http.HttpServletResponse;
     14 import javax.servlet.http.Part;
     15 
     16 import org.apache.commons.collections.CollectionUtils;
     17 
     18 /**
     19  * Servlet implementation class upLoadServlet
     20  */
     21 @WebServlet("/upLoadServlet")
     22 @MultipartConfig(location="D://upFile",
     23                  fileSizeThreshold=1024*1024*20,
     24                  maxFileSize=1024*1024*20,
     25                  maxRequestSize=1024*1024*40        
     26         )
     27 public class upLoadServlet extends HttpServlet {
     28     private static final long serialVersionUID = 1L;
     29        
     30     /**
     31      * @see HttpServlet#HttpServlet()
     32      */
     33     public upLoadServlet() {
     34         super();
     35         // TODO Auto-generated constructor stub
     36     }
     37 
     38     /**
     39      * @see Servlet#init(ServletConfig)
     40      */
     41     public void init(ServletConfig config) throws ServletException {
     42         
     43         File file=new File("D://upFile");
     44         if(!file.exists()&&!file.isDirectory())
     45         {
     46             file.mkdir();
     47         }
     48     }
     49 
     50     /**
     51      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     52      */
     53     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     54         // TODO Auto-generated method stub
     55     }
     56 
     57     /**
     58      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     59      */
     60     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     61          /*
     62           * 单文件上传
     63           * //得到part对象
     64          Part part=request.getPart("fileName");
     65          if(part!=null)
     66          {  
     67              //获得文件的类型
     68              String type= part.getContentType();
     69              //获取文件的大小
     70              long size= part.getSize();
     71              //获取文件头信息
     72              String content= part.getHeader("content-disposition");
     73              //获取文件名所在的位置
     74              int a= content.indexOf("filename=");
     75              //获取完整的文件名
     76              String name=content.substring(a+10,content.lastIndexOf(""") );
     77              //讲上传的文件写到指定的位置
     78              part.write(name);
     79          }*/
     80         
     81         //多文件上传
     82         Collection<Part> parts=request.getParts();
     83         StringBuffer sf=new StringBuffer();
     84         if(!CollectionUtils.isEmpty(parts))
     85         {
     86             for(Part part : parts)
     87             {
     88                  //获取文件头信息
     89                  String content= part.getHeader("content-disposition");
     90                  //获取文件名所在的位置
     91                  int a= content.indexOf("filename=");
     92                  //获取完整的文件名
     93                  String name=content.substring(a+10,content.lastIndexOf(""") );
     94                  //讲上传的文件写到指定的位置
     95                  part.write(name);
     96                  
     97                  sf.append(name).append(",");
     98             }
     99         }
    100         
    101         String result= sf.toString().substring(0,sf.toString().length()-1);        
    102         request.setAttribute("re", result);
    103         System.out.println(111);
    104         request.getRequestDispatcher("/pages/welcome.jsp").forward(request, response);;
    105     }
    106 
    107 }
    View Code

    3.跳转页面

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 欢迎<%=request.getAttribute("re")
    11 %>进入
    12 </body>
    13 </html>
    View Code
  • 相关阅读:
    axios解决调用后端接口跨域问题
    vuex的使用入门-官方用例
    vue使用axios实现前后端通信
    vue组件间通信用例
    vue-router的访问权限管理
    vue-router使用入门
    PHP 流程控制
    PHP 表达式和运算符
    PHP 预定义变量
    PHP 常量
  • 原文地址:https://www.cnblogs.com/zclqian/p/7273187.html
Copyright © 2011-2022 走看看