📄 servletutilities.html
字号:
<FONT color="green">336</FONT> * Binary streams the specified file to the HTTP response in 1KB chunks.<a name="line.336"></a><FONT color="green">337</FONT> *<a name="line.337"></a><FONT color="green">338</FONT> * @param file the file to be streamed.<a name="line.338"></a><FONT color="green">339</FONT> * @param response the HTTP response object.<a name="line.339"></a><FONT color="green">340</FONT> *<a name="line.340"></a><FONT color="green">341</FONT> * @throws IOException if there is an I/O problem.<a name="line.341"></a><FONT color="green">342</FONT> */<a name="line.342"></a><FONT color="green">343</FONT> public static void sendTempFile(File file, HttpServletResponse response)<a name="line.343"></a><FONT color="green">344</FONT> throws IOException {<a name="line.344"></a><FONT color="green">345</FONT> <a name="line.345"></a><FONT color="green">346</FONT> String mimeType = null;<a name="line.346"></a><FONT color="green">347</FONT> String filename = file.getName();<a name="line.347"></a><FONT color="green">348</FONT> if (filename.length() > 5) {<a name="line.348"></a><FONT color="green">349</FONT> if (filename.substring(filename.length() - 5, <a name="line.349"></a><FONT color="green">350</FONT> filename.length()).equals(".jpeg")) {<a name="line.350"></a><FONT color="green">351</FONT> mimeType = "image/jpeg";<a name="line.351"></a><FONT color="green">352</FONT> } <a name="line.352"></a><FONT color="green">353</FONT> else if (filename.substring(filename.length() - 4, <a name="line.353"></a><FONT color="green">354</FONT> filename.length()).equals(".png")) {<a name="line.354"></a><FONT color="green">355</FONT> mimeType = "image/png";<a name="line.355"></a><FONT color="green">356</FONT> }<a name="line.356"></a><FONT color="green">357</FONT> }<a name="line.357"></a><FONT color="green">358</FONT> ServletUtilities.sendTempFile(file, response, mimeType);<a name="line.358"></a><FONT color="green">359</FONT> }<a name="line.359"></a><FONT color="green">360</FONT> <a name="line.360"></a><FONT color="green">361</FONT> /**<a name="line.361"></a><FONT color="green">362</FONT> * Binary streams the specified file to the HTTP response in 1KB chunks.<a name="line.362"></a><FONT color="green">363</FONT> *<a name="line.363"></a><FONT color="green">364</FONT> * @param file the file to be streamed.<a name="line.364"></a><FONT color="green">365</FONT> * @param response the HTTP response object.<a name="line.365"></a><FONT color="green">366</FONT> * @param mimeType the mime type of the file, null allowed.<a name="line.366"></a><FONT color="green">367</FONT> *<a name="line.367"></a><FONT color="green">368</FONT> * @throws IOException if there is an I/O problem.<a name="line.368"></a><FONT color="green">369</FONT> */<a name="line.369"></a><FONT color="green">370</FONT> public static void sendTempFile(File file, HttpServletResponse response,<a name="line.370"></a><FONT color="green">371</FONT> String mimeType) throws IOException {<a name="line.371"></a><FONT color="green">372</FONT> <a name="line.372"></a><FONT color="green">373</FONT> if (file.exists()) {<a name="line.373"></a><FONT color="green">374</FONT> BufferedInputStream bis = new BufferedInputStream(<a name="line.374"></a><FONT color="green">375</FONT> new FileInputStream(file));<a name="line.375"></a><FONT color="green">376</FONT> <a name="line.376"></a><FONT color="green">377</FONT> // Set HTTP headers<a name="line.377"></a><FONT color="green">378</FONT> if (mimeType != null) {<a name="line.378"></a><FONT color="green">379</FONT> response.setHeader("Content-Type", mimeType);<a name="line.379"></a><FONT color="green">380</FONT> }<a name="line.380"></a><FONT color="green">381</FONT> response.setHeader("Content-Length", String.valueOf(file.length()));<a name="line.381"></a><FONT color="green">382</FONT> SimpleDateFormat sdf = new SimpleDateFormat(<a name="line.382"></a><FONT color="green">383</FONT> "EEE, dd MMM yyyy HH:mm:ss z");<a name="line.383"></a><FONT color="green">384</FONT> sdf.setTimeZone(TimeZone.getTimeZone("GMT"));<a name="line.384"></a><FONT color="green">385</FONT> response.setHeader("Last-Modified", <a name="line.385"></a><FONT color="green">386</FONT> sdf.format(new Date(file.lastModified())));<a name="line.386"></a><FONT color="green">387</FONT> <a name="line.387"></a><FONT color="green">388</FONT> BufferedOutputStream bos = new BufferedOutputStream(<a name="line.388"></a><FONT color="green">389</FONT> response.getOutputStream());<a name="line.389"></a><FONT color="green">390</FONT> byte[] input = new byte[1024];<a name="line.390"></a><FONT color="green">391</FONT> boolean eof = false;<a name="line.391"></a><FONT color="green">392</FONT> while (!eof) {<a name="line.392"></a><FONT color="green">393</FONT> int length = bis.read(input);<a name="line.393"></a><FONT color="green">394</FONT> if (length == -1) {<a name="line.394"></a><FONT color="green">395</FONT> eof = true;<a name="line.395"></a><FONT color="green">396</FONT> } <a name="line.396"></a><FONT color="green">397</FONT> else {<a name="line.397"></a><FONT color="green">398</FONT> bos.write(input, 0, length);<a name="line.398"></a><FONT color="green">399</FONT> }<a name="line.399"></a><FONT color="green">400</FONT> }<a name="line.400"></a><FONT color="green">401</FONT> bos.flush();<a name="line.401"></a><FONT color="green">402</FONT> bis.close();<a name="line.402"></a><FONT color="green">403</FONT> bos.close();<a name="line.403"></a><FONT color="green">404</FONT> }<a name="line.404"></a><FONT color="green">405</FONT> else {<a name="line.405"></a><FONT color="green">406</FONT> throw new FileNotFoundException(file.getAbsolutePath());<a name="line.406"></a><FONT color="green">407</FONT> }<a name="line.407"></a><FONT color="green">408</FONT> return;<a name="line.408"></a><FONT color="green">409</FONT> }<a name="line.409"></a><FONT color="green">410</FONT> <a name="line.410"></a><FONT color="green">411</FONT> /**<a name="line.411"></a><FONT color="green">412</FONT> * Perform a search/replace operation on a String<a name="line.412"></a><FONT color="green">413</FONT> * There are String methods to do this since (JDK 1.4)<a name="line.413"></a><FONT color="green">414</FONT> *<a name="line.414"></a><FONT color="green">415</FONT> * @param inputString the String to have the search/replace operation.<a name="line.415"></a><FONT color="green">416</FONT> * @param searchString the search String.<a name="line.416"></a><FONT color="green">417</FONT> * @param replaceString the replace String.<a name="line.417"></a><FONT color="green">418</FONT> *<a name="line.418"></a><FONT color="green">419</FONT> * @return The String with the replacements made.<a name="line.419"></a><FONT color="green">420</FONT> */<a name="line.420"></a><FONT color="green">421</FONT> public static String searchReplace(String inputString,<a name="line.421"></a><FONT color="green">422</FONT> String searchString,<a name="line.422"></a><FONT color="green">423</FONT> String replaceString) {<a name="line.423"></a><FONT color="green">424</FONT> <a name="line.424"></a><FONT color="green">425</FONT> int i = inputString.indexOf(searchString);<a name="line.425"></a><FONT color="green">426</FONT> if (i == -1) {<a name="line.426"></a><FONT color="green">427</FONT> return inputString;<a name="line.427"></a><FONT color="green">428</FONT> }<a name="line.428"></a><FONT color="green">429</FONT> <a name="line.429"></a><FONT color="green">430</FONT> String r = "";<a name="line.430"></a><FONT color="green">431</FONT> r += inputString.substring(0, i) + replaceString;<a name="line.431"></a><FONT color="green">432</FONT> if (i + searchString.length() < inputString.length()) {<a name="line.432"></a><FONT color="green">433</FONT> r += searchReplace(inputString.substring(i + searchString.length()),<a name="line.433"></a><FONT color="green">434</FONT> searchString, replaceString);<a name="line.434"></a><FONT color="green">435</FONT> }<a name="line.435"></a><FONT color="green">436</FONT> <a name="line.436"></a><FONT color="green">437</FONT> return r;<a name="line.437"></a><FONT color="green">438</FONT> }<a name="line.438"></a><FONT color="green">439</FONT> <a name="line.439"></a><FONT color="green">440</FONT> }<a name="line.440"></a></PRE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -