📄 fileuploadbase.html
字号:
<a name="302" href="#302">302</a> <strong>return</strong> parseRequest(<strong>new</strong> <a href="../../../../org/apache/commons/fileupload/servlet/ServletRequestContext.html">ServletRequestContext</a>(req));<a name="303" href="#303">303</a> }<a name="304" href="#304">304</a> <a name="305" href="#305">305</a> <em>/**</em><a name="306" href="#306">306</a> <em> * Processes an <a href="<a href="http://www.ietf.org/rfc/rfc1867.txt" target="alexandria_uri">http://www.ietf.org/rfc/rfc1867.txt</a>">RFC 1867</a></em><a name="307" href="#307">307</a> <em> * compliant <code>multipart/form-data</code> stream.</em><a name="308" href="#308">308</a> <em> *</em><a name="309" href="#309">309</a> <em> * @param ctx The context for the request to be parsed.</em><a name="310" href="#310">310</a> <em> *</em><a name="311" href="#311">311</a> <em> * @return An iterator to instances of <code>FileItemStream</code></em><a name="312" href="#312">312</a> <em> * parsed from the request, in the order that they were</em><a name="313" href="#313">313</a> <em> * transmitted.</em><a name="314" href="#314">314</a> <em> *</em><a name="315" href="#315">315</a> <em> * @throws FileUploadException if there are problems reading/parsing</em><a name="316" href="#316">316</a> <em> * the request or storing files.</em><a name="317" href="#317">317</a> <em> * @throws IOException An I/O error occurred. This may be a network</em><a name="318" href="#318">318</a> <em> * error while communicating with the client or a problem while</em><a name="319" href="#319">319</a> <em> * storing the uploaded content.</em><a name="320" href="#320">320</a> <em> */</em><a name="321" href="#321">321</a> <strong>public</strong> <a href="../../../../org/apache/commons/fileupload/FileItemIterator.html">FileItemIterator</a> getItemIterator(<a href="../../../../org/apache/commons/fileupload/RequestContext.html">RequestContext</a> ctx)<a name="322" href="#322">322</a> throws FileUploadException, IOException {<a name="323" href="#323">323</a> <strong>return</strong> <strong>new</strong> FileItemIteratorImpl(ctx);<a name="324" href="#324">324</a> }<a name="325" href="#325">325</a> <a name="326" href="#326">326</a> <em>/**</em><a name="327" href="#327">327</a> <em> * Processes an <a href="<a href="http://www.ietf.org/rfc/rfc1867.txt" target="alexandria_uri">http://www.ietf.org/rfc/rfc1867.txt</a>">RFC 1867</a></em><a name="328" href="#328">328</a> <em> * compliant <code>multipart/form-data</code> stream.</em><a name="329" href="#329">329</a> <em> *</em><a name="330" href="#330">330</a> <em> * @param ctx The context for the request to be parsed.</em><a name="331" href="#331">331</a> <em> *</em><a name="332" href="#332">332</a> <em> * @return A list of <code>FileItem</code> instances parsed from the</em><a name="333" href="#333">333</a> <em> * request, in the order that they were transmitted.</em><a name="334" href="#334">334</a> <em> *</em><a name="335" href="#335">335</a> <em> * @throws FileUploadException if there are problems reading/parsing</em><a name="336" href="#336">336</a> <em> * the request or storing files.</em><a name="337" href="#337">337</a> <em> */</em><a name="338" href="#338">338</a> <strong>public</strong> List <em class="comment">/*<em class="comment"> <a href="../../../../org/apache/commons/fileupload/FileItem.html">FileItem</a> */</em> parseRequest(<a href="../../../../org/apache/commons/fileupload/RequestContext.html">RequestContext</a> ctx)</em><a name="339" href="#339">339</a> throws <a href="../../../../org/apache/commons/fileupload/FileUploadException.html">FileUploadException</a> {<a name="340" href="#340">340</a> <strong>try</strong> {<a name="341" href="#341">341</a> <a href="../../../../org/apache/commons/fileupload/FileItemIterator.html">FileItemIterator</a> iter = getItemIterator(ctx);<a name="342" href="#342">342</a> List items = <strong>new</strong> ArrayList();<a name="343" href="#343">343</a> <a href="../../../../org/apache/commons/fileupload/FileItemFactory.html">FileItemFactory</a> fac = getFileItemFactory();<a name="344" href="#344">344</a> <strong>if</strong> (fac == <strong>null</strong>) {<a name="345" href="#345">345</a> <strong>throw</strong> <strong>new</strong> NullPointerException(<a name="346" href="#346">346</a> <span class="string">"No FileItemFactory has been set."</span>);<a name="347" href="#347">347</a> }<a name="348" href="#348">348</a> <strong>while</strong> (iter.hasNext()) {<a name="349" href="#349">349</a> <a href="../../../../org/apache/commons/fileupload/FileItemStream.html">FileItemStream</a> item = iter.next();<a name="350" href="#350">350</a> <a href="../../../../org/apache/commons/fileupload/FileItem.html">FileItem</a> fileItem = fac.createItem(item.getFieldName(),<a name="351" href="#351">351</a> item.getContentType(), item.isFormField(),<a name="352" href="#352">352</a> item.getName());<a name="353" href="#353">353</a> <strong>try</strong> {<a name="354" href="#354">354</a> Streams.copy(item.openStream(), fileItem.getOutputStream(),<a name="355" href="#355">355</a> <strong>true</strong>);<a name="356" href="#356">356</a> } <strong>catch</strong> (FileUploadIOException e) {<a name="357" href="#357">357</a> <strong>throw</strong> (FileUploadException) e.getCause();<a name="358" href="#358">358</a> } <strong>catch</strong> (IOException e) {<a name="359" href="#359">359</a> <strong>throw</strong> <strong>new</strong> IOFileUploadException(<a name="360" href="#360">360</a> <span class="string">"Processing of "</span> + MULTIPART_FORM_DATA<a name="361" href="#361">361</a> + <span class="string">" request failed. "</span> + e.getMessage(), e);<a name="362" href="#362">362</a> }<a name="363" href="#363">363</a> items.add(fileItem);<a name="364" href="#364">364</a> }<a name="365" href="#365">365</a> <strong>return</strong> items;<a name="366" href="#366">366</a> } <strong>catch</strong> (FileUploadIOException e) {<a name="367" href="#367">367</a> <strong>throw</strong> (FileUploadException) e.getCause();<a name="368" href="#368">368</a> } <strong>catch</strong> (IOException e) {<a name="369" href="#369">369</a> <strong>throw</strong> <strong>new</strong> <a href="../../../../org/apache/commons/fileupload/FileUploadException.html">FileUploadException</a>(e.getMessage(), e);<a name="370" href="#370">370</a> }<a name="371" href="#371">371</a> }<a name="372" href="#372">372</a> <a name="373" href="#373">373</a> <a name="374" href="#374">374</a> <em class="comment">// ------------------------------------------------------ Protected methods</em><a name="375" href="#375">375</a> <a name="376" href="#376">376</a> <a name="377" href="#377">377</a> <em>/**</em><a name="378" href="#378">378</a> <em> * Retrieves the boundary from the <code>Content-type</code> header.</em><a name="379" href="#379">379</a> <em> *</em><a name="380" href="#380">380</a> <em> * @param contentType The value of the content type header from which to</em><a name="381" href="#381">381</a> <em> * extract the boundary value.</em><a name="382" href="#382">382</a> <em> *</em><a name="383" href="#383">383</a> <em> * @return The boundary, as a byte array.</em><a name="384" href="#384">384</a> <em> */</em><a name="385" href="#385">385</a> <strong>protected</strong> byte[] getBoundary(String contentType) {<a name="386" href="#386">386</a> <a href="../../../../org/apache/commons/fileupload/ParameterParser.html">ParameterParser</a> parser = <strong>new</strong> <a href="../../../../org/apache/commons/fileupload/ParameterParser.html">ParameterParser</a>();<a name="387" href="#387">387</a> parser.setLowerCaseNames(<strong>true</strong>);<a name="388" href="#388">388</a> <em class="comment">// Parameter parser can handle null input</em><a name="389" href="#389">389</a> Map params = parser.parse(contentType, ';');<a name="390" href="#390">390</a> String boundaryStr = (String) params.get(<span class="string">"boundary"</span>);<a name="391" href="#391">391</a> <a name="392" href="#392">392</a> <strong>if</strong> (boundaryStr == <strong>null</strong>) {<a name="393" href="#393">393</a> <strong>return</strong> <strong>null</strong>;<a name="394" href="#394">394</a> }<a name="395" href="#395">395</a> byte[] boundary;<a name="396" href="#396">396</a> <strong>try</strong> {<a name="397" href="#397">397</a> boundary = boundaryStr.getBytes(<span class="string">"ISO-8859-1"</span>);<a name="398" href="#398">398</a> } <strong>catch</strong> (UnsupportedEncodingException e) {<a name="399" href="#399">399</a> boundary = boundaryStr.getBytes();<a name="400" href="#400">400</a> }<a name="401" href="#401">401</a> <strong>return</strong> boundary;<a name="402" href="#402">402</a> }<a name="403" href="#403">403</a> <a name="404" href="#404">404</a> <a name="405" href="#405">405</a> <em>/**</em><a name="406" href="#406">406</a> <em> * Retrieves the file name from the <code>Content-disposition</code></em><a name="407" href="#407">407</a> <em> * header.</em><a name="408" href="#408">408</a> <em> *</em><a name="409" href="#409">409</a> <em> * @param headers A <code>Map</code> containing the HTTP request headers.</em><a name="410" href="#410">410</a> <em> *</em><a name="411" href="#411">411</a> <em> * @return The file name for the current <code>encapsulation</code>.</em><a name="412" href="#412">412</a> <em> */</em><a name="413" href="#413">413</a> <strong>protected</strong> String getFileName(Map <em class="comment">/*<em class="comment"> String, String */</em> headers) {</em><a name="414" href="#414">414</a> String fileName = <strong>null</strong>;<a name="415" href="#415">415</a> String cd = getHeader(headers, CONTENT_DISPOSITION);<a name="416" href="#416">416</a> <strong>if</strong> (cd != <strong>null</strong>) {<a name="417" href="#417">417</a> String cdl = cd.toLowerCase();<a name="418" href="#418">418</a> <strong>if</strong> (cdl.startsWith(FORM_DATA) || cdl.startsWith(ATTACHMENT)) {<a name="419" href="#419">419</a> <a href="../../../../org/apache/commons/fileupload/ParameterParser.html">ParameterParser</a> parser = <strong>new</strong> <a href="../../../../org/apache/commons/fileupload/ParameterParser.html">ParameterParser</a>();<a name="420" href="#420">420</a> parser.setLowerCaseNames(<strong>true</strong>);<a name="421" href="#421">421</a> <em class="comment">// Parameter parser can handle null input</em><a name="422" href="#422">422</a> Map params = parser.parse(cd, ';');<a name="423" href="#423">423</a> <strong>if</strong> (params.containsKey(<span class="string">"filename"</span>)) {<a name="424" href="#424">424</a> fileName = (String) params.get(<span class="string">"filename"</span>);<a name="425" href="#425">425</a> <strong>if</strong> (fileName != <strong>null</strong>) {<a name="426" href="#426">426</a> fileName = fileName.trim();<a name="427" href="#427">427</a> } <strong>else</strong> {<a name="428" href="#428">428</a> <em class="comment">// Even if there is no value, the parameter is present,</em><a name="429" href="#429">429</a> <em class="comment">// so we return an empty file name rather than no file</em><a name="430" href="#430">430</a> <em class="comment">// name.</em><a name="431" href="#431">431</a> fileName = <span class="string">""</span>;<a name="432" href="#432">432</a> }<a name="433" href="#433">433</a> }<a name="434" href="#434">434</a> }<a name="435" href="#435">435</a> }<a name="436" href="#436">436</a> <strong>return</strong> fileName;<a name="437" href="#437">437</a> }<a name="438" href="#438">438</a> <a name="439" href="#439">439</a> <a name="440" href="#440">440</a> <em>/**</em><a name="441" href="#441">441</a> <em> * Retrieves the field name from the <code>Content-disposition</code></em><a name="442" href="#442">442</a> <em> * header.</em><a name="443" href="#443">443</a> <em> *</em><a name="444" href="#444">444</a> <em> * @param headers A <code>Map</code> containing the HTTP request headers.</em><a name="445" href="#445">445</a> <em> *</em><a name="446" href="#446">446</a> <em> * @return The field name for the current <code>encapsulation</code>.</em><a name="447" href="#447">447</a> <em> */</em><a name="448" href="#448">448</a> <strong>protected</strong> String getFieldName(Map <em class="comment">/*<em class="comment"> String, String */</em> headers) {</em><a name="449" href="#449">449</a> String fieldName = <strong>null</strong>;<a name="450" href="#450">450</a> String cd = getHeader(headers, CONTENT_DISPOSITION);<a name="451" href="#451">451</a> <strong>if</strong> (cd != <strong>null</strong> && cd.toLowerCase().startsWith(FORM_DATA)) {<a name="452" href="#452">452</a> <a name="453" href="#453">453</a> <a href="../../../../org/apache/commons/fileupload/ParameterParser.html">ParameterParser</a> parser = <strong>new</strong> <a href="../../../../org/apache/commons/fileupload/ParameterParser.html">ParameterParser</a>();<a name="454" href="#454">454</a> parser.setLowerCaseNames(<strong>true</strong>);<a name="455" href="#455">455</a> <em class="comment">// Parameter parser can handle null input</em><a name="456" href="#456">456</a> Map params = parser.parse(cd, ';');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -