📄 diskfileitem.html
字号:
<a name="402" href="#402">402</a> <em class="jxr_javadoccomment"> * This method is only guaranteed to work <em>once</em>, the first time it</em><a name="403" href="#403">403</a> <em class="jxr_javadoccomment"> * is invoked for a particular item. This is because, in the event that the</em><a name="404" href="#404">404</a> <em class="jxr_javadoccomment"> * method renames a temporary file, that file will no longer be available</em><a name="405" href="#405">405</a> <em class="jxr_javadoccomment"> * to copy or rename again at a later time.</em><a name="406" href="#406">406</a> <em class="jxr_javadoccomment"> *</em><a name="407" href="#407">407</a> <em class="jxr_javadoccomment"> * @param file The <code>File</code> into which the uploaded item should</em><a name="408" href="#408">408</a> <em class="jxr_javadoccomment"> * be stored.</em><a name="409" href="#409">409</a> <em class="jxr_javadoccomment"> *</em><a name="410" href="#410">410</a> <em class="jxr_javadoccomment"> * @throws Exception if an error occurs.</em><a name="411" href="#411">411</a> <em class="jxr_javadoccomment"> */</em><a name="412" href="#412">412</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> write(File file) <strong class="jxr_keyword">throws</strong> Exception {<a name="413" href="#413">413</a> <strong class="jxr_keyword">if</strong> (isInMemory()) {<a name="414" href="#414">414</a> FileOutputStream fout = <strong class="jxr_keyword">null</strong>;<a name="415" href="#415">415</a> <strong class="jxr_keyword">try</strong> {<a name="416" href="#416">416</a> fout = <strong class="jxr_keyword">new</strong> FileOutputStream(file);<a name="417" href="#417">417</a> fout.write(get());<a name="418" href="#418">418</a> } <strong class="jxr_keyword">finally</strong> {<a name="419" href="#419">419</a> <strong class="jxr_keyword">if</strong> (fout != <strong class="jxr_keyword">null</strong>) {<a name="420" href="#420">420</a> fout.close();<a name="421" href="#421">421</a> }<a name="422" href="#422">422</a> }<a name="423" href="#423">423</a> } <strong class="jxr_keyword">else</strong> {<a name="424" href="#424">424</a> File outputFile = getStoreLocation();<a name="425" href="#425">425</a> <strong class="jxr_keyword">if</strong> (outputFile != <strong class="jxr_keyword">null</strong>) {<a name="426" href="#426">426</a> <em class="jxr_comment">// Save the length of the file</em><a name="427" href="#427">427</a> size = outputFile.length();<a name="428" href="#428">428</a> <em class="jxr_comment">/*</em><a name="429" href="#429">429</a> <em class="jxr_comment"> * The uploaded file is being stored on disk</em><a name="430" href="#430">430</a> <em class="jxr_comment"> * in a temporary location so move it to the</em><a name="431" href="#431">431</a> <em class="jxr_comment"> * desired file.</em><a name="432" href="#432">432</a> <em class="jxr_comment"> */</em><a name="433" href="#433">433</a> <strong class="jxr_keyword">if</strong> (!outputFile.renameTo(file)) {<a name="434" href="#434">434</a> BufferedInputStream in = <strong class="jxr_keyword">null</strong>;<a name="435" href="#435">435</a> BufferedOutputStream out = <strong class="jxr_keyword">null</strong>;<a name="436" href="#436">436</a> <strong class="jxr_keyword">try</strong> {<a name="437" href="#437">437</a> in = <strong class="jxr_keyword">new</strong> BufferedInputStream(<a name="438" href="#438">438</a> <strong class="jxr_keyword">new</strong> FileInputStream(outputFile));<a name="439" href="#439">439</a> out = <strong class="jxr_keyword">new</strong> BufferedOutputStream(<a name="440" href="#440">440</a> <strong class="jxr_keyword">new</strong> FileOutputStream(file));<a name="441" href="#441">441</a> IOUtils.copy(in, out);<a name="442" href="#442">442</a> } <strong class="jxr_keyword">finally</strong> {<a name="443" href="#443">443</a> <strong class="jxr_keyword">if</strong> (in != <strong class="jxr_keyword">null</strong>) {<a name="444" href="#444">444</a> <strong class="jxr_keyword">try</strong> {<a name="445" href="#445">445</a> in.close();<a name="446" href="#446">446</a> } <strong class="jxr_keyword">catch</strong> (IOException e) {<a name="447" href="#447">447</a> <em class="jxr_comment">// ignore</em><a name="448" href="#448">448</a> }<a name="449" href="#449">449</a> }<a name="450" href="#450">450</a> <strong class="jxr_keyword">if</strong> (out != <strong class="jxr_keyword">null</strong>) {<a name="451" href="#451">451</a> <strong class="jxr_keyword">try</strong> {<a name="452" href="#452">452</a> out.close();<a name="453" href="#453">453</a> } <strong class="jxr_keyword">catch</strong> (IOException e) {<a name="454" href="#454">454</a> <em class="jxr_comment">// ignore</em><a name="455" href="#455">455</a> }<a name="456" href="#456">456</a> }<a name="457" href="#457">457</a> }<a name="458" href="#458">458</a> }<a name="459" href="#459">459</a> } <strong class="jxr_keyword">else</strong> {<a name="460" href="#460">460</a> <em class="jxr_comment">/*</em><a name="461" href="#461">461</a> <em class="jxr_comment"> * For whatever reason we cannot write the</em><a name="462" href="#462">462</a> <em class="jxr_comment"> * file to disk.</em><a name="463" href="#463">463</a> <em class="jxr_comment"> */</em><a name="464" href="#464">464</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> <a href="../../../../../org/apache/commons/fileupload/FileUploadException.html">FileUploadException</a>(<a name="465" href="#465">465</a> <span class="jxr_string">"Cannot write uploaded file to disk!"</span>);<a name="466" href="#466">466</a> }<a name="467" href="#467">467</a> }<a name="468" href="#468">468</a> }<a name="469" href="#469">469</a> <a name="470" href="#470">470</a> <a name="471" href="#471">471</a> <em class="jxr_javadoccomment">/**</em><a name="472" href="#472">472</a> <em class="jxr_javadoccomment"> * Deletes the underlying storage for a file item, including deleting any</em><a name="473" href="#473">473</a> <em class="jxr_javadoccomment"> * associated temporary disk file. Although this storage will be deleted</em><a name="474" href="#474">474</a> <em class="jxr_javadoccomment"> * automatically when the <code>FileItem</code> instance is garbage</em><a name="475" href="#475">475</a> <em class="jxr_javadoccomment"> * collected, this method can be used to ensure that this is done at an</em><a name="476" href="#476">476</a> <em class="jxr_javadoccomment"> * earlier time, thus preserving system resources.</em><a name="477" href="#477">477</a> <em class="jxr_javadoccomment"> */</em><a name="478" href="#478">478</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> delete() {<a name="479" href="#479">479</a> cachedContent = <strong class="jxr_keyword">null</strong>;<a name="480" href="#480">480</a> File outputFile = getStoreLocation();<a name="481" href="#481">481</a> <strong class="jxr_keyword">if</strong> (outputFile != <strong class="jxr_keyword">null</strong> && outputFile.exists()) {<a name="482" href="#482">482</a> outputFile.delete();<a name="483" href="#483">483</a> }<a name="484" href="#484">484</a> }<a name="485" href="#485">485</a> <a name="486" href="#486">486</a> <a name="487" href="#487">487</a> <em class="jxr_javadoccomment">/**</em><a name="488" href="#488">488</a> <em class="jxr_javadoccomment"> * Returns the name of the field in the multipart form corresponding to</em><a name="489" href="#489">489</a> <em class="jxr_javadoccomment"> * this file item.</em><a name="490" href="#490">490</a> <em class="jxr_javadoccomment"> *</em><a name="491" href="#491">491</a> <em class="jxr_javadoccomment"> * @return The name of the form field.</em><a name="492" href="#492">492</a> <em class="jxr_javadoccomment"> *</em><a name="493" href="#493">493</a> <em class="jxr_javadoccomment"> * @see #setFieldName(java.lang.String)</em><a name="494" href="#494">494</a> <em class="jxr_javadoccomment"> *</em><a name="495" href="#495">495</a> <em class="jxr_javadoccomment"> */</em><a name="496" href="#496">496</a> <strong class="jxr_keyword">public</strong> String getFieldName() {<a name="497" href="#497">497</a> <strong class="jxr_keyword">return</strong> fieldName;<a name="498" href="#498">498</a> }<a name="499" href="#499">499</a> <a name="500" href="#500">500</a> <a name="501" href="#501">501</a> <em class="jxr_javadoccomment">/**</em><a name="502" href="#502">502</a> <em class="jxr_javadoccomment"> * Sets the field name used to reference this file item.</em><a name="503" href="#503">503</a> <em class="jxr_javadoccomment"> *</em><a name="504" href="#504">504</a> <em class="jxr_javadoccomment"> * @param fieldName The name of the form field.</em><a name="505" href="#505">505</a> <em class="jxr_javadoccomment"> *</em><a name="506" href="#506">506</a> <em class="jxr_javadoccomment"> * @see #getFieldName()</em><a name="507" href="#507">507</a> <em class="jxr_javadoccomment"> *</em><a name="508" href="#508">508</a> <em class="jxr_javadoccomment"> */</em><a name="509" href="#509">509</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> setFieldName(String fieldName) {<a name="510" href="#510">510</a> <strong class="jxr_keyword">this</strong>.fieldName = fieldName;<a name="511" href="#511">511</a> }<a name="512" href="#512">512</a> <a name="513" href="#513">513</a> <a name="514" href="#514">514</a> <em class="jxr_javadoccomment">/**</em><a name="515" href="#515">515</a> <em class="jxr_javadoccomment"> * Determines whether or not a <code>FileItem</code> instance represents</em><a name="516" href="#516">516</a> <em class="jxr_javadoccomment"> * a simple form field.</em><a name="517" href="#517">517</a> <em class="jxr_javadoccomment"> *</em><a name="518" href="#518">518</a> <em class="jxr_javadoccomment"> * @return <code>true</code> if the instance represents a simple form</em><a name="519" href="#519">519</a> <em class="jxr_javadoccomment"> * field; <code>false</code> if it represents an uploaded file.</em><a name="520" href="#520">520</a> <em class="jxr_javadoccomment"> *</em><a name="521" href="#521">521</a> <em class="jxr_javadoccomment"> * @see #setFormField(boolean)</em><a name="522" href="#522">522</a> <em class="jxr_javadoccomment"> *</em><a name="523" href="#523">523</a> <em class="jxr_javadoccomment"> */</em><a name="524" href="#524">524</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">boolean</strong> isFormField() {<a name="525" href="#525">525</a> <strong class="jxr_keyword">return</strong> isFormField;<a name="526" href="#526">526</a> }<a name="527" href="#527">527</a> <a name="528" href="#528">528</a> <a name="529" href="#529">529</a> <em class="jxr_javadoccomment">/**</em><a name="530" href="#530">530</a> <em class="jxr_javadoccomment"> * Specifies whether or not a <code>FileItem</code> instance represents</em><a name="531" href="#531">531</a> <em class="jxr_javadoccomment"> * a simple form field.</em><a name="532" href="#532">532</a> <em class="jxr_javadoccomment"> *</em><a name="533" href="#533">533</a> <em class="jxr_javadoccomment"> * @param state <code>true</code> if the instance represents a simple form</em><a name="534" href="#534">534</a> <em class="jxr_javadoccomment"> * field; <code>false</code> if it represents an uploaded file.</em><a name="535" href="#535">535</a> <em class="jxr_javadoccomment"> *</em><a name="536" href="#536">536</a> <em class="jxr_javadoccomment"> * @see #isFormField()</em><a name="537" href="#537">537</a> <em class="jxr_javadoccomment"> *</em><a name="538" href="#538">538</a> <em class="jxr_javadoccomment"> */</em>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -