📄 diskfileitem.html
字号:
<a name="539" href="#539">539</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> setFormField(<strong class="jxr_keyword">boolean</strong> state) {<a name="540" href="#540">540</a> isFormField = state;<a name="541" href="#541">541</a> }<a name="542" href="#542">542</a> <a name="543" href="#543">543</a> <a name="544" href="#544">544</a> <em class="jxr_javadoccomment">/**</em><a name="545" href="#545">545</a> <em class="jxr_javadoccomment"> * Returns an {@link java.io.OutputStream OutputStream} that can</em><a name="546" href="#546">546</a> <em class="jxr_javadoccomment"> * be used for storing the contents of the file.</em><a name="547" href="#547">547</a> <em class="jxr_javadoccomment"> *</em><a name="548" href="#548">548</a> <em class="jxr_javadoccomment"> * @return An {@link java.io.OutputStream OutputStream} that can be used</em><a name="549" href="#549">549</a> <em class="jxr_javadoccomment"> * for storing the contensts of the file.</em><a name="550" href="#550">550</a> <em class="jxr_javadoccomment"> *</em><a name="551" href="#551">551</a> <em class="jxr_javadoccomment"> * @throws IOException if an error occurs.</em><a name="552" href="#552">552</a> <em class="jxr_javadoccomment"> */</em><a name="553" href="#553">553</a> <strong class="jxr_keyword">public</strong> OutputStream getOutputStream()<a name="554" href="#554">554</a> <strong class="jxr_keyword">throws</strong> IOException {<a name="555" href="#555">555</a> <strong class="jxr_keyword">if</strong> (dfos == <strong class="jxr_keyword">null</strong>) {<a name="556" href="#556">556</a> File outputFile = getTempFile();<a name="557" href="#557">557</a> dfos = <strong class="jxr_keyword">new</strong> DeferredFileOutputStream(sizeThreshold, outputFile);<a name="558" href="#558">558</a> }<a name="559" href="#559">559</a> <strong class="jxr_keyword">return</strong> dfos;<a name="560" href="#560">560</a> }<a name="561" href="#561">561</a> <a name="562" href="#562">562</a> <a name="563" href="#563">563</a> <em class="jxr_comment">// --------------------------------------------------------- Public methods</em><a name="564" href="#564">564</a> <a name="565" href="#565">565</a> <a name="566" href="#566">566</a> <em class="jxr_javadoccomment">/**</em><a name="567" href="#567">567</a> <em class="jxr_javadoccomment"> * Returns the {@link java.io.File} object for the <code>FileItem</code>'s</em><a name="568" href="#568">568</a> <em class="jxr_javadoccomment"> * data's temporary location on the disk. Note that for</em><a name="569" href="#569">569</a> <em class="jxr_javadoccomment"> * <code>FileItem</code>s that have their data stored in memory,</em><a name="570" href="#570">570</a> <em class="jxr_javadoccomment"> * this method will return <code>null</code>. When handling large</em><a name="571" href="#571">571</a> <em class="jxr_javadoccomment"> * files, you can use {@link java.io.File#renameTo(java.io.File)} to</em><a name="572" href="#572">572</a> <em class="jxr_javadoccomment"> * move the file to new location without copying the data, if the</em><a name="573" href="#573">573</a> <em class="jxr_javadoccomment"> * source and destination locations reside within the same logical</em><a name="574" href="#574">574</a> <em class="jxr_javadoccomment"> * volume.</em><a name="575" href="#575">575</a> <em class="jxr_javadoccomment"> *</em><a name="576" href="#576">576</a> <em class="jxr_javadoccomment"> * @return The data file, or <code>null</code> if the data is stored in</em><a name="577" href="#577">577</a> <em class="jxr_javadoccomment"> * memory.</em><a name="578" href="#578">578</a> <em class="jxr_javadoccomment"> */</em><a name="579" href="#579">579</a> <strong class="jxr_keyword">public</strong> File getStoreLocation() {<a name="580" href="#580">580</a> <strong class="jxr_keyword">return</strong> dfos == <strong class="jxr_keyword">null</strong> ? <strong class="jxr_keyword">null</strong> : dfos.getFile();<a name="581" href="#581">581</a> }<a name="582" href="#582">582</a> <a name="583" href="#583">583</a> <a name="584" href="#584">584</a> <em class="jxr_comment">// ------------------------------------------------------ Protected methods</em><a name="585" href="#585">585</a> <a name="586" href="#586">586</a> <a name="587" href="#587">587</a> <em class="jxr_javadoccomment">/**</em><a name="588" href="#588">588</a> <em class="jxr_javadoccomment"> * Removes the file contents from the temporary storage.</em><a name="589" href="#589">589</a> <em class="jxr_javadoccomment"> */</em><a name="590" href="#590">590</a> <strong class="jxr_keyword">protected</strong> <strong class="jxr_keyword">void</strong> finalize() {<a name="591" href="#591">591</a> File outputFile = dfos.getFile();<a name="592" href="#592">592</a> <a name="593" href="#593">593</a> <strong class="jxr_keyword">if</strong> (outputFile != <strong class="jxr_keyword">null</strong> && outputFile.exists()) {<a name="594" href="#594">594</a> outputFile.delete();<a name="595" href="#595">595</a> }<a name="596" href="#596">596</a> }<a name="597" href="#597">597</a> <a name="598" href="#598">598</a> <a name="599" href="#599">599</a> <em class="jxr_javadoccomment">/**</em><a name="600" href="#600">600</a> <em class="jxr_javadoccomment"> * Creates and returns a {@link java.io.File File} representing a uniquely</em><a name="601" href="#601">601</a> <em class="jxr_javadoccomment"> * named temporary file in the configured repository path. The lifetime of</em><a name="602" href="#602">602</a> <em class="jxr_javadoccomment"> * the file is tied to the lifetime of the <code>FileItem</code> instance;</em><a name="603" href="#603">603</a> <em class="jxr_javadoccomment"> * the file will be deleted when the instance is garbage collected.</em><a name="604" href="#604">604</a> <em class="jxr_javadoccomment"> *</em><a name="605" href="#605">605</a> <em class="jxr_javadoccomment"> * @return The {@link java.io.File File} to be used for temporary storage.</em><a name="606" href="#606">606</a> <em class="jxr_javadoccomment"> */</em><a name="607" href="#607">607</a> <strong class="jxr_keyword">protected</strong> File getTempFile() {<a name="608" href="#608">608</a> <strong class="jxr_keyword">if</strong> (tempFile == <strong class="jxr_keyword">null</strong>) {<a name="609" href="#609">609</a> File tempDir = repository;<a name="610" href="#610">610</a> <strong class="jxr_keyword">if</strong> (tempDir == <strong class="jxr_keyword">null</strong>) {<a name="611" href="#611">611</a> tempDir = <strong class="jxr_keyword">new</strong> File(System.getProperty(<span class="jxr_string">"java.io.tmpdir"</span>));<a name="612" href="#612">612</a> }<a name="613" href="#613">613</a> <a name="614" href="#614">614</a> String tempFileName =<a name="615" href="#615">615</a> <span class="jxr_string">"upload_"</span> + UID + <span class="jxr_string">"_"</span> + getUniqueId() + <span class="jxr_string">".tmp"</span>;<a name="616" href="#616">616</a> <a name="617" href="#617">617</a> tempFile = <strong class="jxr_keyword">new</strong> File(tempDir, tempFileName);<a name="618" href="#618">618</a> }<a name="619" href="#619">619</a> <strong class="jxr_keyword">return</strong> tempFile;<a name="620" href="#620">620</a> }<a name="621" href="#621">621</a> <a name="622" href="#622">622</a> <a name="623" href="#623">623</a> <em class="jxr_comment">// -------------------------------------------------------- Private methods</em><a name="624" href="#624">624</a> <a name="625" href="#625">625</a> <a name="626" href="#626">626</a> <em class="jxr_javadoccomment">/**</em><a name="627" href="#627">627</a> <em class="jxr_javadoccomment"> * Returns an identifier that is unique within the class loader used to</em><a name="628" href="#628">628</a> <em class="jxr_javadoccomment"> * load this class, but does not have random-like apearance.</em><a name="629" href="#629">629</a> <em class="jxr_javadoccomment"> *</em><a name="630" href="#630">630</a> <em class="jxr_javadoccomment"> * @return A String with the non-random looking instance identifier.</em><a name="631" href="#631">631</a> <em class="jxr_javadoccomment"> */</em><a name="632" href="#632">632</a> <strong class="jxr_keyword">private</strong> <strong class="jxr_keyword">static</strong> String getUniqueId() {<a name="633" href="#633">633</a> <strong class="jxr_keyword">final</strong> <strong class="jxr_keyword">int</strong> limit = 100000000;<a name="634" href="#634">634</a> <strong class="jxr_keyword">int</strong> current;<a name="635" href="#635">635</a> <strong class="jxr_keyword">synchronized</strong> (DiskFileItem.<strong class="jxr_keyword">class</strong>) {<a name="636" href="#636">636</a> current = counter++;<a name="637" href="#637">637</a> }<a name="638" href="#638">638</a> String id = Integer.toString(current);<a name="639" href="#639">639</a> <a name="640" href="#640">640</a> <em class="jxr_comment">// If you manage to get more than 100 million of ids, you'll</em><a name="641" href="#641">641</a> <em class="jxr_comment">// start getting ids longer than 8 characters.</em><a name="642" href="#642">642</a> <strong class="jxr_keyword">if</strong> (current < limit) {<a name="643" href="#643">643</a> id = (<span class="jxr_string">"00000000"</span> + id).substring(id.length());<a name="644" href="#644">644</a> }<a name="645" href="#645">645</a> <strong class="jxr_keyword">return</strong> id;<a name="646" href="#646">646</a> }<a name="647" href="#647">647</a> <a name="648" href="#648">648</a> <a name="649" href="#649">649</a> <a name="650" href="#650">650</a> <a name="651" href="#651">651</a> <em class="jxr_javadoccomment">/**</em><a name="652" href="#652">652</a> <em class="jxr_javadoccomment"> * Returns a string representation of this object.</em><a name="653" href="#653">653</a> <em class="jxr_javadoccomment"> *</em><a name="654" href="#654">654</a> <em class="jxr_javadoccomment"> * @return a string representation of this object.</em><a name="655" href="#655">655</a> <em class="jxr_javadoccomment"> */</em><a name="656" href="#656">656</a> <strong class="jxr_keyword">public</strong> String toString() {<a name="657" href="#657">657</a> <strong class="jxr_keyword">return</strong> <span class="jxr_string">"name="</span> + <strong class="jxr_keyword">this</strong>.getName()<a name="658" href="#658">658</a> + <span class="jxr_string">", StoreLocation="</span><a name="659" href="#659">659</a> + String.valueOf(<strong class="jxr_keyword">this</strong>.getStoreLocation())<a name="660" href="#660">660</a> + <span class="jxr_string">", size="</span><a name="661" href="#661">661</a> + <strong class="jxr_keyword">this</strong>.getSize()<a name="662" href="#662">662</a> + <span class="jxr_string">"bytes, "</span><a name="663" href="#663">663</a> + <span class="jxr_string">"isFormField="</span> + isFormField()<a name="664" href="#664">664</a> + <span class="jxr_string">", FieldName="</span><a name="665" href="#665">665</a> + <strong class="jxr_keyword">this</strong>.getFieldName();<a name="666" href="#666">666</a> }<a name="667" href="#667">667</a> <a name="668" href="#668">668</a> <a name="669" href="#669">669</a> <em class="jxr_comment">// -------------------------------------------------- Serialization methods</em><a name="670" href="#670">670</a> <a name="671" href="#671">671</a> <a name="672" href="#672">672</a> <em class="jxr_javadoccomment">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -