{"id":151,"date":"2017-05-17T09:26:00","date_gmt":"2017-05-17T01:26:00","guid":{"rendered":"https:\/\/cn1.club\/?p=151"},"modified":"2025-03-13T20:19:35","modified_gmt":"2025-03-13T12:19:35","slug":"%e5%9c%a8jsp%e9%a1%b5%e9%9d%a2%e5%ae%9e%e7%8e%b0%e6%96%87%e4%bb%b6%e7%9a%84%e4%b8%8a%e4%bc%a0%e4%b8%8e%e4%b8%8b%e8%bd%bd","status":"publish","type":"post","link":"http:\/\/cn1.club\/?p=151","title":{"rendered":"\u5728JSP\u9875\u9762\u5b9e\u73b0\u6587\u4ef6\u7684\u4e0a\u4f20\u4e0e\u4e0b\u8f7d"},"content":{"rendered":"\n<p>\u51c6\u5907\u5de5\u4f5c,\u5f15\u5165jar\u5305:<\/p>\n\n\n\n<p>commons-fileupload-1.2.1.jar<\/p>\n\n\n\n<p>commons-io-1.4<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>java\u9875:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >package com.baidu.com;\n\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.OutputStream;\nimport java.util.List;\n\nimport javax.servlet.ServletException;\nimport javax.servlet.http.HttpServlet;\nimport javax.servlet.http.HttpServletRequest;\nimport javax.servlet.http.HttpServletResponse;\n\nimport org.apache.commons.fileupload.FileItem;\nimport org.apache.commons.fileupload.FileUploadException;\nimport org.apache.commons.fileupload.disk.DiskFileItemFactory;\nimport org.apache.commons.fileupload.servlet.ServletFileUpload;\n\nimport com.sun.xml.internal.bind.v2.runtime.Name;\npublic class Test4 extends HttpServlet {\n\tprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n\t\ttry {\n\t\t\/\/\u5148\u5efa\u7acb\u78c1\u76d8\u6587\u4ef6\u9879\u5de5\u5382\n\t\tDiskFileItemFactory diskFileItemFactory =new DiskFileItemFactory();\n\t\t\/\/\u521b\u5efa\u4e00\u4e2a\u6838\u5fc3\u89e3\u6790\u7c7b\n\t\tServletFileUpload fileupload = new ServletFileUpload(diskFileItemFactory);\n\t\t\/\/\u5229\u7528\u6838\u5fc3\u89e3\u6790\u7c7b\u89e3\u6790Request\n\t\t\n\t\t\tList&lt;FileItem&gt; list = fileupload.parseRequest(request);\n\t\t\t\/\/\u904d\u5386\u8fd9\u4e2aList\u96c6\u5408,\u5f97\u5230\u4ee3\u8868\u6bcf\u4e2a\u90e8\u5206\u7684\u6587\u4ef6\u9879\u5bf9\u8c61,\u6839\u636e\u5224\u65ad\u662f\u5426\u662f\u6587\u4ef6\n\t\t\tfor (FileItem fileItem : list) {\n\t\t\t\t\/\/\u5224\u65ad\u8fd9\u4e2a\u95ee\u6d25\u662f\u666e\u901a\u7bb1\u8fd8\u662f\u6587\u4ef6\u4e0a\u4f20\u9879\n\t\t\t\tif (fileItem.isFormField()) {\n\t\t\t\t\t\/\/\u666e\u901a\u7bb1\n\t\t\t\t\t\/\/\u63a5\u53d7\u666e\u901a\u9879\u76ee\u7684\u503c\n\t\t\t\t\t\/\/\u5982\u679c\u662f\u666e\u901a\u7bb1\u90a3\u5c31\u53d6\u666e\u901a\u9879\u7684\u540d\u79f0\u548c\u503c(\u63a5\u53d7\u503c\u4e0d\u80fd\u518d\u4f7f\u7528request.getparameter())\n\t\t\t\t\tString name = fileItem.getFieldName();\n\t\t\t\t\t\/\/\u62ff\u666e\u901a\u7c7b\u578b\u7684\u503c\n\t\t\t\t\tString value = fileItem.getString(&quot;UTF-8&quot;);\n\t\t\t\t\tSystem.out.println(&quot;\u8f93\u51fa\u6253\u5370 :&quot;+ name +&quot;       &quot;+value);\n\t\t\t\t\t\/\/\n\t\t\t\t}else {\n\t\t\t\t\t\/\/\u6587\u4ef6\u4e0a\u4f20\u9879\n\t\t\t\t\t\/\/\u83b7\u5f97\u6587\u4ef6\u4e0a\u4f20\u7684\u6587\u4ef6\u540d\u79f0\n\t\t\t\t\tString filename = fileItem.getName();\n\t\t\t\t\t\/\/\u83b7\u5f97\u6587\u4ef6\u4e0a\u4f20\u7684\u6587\u4ef6\u6570\u636e\n\t\t\t\t\tInputStream is = fileItem.getInputStream();\n\t\t\t\t\t\/\/\u83b7\u5f97\u6587\u4ef6\u4e0a\u4f20\u7684\u6587\u4ef6\u7684\u8def\u5f84 ,\u6c60\u7554\u7684\u7edd\u5bf9\u8def\u5f84\n\t\t\t\t\tString realpath = getServletContext().getRealPath(&quot;.\/a&quot;);\n\t\t\t\t\t\/\/\u521b\u5efa\u4e00\u4e2a\u8f93\u51fa\u6d41\u5199\u5165\u5230\u521a\u624d(\u4e0a\u9762)\u7684\u8bbe\u7f6e\u7684\u8def\u5f84\u4e2d\n\t\t\t\t\tOutputStream os = new FileOutputStream(realpath+&quot;\/&quot;+filename);\n\t\t\t\t\t\/\/\u4ee5\u4e0b\u4e24\u4e2a\u8f93\u51fa\u6d41,\u5229\u7528\u4e4b\u524d\u6240\u5b66\u7684\u6570\u7ec4\u4f20\u8f93\u5b57\u8282\u6d41\u5bf9\u8c61\n\t\t\t\t\t\/\/\u5b9a\u4e49\u4e00\u4e2aint\u7c7b\u578b\u7684\u53d8\u91cf\n\t\t\t\t\tint len = 0;\n\t\t\t\t\t\/\/\u5b9a\u4e49\u4e00\u4e2abyte\u7c7b\u578b\u7684\u6570\u7ec4\n\t\t\t\t\tbyte&#091;] b = new byte&#091;1024];\n\t\t\t\t\twhile ((len = is.read(b))!= -1) {\n\t\t\t\t\t\tos.write(b, 0, len);\n\t\t\t\t\t\t\n\t\t\t\t\t}\n\t\t\t\t\tos.close();\n\t\t\t\t\tis.close();\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t\t\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\t\n\t\t\t\n\t\t} catch (FileUploadException e) {\n\t\t\t\/\/ TODO Auto-generated catch block\n\t\t\te.printStackTrace();\n\t\t}\n\t\t\n\t}\n\t\n\n\tprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\n\t\tdoGet(request, response);\n\t}\n\n}\n<\/code><\/pre>\n\n\n\n<p><strong>.jsp\u9875:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;%@ page language=&quot;java&quot; contentType=&quot;text\/html; charset=UTF-8&quot;\n    pageEncoding=&quot;UTF-8&quot;%&gt;\n&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN&quot; &quot;http:\/\/www.w3.org\/TR\/html4\/loose.dtd&quot;&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=UTf-8&quot;&gt;\n&lt;title&gt;Insert title here&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n&lt;h1&gt;\u6587\u4ef6\u4e0a\u4f20&lt;\/h1&gt;\n&lt;form action=&quot;\/shangchuanxiazai\/Test1&quot; method=&quot;post&quot; enctype=&quot;multipart\/form-data&quot;&gt;\n\u6587\u4ef6\u63cf\u8ff0:&lt;input type =&quot;text&quot; name=&quot;info&quot;&gt;&lt;br&gt;\n\u6587\u4ef6\u4e0a\u4f20:&lt;input type=&quot;file&quot; name = &quot;up&quot;&gt;&lt;br&gt;\n&lt;input type = &quot;submit&quot; value=&quot;\u4e0a\u4f20&quot;&gt; \n\n\n&lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u51c6\u5907\u5de5\u4f5c,\u5f15\u5165jar\u5305: commons-fileupload-1.2.1.jar commons-io-1. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":196,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"class_list":["post-151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coder"],"_links":{"self":[{"href":"http:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=151"}],"version-history":[{"count":0,"href":"http:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/posts\/151\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/cn1.club\/index.php?rest_route=\/wp\/v2\/media\/196"}],"wp:attachment":[{"href":"http:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cn1.club\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}