⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multipartstream.html

📁 最好的java上传组件
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="328" href="#328">328</a> <em>     *</em><a name="329" href="#329">329</a> <em>     * @throws IOException if there is no more data available.</em><a name="330" href="#330">330</a> <em>     */</em><a name="331" href="#331">331</a>     <strong>public</strong> byte readByte()<a name="332" href="#332">332</a>         throws IOException {<a name="333" href="#333">333</a>         <em class="comment">// Buffer depleted ?</em><a name="334" href="#334">334</a>         <strong>if</strong> (head == tail) {<a name="335" href="#335">335</a>             head = 0;<a name="336" href="#336">336</a>             <em class="comment">// Refill.</em><a name="337" href="#337">337</a>             tail = input.read(buffer, head, bufSize);<a name="338" href="#338">338</a>             <strong>if</strong> (tail == -1) {<a name="339" href="#339">339</a>                 <em class="comment">// No more data available.</em><a name="340" href="#340">340</a>                 <strong>throw</strong> <strong>new</strong> IOException(<span class="string">"No more data is available"</span>);<a name="341" href="#341">341</a>             }<a name="342" href="#342">342</a>         }<a name="343" href="#343">343</a>         <strong>return</strong> buffer[head++];<a name="344" href="#344">344</a>     }<a name="345" href="#345">345</a> <a name="346" href="#346">346</a> <a name="347" href="#347">347</a>     <em>/**<em>*</em></em><a name="348" href="#348">348</a> <em>     * Skips a &lt;code>boundary&lt;/code> token, and checks whether more</em><a name="349" href="#349">349</a> <em>     * &lt;code>encapsulations&lt;/code> are contained in the stream.</em><a name="350" href="#350">350</a> <em>     *</em><a name="351" href="#351">351</a> <em>     * @return &lt;code>true&lt;/code> if there are more encapsulations in</em><a name="352" href="#352">352</a> <em>     *         this stream; &lt;code>false&lt;/code> otherwise.</em><a name="353" href="#353">353</a> <em>     *</em><a name="354" href="#354">354</a> <em>     * @throws MalformedStreamException if the stream ends unexpecetedly or</em><a name="355" href="#355">355</a> <em>     *                                  fails to follow required syntax.</em><a name="356" href="#356">356</a> <em>     */</em><a name="357" href="#357">357</a>     <strong>public</strong> <strong>boolean</strong> readBoundary()<a name="358" href="#358">358</a>         throws MalformedStreamException {<a name="359" href="#359">359</a>         byte[] marker = <strong>new</strong> byte[2];<a name="360" href="#360">360</a>         <strong>boolean</strong> nextChunk = false;<a name="361" href="#361">361</a> <a name="362" href="#362">362</a>         head += boundaryLength;<a name="363" href="#363">363</a>         <strong>try</strong> {<a name="364" href="#364">364</a>             marker[0] = readByte();<a name="365" href="#365">365</a>             <strong>if</strong> (marker[0] == LF) {<a name="366" href="#366">366</a>                 <em class="comment">// Work around IE5 Mac bug with input type=image.</em><a name="367" href="#367">367</a>                 <em class="comment">// Because the boundary delimiter, not including the trailing</em><a name="368" href="#368">368</a>                 <em class="comment">// CRLF, must not appear within any file (RFC 2046, section</em><a name="369" href="#369">369</a>                 <em class="comment">// 5.1.1), we know the missing CR is due to a buggy browser</em><a name="370" href="#370">370</a>                 <em class="comment">// rather than a file containing something similar to a</em><a name="371" href="#371">371</a>                 <em class="comment">// boundary.</em><a name="372" href="#372">372</a>                 <strong>return</strong> <strong>true</strong>;<a name="373" href="#373">373</a>             }<a name="374" href="#374">374</a> <a name="375" href="#375">375</a>             marker[1] = readByte();<a name="376" href="#376">376</a>             <strong>if</strong> (arrayequals(marker, STREAM_TERMINATOR, 2)) {<a name="377" href="#377">377</a>                 nextChunk = false;<a name="378" href="#378">378</a>             } <strong>else</strong> <strong>if</strong> (arrayequals(marker, FIELD_SEPARATOR, 2)) {<a name="379" href="#379">379</a>                 nextChunk = <strong>true</strong>;<a name="380" href="#380">380</a>             } <strong>else</strong> {<a name="381" href="#381">381</a>                 <strong>throw</strong> <strong>new</strong> MalformedStreamException(<a name="382" href="#382">382</a>                         <span class="string">"Unexpected characters follow a boundary"</span>);<a name="383" href="#383">383</a>             }<a name="384" href="#384">384</a>         } <strong>catch</strong> (IOException e) {<a name="385" href="#385">385</a>             <strong>throw</strong> <strong>new</strong> MalformedStreamException(<span class="string">"Stream ended unexpectedly"</span>);<a name="386" href="#386">386</a>         }<a name="387" href="#387">387</a>         <strong>return</strong> nextChunk;<a name="388" href="#388">388</a>     }<a name="389" href="#389">389</a> <a name="390" href="#390">390</a> <a name="391" href="#391">391</a>     <em>/**<em>*</em></em><a name="392" href="#392">392</a> <em>     * &lt;p>Changes the boundary token used for partitioning the stream.</em><a name="393" href="#393">393</a> <em>     *</em><a name="394" href="#394">394</a> <em>     * &lt;p>This method allows single pass processing of nested multipart</em><a name="395" href="#395">395</a> <em>     * streams.</em><a name="396" href="#396">396</a> <em>     *</em><a name="397" href="#397">397</a> <em>     * &lt;p>The boundary token of the nested stream is &lt;code>required&lt;/code></em><a name="398" href="#398">398</a> <em>     * to be of the same length as the boundary token in parent stream.</em><a name="399" href="#399">399</a> <em>     *</em><a name="400" href="#400">400</a> <em>     * &lt;p>Restoring the parent stream boundary token after processing of a</em><a name="401" href="#401">401</a> <em>     * nested stream is left to the application.</em><a name="402" href="#402">402</a> <em>     *</em><a name="403" href="#403">403</a> <em>     * @param boundary The boundary to be used for parsing of the nested</em><a name="404" href="#404">404</a> <em>     *                 stream.</em><a name="405" href="#405">405</a> <em>     *</em><a name="406" href="#406">406</a> <em>     * @throws IllegalBoundaryException if the &lt;code>boundary&lt;/code></em><a name="407" href="#407">407</a> <em>     *                                  has a different length than the one</em><a name="408" href="#408">408</a> <em>     *                                  being currently parsed.</em><a name="409" href="#409">409</a> <em>     */</em><a name="410" href="#410">410</a>     <strong>public</strong> <strong>void</strong> setBoundary(byte[] boundary)<a name="411" href="#411">411</a>         throws IllegalBoundaryException {<a name="412" href="#412">412</a>         <strong>if</strong> (boundary.length != boundaryLength - BOUNDARY_PREFIX.length) {<a name="413" href="#413">413</a>             <strong>throw</strong> <strong>new</strong> IllegalBoundaryException(<a name="414" href="#414">414</a>                     <span class="string">"The length of a boundary token can not be changed"</span>);<a name="415" href="#415">415</a>         }<a name="416" href="#416">416</a>         System.arraycopy(boundary, 0, <strong>this</strong>.boundary, BOUNDARY_PREFIX.length,<a name="417" href="#417">417</a>                 boundary.length);<a name="418" href="#418">418</a>     }<a name="419" href="#419">419</a> <a name="420" href="#420">420</a> <a name="421" href="#421">421</a>     <em>/**<em>*</em></em><a name="422" href="#422">422</a> <em>     * &lt;p>Reads the &lt;code>header-part&lt;/code> of the current</em><a name="423" href="#423">423</a> <em>     * &lt;code>encapsulation&lt;/code>.</em><a name="424" href="#424">424</a> <em>     *</em><a name="425" href="#425">425</a> <em>     * &lt;p>Headers are returned verbatim to the input stream, including the</em><a name="426" href="#426">426</a> <em>     * trailing &lt;code>CRLF&lt;/code> marker. Parsing is left to the</em><a name="427" href="#427">427</a> <em>     * application.</em><a name="428" href="#428">428</a> <em>     *</em><a name="429" href="#429">429</a> <em>     * &lt;p>&lt;strong>TODO&lt;/strong> allow limiting maximum header size to</em><a name="430" href="#430">430</a> <em>     * protect against abuse.</em><a name="431" href="#431">431</a> <em>     *</em><a name="432" href="#432">432</a> <em>     * @return The &lt;code>header-part&lt;/code> of the current encapsulation.</em><a name="433" href="#433">433</a> <em>     *</em><a name="434" href="#434">434</a> <em>     * @throws MalformedStreamException if the stream ends unexpecetedly.</em><a name="435" href="#435">435</a> <em>     */</em><a name="436" href="#436">436</a>     <strong>public</strong> String readHeaders()<a name="437" href="#437">437</a>         throws MalformedStreamException {<a name="438" href="#438">438</a>         <strong>int</strong> i = 0;<a name="439" href="#439">439</a>         byte[] b = <strong>new</strong> byte[1];<a name="440" href="#440">440</a>         <em class="comment">// to support multi-byte characters</em><a name="441" href="#441">441</a>         ByteArrayOutputStream baos = <strong>new</strong> ByteArrayOutputStream();<a name="442" href="#442">442</a>         <strong>int</strong> sizeMax = HEADER_PART_SIZE_MAX;<a name="443" href="#443">443</a>         <strong>int</strong> size = 0;<a name="444" href="#444">444</a>         <strong>while</strong> (i &lt; HEADER_SEPARATOR.length) {<a name="445" href="#445">445</a>             <strong>try</strong> {<a name="446" href="#446">446</a>                 b[0] = readByte();<a name="447" href="#447">447</a>             } <strong>catch</strong> (IOException e) {<a name="448" href="#448">448</a>                 <strong>throw</strong> <strong>new</strong> MalformedStreamException(<span class="string">"Stream ended unexpectedly"</span>);<a name="449" href="#449">449</a>             }<a name="450" href="#450">450</a>             size++;<a name="451" href="#451">451</a>             <strong>if</strong> (b[0] == HEADER_SEPARATOR[i]) {<a name="452" href="#452">452</a>                 i++;<a name="453" href="#453">453</a>             } <strong>else</strong> {<a name="454" href="#454">454</a>                 i = 0;<a name="455" href="#455">455</a>             }<a name="456" href="#456">456</a>             <strong>if</strong> (size &lt;= sizeMax) {<a name="457" href="#457">457</a>                 baos.write(b[0]);<a name="458" href="#458">458</a>             }<a name="459" href="#459">459</a>         }<a name="460" href="#460">460</a> <a name="461" href="#461">461</a>         String headers = <strong>null</strong>;<a name="462" href="#462">462</a>         <strong>if</strong> (headerEncoding != <strong>null</strong>) {<a name="463" href="#463">463</a>             <strong>try</strong> {<a name="464" href="#464">464</a>                 headers = baos.toString(headerEncoding);<a name="465" href="#465">465</a>             } <strong>catch</strong> (UnsupportedEncodingException e) {<a name="466" href="#466">466</a>                 <em class="comment">// Fall back to platform default if specified encoding is not</em><a name="467" href="#467">467</a>                 <em class="comment">// supported.</em><a name="468" href="#468">468</a>                 headers = baos.toString();<a name="469" href="#469">469</a>             }<a name="470" href="#470">470</a>         } <strong>else</strong> {<a name="471" href="#471">471</a>             headers = baos.toString();<a name="472" href="#472">472</a>         }<a name="473" href="#473">473</a> <a name="474" href="#474">474</a>         <strong>return</strong> headers;<a name="475" href="#475">475</a>     }<a name="476" href="#476">476</a> <a name="477" href="#477">477</a> <a name="478" href="#478">478</a>     <em>/**<em>*</em></em><a name="479" href="#479">479</a> <em>     * &lt;p>Reads &lt;code>body-data&lt;/code> from the current</em><a name="480" href="#480">480</a> <em>     * &lt;code>encapsulation&lt;/code> and writes its contents into the</em><a name="481" href="#481">481</a> <em>     * output &lt;code>Stream&lt;/code>.</em><a name="482" href="#482">482</a> <em>     *</em><a name="483" href="#483">483</a> <em>     * &lt;p>Arbitrary large amounts of data can be processed by this</em><a name="484" href="#484">484</a> <em>     * method using a constant size buffer. (see {@link</em><a name="485" href="#485">485</a> <em>     * #MultipartStream(InputStream,byte[],int) constructor}).</em><a name="486" href="#486">486</a> <em>     *</em><a name="487" href="#487">487</a> <em>     * @param output The &lt;code>Stream&lt;/code> to write data into.</em><a name="488" href="#488">488</a> <em>     *</em><a name="489" href="#489">489</a> <em>     * @return the amount of data written.</em><a name="490" href="#490">490</a> <em>     *</em><a name="491" href="#491">491</a> <em>     * @throws MalformedStreamException if the stream ends unexpectedly.</em><a name="492" href="#492">492</a> <em>     * @throws IOException              if an i/o error occurs.</em><a name="493" href="#493">493</a> <em>     */</em><a name="494" href="#494">494</a>     <strong>public</strong> <strong>int</strong> readBodyData(OutputStream output)<a name="495" href="#495">495</a>         throws MalformedStreamException,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -