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

📄 diskfileitem.html

📁 java 的上传功能很强大的实施上述事实上说是
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<a name="265" href="#265">265</a>     }<a name="266" href="#266">266</a> <a name="267" href="#267">267</a> <a name="268" href="#268">268</a>     <em class="jxr_javadoccomment">/**</em><a name="269" href="#269">269</a> <em class="jxr_javadoccomment">     * Returns the original filename in the client's filesystem.</em><a name="270" href="#270">270</a> <em class="jxr_javadoccomment">     *</em><a name="271" href="#271">271</a> <em class="jxr_javadoccomment">     * @return The original filename in the client's filesystem.</em><a name="272" href="#272">272</a> <em class="jxr_javadoccomment">     */</em><a name="273" href="#273">273</a>     <strong class="jxr_keyword">public</strong> String getName() {<a name="274" href="#274">274</a>         <strong class="jxr_keyword">return</strong> fileName;<a name="275" href="#275">275</a>     }<a name="276" href="#276">276</a> <a name="277" href="#277">277</a> <a name="278" href="#278">278</a>     <em class="jxr_comment">// ------------------------------------------------------- FileItem methods</em><a name="279" href="#279">279</a> <a name="280" href="#280">280</a> <a name="281" href="#281">281</a>     <em class="jxr_javadoccomment">/**</em><a name="282" href="#282">282</a> <em class="jxr_javadoccomment">     * Provides a hint as to whether or not the file contents will be read</em><a name="283" href="#283">283</a> <em class="jxr_javadoccomment">     * from memory.</em><a name="284" href="#284">284</a> <em class="jxr_javadoccomment">     *</em><a name="285" href="#285">285</a> <em class="jxr_javadoccomment">     * @return &lt;code&gt;true&lt;/code&gt; if the file contents will be read</em><a name="286" href="#286">286</a> <em class="jxr_javadoccomment">     *         from memory; &lt;code&gt;false&lt;/code&gt; otherwise.</em><a name="287" href="#287">287</a> <em class="jxr_javadoccomment">     */</em><a name="288" href="#288">288</a>     <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">boolean</strong> isInMemory() {<a name="289" href="#289">289</a>         <strong class="jxr_keyword">if</strong> (cachedContent != <strong class="jxr_keyword">null</strong>) {<a name="290" href="#290">290</a>             <strong class="jxr_keyword">return</strong> <strong class="jxr_keyword">true</strong>;<a name="291" href="#291">291</a>         }<a name="292" href="#292">292</a>         <strong class="jxr_keyword">return</strong> dfos.isInMemory();<a name="293" href="#293">293</a>     }<a name="294" href="#294">294</a> <a name="295" href="#295">295</a> <a name="296" href="#296">296</a>     <em class="jxr_javadoccomment">/**</em><a name="297" href="#297">297</a> <em class="jxr_javadoccomment">     * Returns the size of the file.</em><a name="298" href="#298">298</a> <em class="jxr_javadoccomment">     *</em><a name="299" href="#299">299</a> <em class="jxr_javadoccomment">     * @return The size of the file, in bytes.</em><a name="300" href="#300">300</a> <em class="jxr_javadoccomment">     */</em><a name="301" href="#301">301</a>     <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">long</strong> getSize() {<a name="302" href="#302">302</a>         <strong class="jxr_keyword">if</strong> (size &gt;= 0) {<a name="303" href="#303">303</a>             <strong class="jxr_keyword">return</strong> size;<a name="304" href="#304">304</a>         } <strong class="jxr_keyword">else</strong> <strong class="jxr_keyword">if</strong> (cachedContent != <strong class="jxr_keyword">null</strong>) {<a name="305" href="#305">305</a>             <strong class="jxr_keyword">return</strong> cachedContent.length;<a name="306" href="#306">306</a>         } <strong class="jxr_keyword">else</strong> <strong class="jxr_keyword">if</strong> (dfos.isInMemory()) {<a name="307" href="#307">307</a>             <strong class="jxr_keyword">return</strong> dfos.getData().length;<a name="308" href="#308">308</a>         } <strong class="jxr_keyword">else</strong> {<a name="309" href="#309">309</a>             <strong class="jxr_keyword">return</strong> dfos.getFile().length();<a name="310" href="#310">310</a>         }<a name="311" href="#311">311</a>     }<a name="312" href="#312">312</a> <a name="313" href="#313">313</a> <a name="314" href="#314">314</a>     <em class="jxr_javadoccomment">/**</em><a name="315" href="#315">315</a> <em class="jxr_javadoccomment">     * Returns the contents of the file as an array of bytes.  If the</em><a name="316" href="#316">316</a> <em class="jxr_javadoccomment">     * contents of the file were not yet cached in memory, they will be</em><a name="317" href="#317">317</a> <em class="jxr_javadoccomment">     * loaded from the disk storage and cached.</em><a name="318" href="#318">318</a> <em class="jxr_javadoccomment">     *</em><a name="319" href="#319">319</a> <em class="jxr_javadoccomment">     * @return The contents of the file as an array of bytes.</em><a name="320" href="#320">320</a> <em class="jxr_javadoccomment">     */</em><a name="321" href="#321">321</a>     <strong class="jxr_keyword">public</strong> byte[] get() {<a name="322" href="#322">322</a>         <strong class="jxr_keyword">if</strong> (isInMemory()) {<a name="323" href="#323">323</a>             <strong class="jxr_keyword">if</strong> (cachedContent == <strong class="jxr_keyword">null</strong>) {<a name="324" href="#324">324</a>                 cachedContent = dfos.getData();<a name="325" href="#325">325</a>             }<a name="326" href="#326">326</a>             <strong class="jxr_keyword">return</strong> cachedContent;<a name="327" href="#327">327</a>         }<a name="328" href="#328">328</a> <a name="329" href="#329">329</a>         byte[] fileData = <strong class="jxr_keyword">new</strong> byte[(<strong class="jxr_keyword">int</strong>) getSize()];<a name="330" href="#330">330</a>         FileInputStream fis = <strong class="jxr_keyword">null</strong>;<a name="331" href="#331">331</a> <a name="332" href="#332">332</a>         <strong class="jxr_keyword">try</strong> {<a name="333" href="#333">333</a>             fis = <strong class="jxr_keyword">new</strong> FileInputStream(dfos.getFile());<a name="334" href="#334">334</a>             fis.read(fileData);<a name="335" href="#335">335</a>         } <strong class="jxr_keyword">catch</strong> (IOException e) {<a name="336" href="#336">336</a>             fileData = <strong class="jxr_keyword">null</strong>;<a name="337" href="#337">337</a>         } <strong class="jxr_keyword">finally</strong> {<a name="338" href="#338">338</a>             <strong class="jxr_keyword">if</strong> (fis != <strong class="jxr_keyword">null</strong>) {<a name="339" href="#339">339</a>                 <strong class="jxr_keyword">try</strong> {<a name="340" href="#340">340</a>                     fis.close();<a name="341" href="#341">341</a>                 } <strong class="jxr_keyword">catch</strong> (IOException e) {<a name="342" href="#342">342</a>                     <em class="jxr_comment">// ignore</em><a name="343" href="#343">343</a>                 }<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>         <strong class="jxr_keyword">return</strong> fileData;<a name="348" href="#348">348</a>     }<a name="349" href="#349">349</a> <a name="350" href="#350">350</a> <a name="351" href="#351">351</a>     <em class="jxr_javadoccomment">/**</em><a name="352" href="#352">352</a> <em class="jxr_javadoccomment">     * Returns the contents of the file as a String, using the specified</em><a name="353" href="#353">353</a> <em class="jxr_javadoccomment">     * encoding.  This method uses {@link #get()} to retrieve the</em><a name="354" href="#354">354</a> <em class="jxr_javadoccomment">     * contents of the file.</em><a name="355" href="#355">355</a> <em class="jxr_javadoccomment">     *</em><a name="356" href="#356">356</a> <em class="jxr_javadoccomment">     * @param charset The charset to use.</em><a name="357" href="#357">357</a> <em class="jxr_javadoccomment">     *</em><a name="358" href="#358">358</a> <em class="jxr_javadoccomment">     * @return The contents of the file, as a string.</em><a name="359" href="#359">359</a> <em class="jxr_javadoccomment">     *</em><a name="360" href="#360">360</a> <em class="jxr_javadoccomment">     * @throws UnsupportedEncodingException if the requested character</em><a name="361" href="#361">361</a> <em class="jxr_javadoccomment">     *                                      encoding is not available.</em><a name="362" href="#362">362</a> <em class="jxr_javadoccomment">     */</em><a name="363" href="#363">363</a>     <strong class="jxr_keyword">public</strong> String getString(<strong class="jxr_keyword">final</strong> String charset)<a name="364" href="#364">364</a>         <strong class="jxr_keyword">throws</strong> UnsupportedEncodingException {<a name="365" href="#365">365</a>         <strong class="jxr_keyword">return</strong> <strong class="jxr_keyword">new</strong> String(get(), charset);<a name="366" href="#366">366</a>     }<a name="367" href="#367">367</a> <a name="368" href="#368">368</a> <a name="369" href="#369">369</a>     <em class="jxr_javadoccomment">/**</em><a name="370" href="#370">370</a> <em class="jxr_javadoccomment">     * Returns the contents of the file as a String, using the default</em><a name="371" href="#371">371</a> <em class="jxr_javadoccomment">     * character encoding.  This method uses {@link #get()} to retrieve the</em><a name="372" href="#372">372</a> <em class="jxr_javadoccomment">     * contents of the file.</em><a name="373" href="#373">373</a> <em class="jxr_javadoccomment">     *</em><a name="374" href="#374">374</a> <em class="jxr_javadoccomment">     * @return The contents of the file, as a string.</em><a name="375" href="#375">375</a> <em class="jxr_javadoccomment">     *</em><a name="376" href="#376">376</a> <em class="jxr_javadoccomment">     * @todo Consider making this method throw UnsupportedEncodingException.</em><a name="377" href="#377">377</a> <em class="jxr_javadoccomment">     */</em><a name="378" href="#378">378</a>     <strong class="jxr_keyword">public</strong> String getString() {<a name="379" href="#379">379</a>         byte[] rawdata = get();<a name="380" href="#380">380</a>         String charset = getCharSet();<a name="381" href="#381">381</a>         <strong class="jxr_keyword">if</strong> (charset == <strong class="jxr_keyword">null</strong>) {<a name="382" href="#382">382</a>             charset = DEFAULT_CHARSET;<a name="383" href="#383">383</a>         }<a name="384" href="#384">384</a>         <strong class="jxr_keyword">try</strong> {<a name="385" href="#385">385</a>             <strong class="jxr_keyword">return</strong> <strong class="jxr_keyword">new</strong> String(rawdata, charset);<a name="386" href="#386">386</a>         } <strong class="jxr_keyword">catch</strong> (UnsupportedEncodingException e) {<a name="387" href="#387">387</a>             <strong class="jxr_keyword">return</strong> <strong class="jxr_keyword">new</strong> String(rawdata);<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> <a name="392" href="#392">392</a>     <em class="jxr_javadoccomment">/**</em><a name="393" href="#393">393</a> <em class="jxr_javadoccomment">     * A convenience method to write an uploaded item to disk. The client code</em><a name="394" href="#394">394</a> <em class="jxr_javadoccomment">     * is not concerned with whether or not the item is stored in memory, or on</em><a name="395" href="#395">395</a> <em class="jxr_javadoccomment">     * disk in a temporary location. They just want to write the uploaded item</em><a name="396" href="#396">396</a> <em class="jxr_javadoccomment">     * to a file.</em><a name="397" href="#397">397</a> <em class="jxr_javadoccomment">     * &lt;p&gt;</em><a name="398" href="#398">398</a> <em class="jxr_javadoccomment">     * This implementation first attempts to rename the uploaded item to the</em><a name="399" href="#399">399</a> <em class="jxr_javadoccomment">     * specified destination file, if the item was originally written to disk.</em><a name="400" href="#400">400</a> <em class="jxr_javadoccomment">     * Otherwise, the data will be copied to the specified file.</em><a name="401" href="#401">401</a> <em class="jxr_javadoccomment">     * &lt;p&gt;</em>

⌨️ 快捷键说明

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