<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>-Flyぁ梦- &#187; apache</title>
	<atom:link href="http://blog.11034.org/tag/apache/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.11034.org</link>
	<description></description>
	<lastBuildDate>Sun, 22 Jun 2025 08:59:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>使用Apache FileUpload</title>
		<link>http://blog.11034.org/2011-03/apache_fileupload.html</link>
		<comments>http://blog.11034.org/2011-03/apache_fileupload.html#comments</comments>
		<pubDate>Thu, 24 Mar 2011 13:08:48 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[apache]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=821</guid>
		<description><![CDATA[用Java写Web程序，最大的好处就是几乎所有的功能都能找到强大的第三方类库来帮你实现。Apache的comm [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>用Java写Web程序，最大的好处就是几乎所有的功能都能找到强大的第三方类库来帮你实现。Apache的commons-fileupload是不错的上传文件的类库，实习中恰好又用上了，很幸运地曾经用fileupload写过程序，不过时间久了难免遗忘，写一篇文章保存着以后自己看也好。</p>
<p><span id="more-821"></span></p>
<p>在Web页面中处理上传文件的form表单，有2个值得注意的地方：</p>
<ol>
<li>form表单需要有属性enctype=&#8221;multipart/form-data&#8221;，而且method必须为post</li>
<li>当form使用了以上属性后，常用的HttpServletRequest.getParameter(&#8220;fieldname&#8221;)方法来获取表单中的字段值就失效了</li>
</ol>
<p>编程的时候这2点很容易忘记，忘了第一点文件无法上传，fileupload中的判断方法ServletFileUpload.isMultipartContent()就会返回false。忘了第二点就更麻烦，显而易见的程序就跑不出来了，往往让人很郁闷。</p>
<p>这样就可以使用fileupload来处理文件上传了，先初始化2个类DiskFileItemFactory和ServletFileUpload</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #0000ff;">&quot;新建一个Factory&quot;</span>
DiskFileItemFactory factory <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DiskFileItemFactory<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">&quot;设置放置上传临时文件的文件夹&quot;</span>
factory.<span style="color: #006633;">setRepository</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;临时文件夹&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">&quot;设置上传文件放在内存中的临界大小，大于临界值则会以临时文件的形式存在临时文件夹中，默认值10K&quot;</span>
factory.<span style="color: #006633;">setSizeThreshold</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">&quot;新建一个ServletFileUpload，基于Servlet的Web程序嘛&quot;</span>
ServletFileUpload servletFileUpload <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ServletFileUpload<span style="color: #009900;">&#40;</span>factory<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #0000ff;">&quot;设置上传文件最大size，10M是默认值&quot;</span>
servletFileUpload.<span style="color: #006633;">setSizeMax</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>然后就可以开始处理request数据，读取HttpServletRequest无法读取的表单字段数据和保存上传的文件了</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">HttpServletRequest request<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>ServletFileUpload.<span style="color: #006633;">isMultipartContent</span><span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    List<span style="color: #339933;">&lt;</span>FileItem<span style="color: #339933;">&gt;</span> items <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>FileItem<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span>servletFileUpload.<span style="color: #006633;">parseRequest</span><span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>FileItem item <span style="color: #339933;">:</span> items<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>item.<span style="color: #006633;">isFormField</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;form中普通的字段&quot;</span>
            <span style="color: #003399;">String</span> fieldName <span style="color: #339933;">=</span> item.<span style="color: #006633;">getFieldName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> value <span style="color: #339933;">=</span> item.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> value <span style="color: #339933;">=</span> item.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;form中的file项，即上传文件的内容&quot;</span>
            <span style="color: #000066; font-weight: bold;">long</span> fileSize <span style="color: #339933;">=</span> item.<span style="color: #006633;">getSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> fileName <span style="color: #339933;">=</span> item.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #0000ff;">&quot;Tip：一般可以用 时间戳、文件大小、随机数和源文件名 这几个参数来动态生成唯一的新文件名&quot;</span>
            item.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;新的文件位置&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            item.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #0000ff;">&quot;用来释放临时文件&quot;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>很简单，很方便，apache威武，开源威武！</p>
<h4  class="related_post_title">随便看一看</h4><ul class="related_post"><li>2016-06-08 -- <a target="_blank" href="http://blog.11034.org/2016-06/tomcat_shutdown.html" title="Tomcat监听shutdown释放数据库连接池">Tomcat监听shutdown释放数据库连接池</a></li><li>2012-02-26 -- <a target="_blank" href="http://blog.11034.org/2012-02/get_drunk.html" title="初中聚，第一次喝醉">初中聚，第一次喝醉</a></li><li>2011-03-10 -- <a target="_blank" href="http://blog.11034.org/2011-03/learning_cs.html" title="学计算机的尼玛伤不起啊！！！！！！">学计算机的尼玛伤不起啊！！！！！！</a></li><li>2011-05-15 -- <a target="_blank" href="http://blog.11034.org/2011-05/walking_in_shanghai.html" title="周末上海大暴走">周末上海大暴走</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/semaphore.html" title="Semaphore简单分析">Semaphore简单分析</a></li></ul><h4 class="related_post_title">看看 Java </h4><ul class="related_post"><li>2016-09-09 -- <a target="_blank" href="http://blog.11034.org/2016-09/64bits_linux_arena_memory.html" title="64位Linux下Java进程堆外内存迷之64M问题">64位Linux下Java进程堆外内存迷之64M问题</a></li><li>2016-08-18 -- <a target="_blank" href="http://blog.11034.org/2016-08/java_concurrency_in_practice.html" title="读java concurrency in practice">读java concurrency in practice</a></li><li>2016-08-05 -- <a target="_blank" href="http://blog.11034.org/2016-08/thread_stop.html" title="线程清理">线程清理</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/futuretask.html" title="FutureTask简单分析和用法">FutureTask简单分析和用法</a></li><li>2016-06-21 -- <a target="_blank" href="http://blog.11034.org/2016-06/semaphore.html" title="Semaphore简单分析">Semaphore简单分析</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2011-03/apache_fileupload.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
