• 使用getParts()上传多个文件


    <!DOCTYPE html>
    <html>
        <head>
            <title></title>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        </head>
        <body>
            <div>
                <form action="UploadServlet3" method="POST" enctype="multipart/form-data">
                    <table>
                        <tr>
                            <td><label for="file1">文件1:</label></td>
                            <td><input type="file" id="file1" name="file"></td>
                        </tr>
                        <tr>
                            <td><label for="file2">文件2:</label></td>
                            <td><input type="file" id="file2" name="file"></td>
                        </tr>
                        <tr>
                            <td><label for="file3">文件3:</label></td>
                            <td><input type="file" id="file3" name="file"></td>
                        </tr>
                        <tr>
                            <td colspan="2"><input type="submit" value="上传" name="upload"></td>
                        </tr>
                    </table>
                </form>
            </div>
        </body>
    </html>
    

     

    @MultipartConfig(location = "e:/workspace")
    @WebServlet(name = "UploadServlet", urlPatterns = {"/UploadServlet"})
    public class UploadServlet extends HttpServlet {
     
        /**
         * Processes requests for both HTTP
         * <code>GET</code> and
         * <code>POST</code> methods.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            //迭代Collection中所有Part对象
            for (Part part : request.getParts()) {
                //只处理上传文件区段
                if (part.getName().startsWith("file")) {
                    String fileName = getFileName(part);
                    part.write(fileName);
                }
            }
        }
     
        private String getFileName(Part part) {
            String header = part.getHeader("Content-Disposition");
            String fileName = header.substring(header.indexOf("filename="") + 10, header.lastIndexOf("""));
            header.lastIndexOf(""");
            return fileName;
        }
     
        // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
        /**
         * Handles the HTTP
         * <code>GET</code> method.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
     
        /**
         * Handles the HTTP
         * <code>POST</code> method.
         *
         * @param request servlet request
         * @param response servlet response
         * @throws ServletException if a servlet-specific error occurs
         * @throws IOException if an I/O error occurs
         */
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
     
        /**
         * Returns a short description of the servlet.
         *
         * @return a String containing servlet description
         */
        @Override
        public String getServletInfo() {
            return "Short description";
        }// </editor-fold>
    }
    

      

     

  • 相关阅读:
    windows系统设置虚拟机开机自启并运行虚拟系统
    Pycharm模板添加默认信息
    (翻译)从底层了解ASP.NET体系结构 [转]
    sql2000安装在win2003后只有在本机才能访问,局域网内其他机器不能访问
    Web上传文件的原理及实现[转]
    Http 请求处理流程[转]
    .net 必知
    sql2000数据库在企业管理器中显示置疑(suspect),在查询分析器不显示。在企业管理器中附加:提示错误823
    HTTP请求过程简介[转]
    面向对象
  • 原文地址:https://www.cnblogs.com/Nbge/p/5249922.html
Copyright © 2020-2023  润新知