{"id":223404,"date":"2021-08-08T10:20:18","date_gmt":"2021-08-08T02:20:18","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=223404"},"modified":"2021-07-26T10:21:00","modified_gmt":"2021-07-26T02:21:00","slug":"jsp-file-upload","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/jsp-file-upload.html","title":{"rendered":"JSP\u7b14\u8bb0-\u6587\u4ef6\u4e0a\u4f20"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nJSP\uff08\u5168\u79f0JavaServer Pages\uff09\u662f\u7531Sun Microsystems\u516c\u53f8\u4e3b\u5bfc\u521b\u5efa\u7684\u4e00\u79cd\u52a8\u6001\u7f51\u9875\u6280\u672f\u6807\u51c6\u3002JSP\u90e8\u7f72\u4e8e\u7f51\u7edc\u670d\u52a1\u5668\u4e0a\uff0c\u53ef\u4ee5\u54cd\u5e94\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u8bf7\u6c42\uff0c\u5e76\u6839\u636e\u8bf7\u6c42\u5185\u5bb9\u52a8\u6001\u5730\u751f\u6210HTML\u3001XML\u6216\u5176\u4ed6\u683c\u5f0f\u6587\u6863\u7684Web\u7f51\u9875\uff0c\u7136\u540e\u8fd4\u56de\u7ed9\u8bf7\u6c42\u8005\u3002JSP\u6280\u672f\u4ee5Java\u8bed\u8a00\u4f5c\u4e3a\u811a\u672c\u8bed\u8a00\uff0c\u4e3a\u7528\u6237\u7684HTTP\u8bf7\u6c42\u63d0\u4f9b\u670d\u52a1\uff0c\u5e76\u80fd\u4e0e\u670d\u52a1\u5668\u4e0a\u7684\u5176\u5b83Java\u7a0b\u5e8f\u5171\u540c\u5904\u7406\u590d\u6742\u7684\u4e1a\u52a1\u9700\u6c42\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\nJSP \u53ef\u4ee5\u4e0e HTML form \u6807\u7b7e\u4e00\u8d77\u4f7f\u7528\uff0c\u6765\u5141\u8bb8\u7528\u6237\u4e0a\u4f20\u6587\u4ef6\u5230\u670d\u52a1\u5668\u3002\u4e0a\u4f20\u7684\u6587\u4ef6\u53ef\u4ee5\u662f\u6587\u672c\u6587\u4ef6\u6216\u56fe\u50cf\u6587\u4ef6\u6216\u4efb\u4f55\u6587\u6863\u3002<\/p>\n

\u672c\u7ae0\u8282\u6211\u4eec\u4f7f\u7528 Servlet \u6765\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff0c\u4f7f\u7528\u5230\u7684\u6587\u4ef6\u6709\uff1a<\/p>\n

1.upload.jsp : \u6587\u4ef6\u4e0a\u4f20\u8868\u5355\u3002<\/p>\n

2.message.jsp : \u4e0a\u4f20\u6210\u529f\u540e\u8df3\u8f6c\u9875\u9762\u3002<\/p>\n

3.UploadServlet.java : \u4e0a\u4f20\u5904\u7406 Servlet\u3002<\/p>\n

4.\u9700\u8981\u5f15\u5165\u7684 jar \u6587\u4ef6\uff1acommons-fileupload-1.3.2\u3001commons-io-2.5.jar\u3002<\/p>\n

\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6\u4e0a\u4f20\u8868\u5355<\/strong><\/div>\n

\u4e0b\u9762\u7684 HTML \u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u6587\u4ef6\u4e0a\u4f20\u8868\u5355\u3002\u4ee5\u4e0b\u51e0\u70b9\u9700\u8981\u6ce8\u610f\uff1a<\/p>\n

1.\u8868\u5355 method \u5c5e\u6027\u5e94\u8be5\u8bbe\u7f6e\u4e3a POST \u65b9\u6cd5\uff0c\u4e0d\u80fd\u4f7f\u7528 GET \u65b9\u6cd5\u3002<\/p>\n

2.\u8868\u5355 enctype \u5c5e\u6027\u5e94\u8be5\u8bbe\u7f6e\u4e3a multipart\/form-data.<\/p>\n

3.\u8868\u5355 action \u5c5e\u6027\u5e94\u8be5\u8bbe\u7f6e\u4e3a\u5728\u540e\u7aef\u670d\u52a1\u5668\u4e0a\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\u7684 Servlet \u6587\u4ef6\u3002\u4e0b\u9762\u7684\u5b9e\u4f8b\u4f7f\u7528\u4e86 UploadServlet <\/p>\n

4.Servlet \u6765\u4e0a\u4f20\u6587\u4ef6\u3002
\n\u4e0a\u4f20\u5355\u4e2a\u6587\u4ef6\uff0c\u60a8\u5e94\u8be5\u4f7f\u7528\u5355\u4e2a\u5e26\u6709\u5c5e\u6027 type=\"file\" \u7684 <input ...\/> \u6807\u7b7e\u3002\u4e3a\u4e86\u5141\u8bb8\u591a\u4e2a\u6587\u4ef6\u4e0a\u4f20\uff0c\u8bf7\u5305\u542b\u591a\u4e2a name \u5c5e\u6027\u503c\u4e0d\u540c\u7684 input \u6807\u7b7e\u3002\u8f93\u5165\u6807\u7b7e\u5177\u6709\u4e0d\u540c\u7684\u540d\u79f0\u5c5e\u6027\u7684\u503c\u3002\u6d4f\u89c8\u5668\u4f1a\u4e3a\u6bcf\u4e2a input \u6807\u7b7e\u5173\u8054\u4e00\u4e2a\u6d4f\u89c8\u6309\u94ae\u3002<\/p>\n

upload.jsp \u6587\u4ef6\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

<%@ page language=\"java\" contentType=\"text\/html; charset=UTF-8\"\r\n    pageEncoding=\"UTF-8\"%>\r\n<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\"\r\n    \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\">\r\n<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\">\r\n<title>\u6587\u4ef6\u4e0a\u4f20\u5b9e\u4f8b - \u83dc\u9e1f\u6559\u7a0b<\/title>\r\n<\/head>\r\n<body>\r\n<h1>\u6587\u4ef6\u4e0a\u4f20\u5b9e\u4f8b - \u83dc\u9e1f\u6559\u7a0b<\/h1>\r\n<form method=\"post\" action=\"\/TomcatTest\/UploadServlet\" enctype=\"multipart\/form-data\">\r\n    \u9009\u62e9\u4e00\u4e2a\u6587\u4ef6:\r\n    <input type=\"file\" name=\"uploadFile\" \/>\r\n    <br\/><br\/>\r\n    <input type=\"submit\" value=\"\u4e0a\u4f20\" \/>\r\n<\/form>\r\n<\/body>\r\n<\/html><\/pre>\n
\u7f16\u5199\u540e\u53f0 Servlet<\/strong><\/div>\n

\u4ee5\u4e0b\u662f UploadServlet \u7684\u6e90\u4ee3\u7801\uff0c\u540c\u4e8e\u5904\u7406\u6587\u4ef6\u4e0a\u4f20\uff0c\u5728\u8fd9\u4e4b\u524d\u6211\u4eec\u5148\u786e\u4fdd\u4f9d\u8d56\u5305\u5df2\u7ecf\u5f15\u5165\u5230\u9879\u76ee\u7684 WEB-INF\/lib \u76ee\u5f55\u4e0b\uff1acommons-fileupload-1.3.2.jar\uff0ccommons-io-2.5.jar
\nUploadServlet \u7684\u6e90\u4ee3\u7801 \u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\r\npackage com.runoob.test;\r\n\r\nimport java.io.File;\r\nimport java.io.IOException;\r\nimport java.io.PrintWriter;\r\nimport java.util.List;\r\n \r\nimport javax.servlet.ServletException;\r\nimport javax.servlet.annotation.WebServlet;\r\nimport javax.servlet.http.HttpServlet;\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\n \r\nimport org.apache.commons.fileupload.FileItem;\r\nimport org.apache.commons.fileupload.disk.DiskFileItemFactory;\r\nimport org.apache.commons.fileupload.servlet.ServletFileUpload;\r\n \r\n\r\n\/**\r\n * Servlet implementation class UploadServlet\r\n *\/\r\n\r\n\/\/ \u5982\u679c\u4e0d\u914d\u7f6e web.xml \uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\r\n\/\/ @WebServlet(\"\/UploadServlet\")\r\npublic class UploadServlet extends HttpServlet {\r\n    private static final long serialVersionUID = 1L;\r\n     \r\n    \/\/ \u4e0a\u4f20\u6587\u4ef6\u5b58\u50a8\u76ee\u5f55\r\n    private static final String UPLOAD_DIRECTORY = \"upload\";\r\n \r\n    \/\/ \u4e0a\u4f20\u914d\u7f6e\r\n    private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 3;  \/\/ 3MB\r\n    private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; \/\/ 40MB\r\n    private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; \/\/ 50MB\r\n \r\n    \/**\r\n     * \u4e0a\u4f20\u6570\u636e\u53ca\u4fdd\u5b58\u6587\u4ef6\r\n     *\/\r\n    protected void doPost(HttpServletRequest request,\r\n        HttpServletResponse response) throws ServletException, IOException {\r\n        \/\/ \u68c0\u6d4b\u662f\u5426\u4e3a\u591a\u5a92\u4f53\u4e0a\u4f20\r\n        if (!ServletFileUpload.isMultipartContent(request)) {\r\n            \/\/ \u5982\u679c\u4e0d\u662f\u5219\u505c\u6b62\r\n            PrintWriter writer = response.getWriter();\r\n            writer.println(\"Error: \u8868\u5355\u5fc5\u987b\u5305\u542b enctype=multipart\/form-data\");\r\n            writer.flush();\r\n            return;\r\n        }\r\n \r\n        \/\/ \u914d\u7f6e\u4e0a\u4f20\u53c2\u6570\r\n        DiskFileItemFactory factory = new DiskFileItemFactory();\r\n        \/\/ \u8bbe\u7f6e\u5185\u5b58\u4e34\u754c\u503c - \u8d85\u8fc7\u540e\u5c06\u4ea7\u751f\u4e34\u65f6\u6587\u4ef6\u5e76\u5b58\u50a8\u4e8e\u4e34\u65f6\u76ee\u5f55\u4e2d\r\n        factory.setSizeThreshold(MEMORY_THRESHOLD);\r\n        \/\/ \u8bbe\u7f6e\u4e34\u65f6\u5b58\u50a8\u76ee\u5f55\r\n        factory.setRepository(new File(System.getProperty(\"java.io.tmpdir\")));\r\n \r\n        ServletFileUpload upload = new ServletFileUpload(factory);\r\n         \r\n        \/\/ \u8bbe\u7f6e\u6700\u5927\u6587\u4ef6\u4e0a\u4f20\u503c\r\n        upload.setFileSizeMax(MAX_FILE_SIZE);\r\n         \r\n        \/\/ \u8bbe\u7f6e\u6700\u5927\u8bf7\u6c42\u503c (\u5305\u542b\u6587\u4ef6\u548c\u8868\u5355\u6570\u636e)\r\n        upload.setSizeMax(MAX_REQUEST_SIZE);\r\n        \r\n        \/\/ \u4e2d\u6587\u5904\u7406\r\n        upload.setHeaderEncoding(\"UTF-8\"); \r\n\r\n        \/\/ \u6784\u9020\u4e34\u65f6\u8def\u5f84\u6765\u5b58\u50a8\u4e0a\u4f20\u7684\u6587\u4ef6\r\n        \/\/ \u8fd9\u4e2a\u8def\u5f84\u76f8\u5bf9\u5f53\u524d\u5e94\u7528\u7684\u76ee\u5f55\r\n        String uploadPath = getServletContext().getRealPath(\"\/\") + File.separator + UPLOAD_DIRECTORY;\r\n       \r\n         \r\n        \/\/ \u5982\u679c\u76ee\u5f55\u4e0d\u5b58\u5728\u5219\u521b\u5efa\r\n        File uploadDir = new File(uploadPath);\r\n        if (!uploadDir.exists()) {\r\n            uploadDir.mkdir();\r\n        }\r\n \r\n        try {\r\n            \/\/ \u89e3\u6790\u8bf7\u6c42\u7684\u5185\u5bb9\u63d0\u53d6\u6587\u4ef6\u6570\u636e\r\n            @SuppressWarnings(\"unchecked\")\r\n            List formItems = upload.parseRequest(request);\r\n \r\n            if (formItems != null && formItems.size() > 0) {\r\n                \/\/ \u8fed\u4ee3\u8868\u5355\u6570\u636e\r\n                for (FileItem item : formItems) {\r\n                    \/\/ \u5904\u7406\u4e0d\u5728\u8868\u5355\u4e2d\u7684\u5b57\u6bb5\r\n                    if (!item.isFormField()) {\r\n                        String fileName = new File(item.getName()).getName();\r\n                        String filePath = uploadPath + File.separator + fileName;\r\n                        File storeFile = new File(filePath);\r\n                        \/\/ \u5728\u63a7\u5236\u53f0\u8f93\u51fa\u6587\u4ef6\u7684\u4e0a\u4f20\u8def\u5f84\r\n                        System.out.println(filePath);\r\n                        \/\/ \u4fdd\u5b58\u6587\u4ef6\u5230\u786c\u76d8\r\n                        item.write(storeFile);\r\n                        request.setAttribute(\"message\",\r\n                            \"\u6587\u4ef6\u4e0a\u4f20\u6210\u529f!\");\r\n                    }\r\n                }\r\n            }\r\n        } catch (Exception ex) {\r\n            request.setAttribute(\"message\",\r\n                    \"\u9519\u8bef\u4fe1\u606f: \" + ex.getMessage());\r\n        }\r\n        \/\/ \u8df3\u8f6c\u5230 message.jsp\r\n        getServletContext().getRequestDispatcher(\"\/message.jsp\").forward(\r\n                request, response);\r\n    }\r\n}<\/fileitem><\/pre>\n

message.jsp \u6587\u4ef6\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

\r\n<%@ page language=\"java\" contentType=\"text\/html; charset=UTF-8\"\r\n    pageEncoding=\"UTF-8\"%>\r\n<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\"\r\n    \"http:\/\/www.w3.org\/TR\/html4\/loose.dtd\">\r\n<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\">\r\n<title>\u6587\u4ef6\u4e0a\u4f20\u7ed3\u679c<\/title>\r\n<\/head>\r\n<body>\r\n    <center>\r\n        <h2>${message}<\/h2>\r\n    <\/center>\r\n<\/body>\r\n<\/html><\/pre>\n
\u7f16\u8bd1\u548c\u8fd0\u884c Servlet<\/strong><\/div>\n

\u7f16\u8bd1\u4e0a\u9762\u7684 Servlet UploadServlet\uff0c\u5e76\u5728 web.xml \u6587\u4ef6\u4e2d\u521b\u5efa\u6240\u9700\u7684\u6761\u76ee\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<web-app xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n    xmlns=\"http:\/\/java.sun.com\/xml\/ns\/javaee\"\r\n    xmlns:web=\"http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_2_5.xsd\"\r\n    xsi:schemaLocation=\"http:\/\/java.sun.com\/xml\/ns\/javaee\r\n        http:\/\/java.sun.com\/xml\/ns\/javaee\/web-app_2_5.xsd\"\r\n    id=\"WebApp_ID\" version=\"2.5\">\r\n  <servlet>\r\n    <display-name>UploadServlet<\/display-name>\r\n    <servlet-name>UploadServlet<\/servlet-name>\r\n    <servlet-class>com.runoob.test.UploadServlet<\/servlet-class>\r\n  <\/servlet>\r\n   \r\n  <servlet-mapping>\r\n    <servlet-name>UploadServlet<\/servlet-name>\r\n    <url-pattern>\/TomcatTest\/UploadServlet<\/url-pattern>\r\n  <\/servlet-mapping>\r\n<\/web-app>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

JSP \u53ef\u4ee5\u4e0e HTML form \u6807\u7b7e\u4e00\u8d77\u4f7f\u7528\uff0c\u6765\u5141\u8bb8\u7528\u6237\u4e0a\u4f20\u6587\u4ef6\u5230\u670d\u52a1\u5668\u3002\u4e0a\u4f20\u7684\u6587\u4ef6\u53ef\u4ee5\u662f\u6587\u672c\u6587\u4ef6\u6216\u56fe\u50cf\u6587 […]<\/p>\n","protected":false},"author":1470,"featured_media":223365,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-223404","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=223404"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223404\/revisions"}],"predecessor-version":[{"id":223462,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223404\/revisions\/223462"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/223365"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=223404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=223404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=223404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}