📄 servletutilities.html
字号:
<FONT color="green">167</FONT> * the {@link DisplayChart} servlet right after it is<a name="line.167"></a><FONT color="green">168</FONT> * streamed to the client).<a name="line.168"></a><FONT color="green">169</FONT> *<a name="line.169"></a><FONT color="green">170</FONT> * @return The filename of the chart saved in the temporary directory.<a name="line.170"></a><FONT color="green">171</FONT> *<a name="line.171"></a><FONT color="green">172</FONT> * @throws IOException if there is a problem saving the file.<a name="line.172"></a><FONT color="green">173</FONT> */<a name="line.173"></a><FONT color="green">174</FONT> public static String saveChartAsPNG(JFreeChart chart, int width, int height,<a name="line.174"></a><FONT color="green">175</FONT> ChartRenderingInfo info, HttpSession session) throws IOException {<a name="line.175"></a><FONT color="green">176</FONT> <a name="line.176"></a><FONT color="green">177</FONT> if (chart == null) {<a name="line.177"></a><FONT color="green">178</FONT> throw new IllegalArgumentException("Null 'chart' argument."); <a name="line.178"></a><FONT color="green">179</FONT> }<a name="line.179"></a><FONT color="green">180</FONT> ServletUtilities.createTempDir();<a name="line.180"></a><FONT color="green">181</FONT> String prefix = ServletUtilities.tempFilePrefix;<a name="line.181"></a><FONT color="green">182</FONT> if (session == null) {<a name="line.182"></a><FONT color="green">183</FONT> prefix = ServletUtilities.tempOneTimeFilePrefix;<a name="line.183"></a><FONT color="green">184</FONT> }<a name="line.184"></a><FONT color="green">185</FONT> File tempFile = File.createTempFile(prefix, ".png", <a name="line.185"></a><FONT color="green">186</FONT> new File(System.getProperty("java.io.tmpdir")));<a name="line.186"></a><FONT color="green">187</FONT> ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);<a name="line.187"></a><FONT color="green">188</FONT> if (session != null) {<a name="line.188"></a><FONT color="green">189</FONT> ServletUtilities.registerChartForDeletion(tempFile, session);<a name="line.189"></a><FONT color="green">190</FONT> }<a name="line.190"></a><FONT color="green">191</FONT> return tempFile.getName();<a name="line.191"></a><FONT color="green">192</FONT> <a name="line.192"></a><FONT color="green">193</FONT> }<a name="line.193"></a><FONT color="green">194</FONT> <a name="line.194"></a><FONT color="green">195</FONT> /**<a name="line.195"></a><FONT color="green">196</FONT> * Saves the chart as a JPEG format file in the temporary directory.<a name="line.196"></a><FONT color="green">197</FONT> * <p><a name="line.197"></a><FONT color="green">198</FONT> * SPECIAL NOTE: Please avoid using JPEG as an image format for charts,<a name="line.198"></a><FONT color="green">199</FONT> * it is a "lossy" format that introduces visible distortions in the<a name="line.199"></a><FONT color="green">200</FONT> * resulting image - use PNG instead. In addition, note that JPEG output<a name="line.200"></a><FONT color="green">201</FONT> * is supported by JFreeChart only for JRE 1.4.2 or later.<a name="line.201"></a><FONT color="green">202</FONT> * <a name="line.202"></a><FONT color="green">203</FONT> * @param chart the JFreeChart to be saved.<a name="line.203"></a><FONT color="green">204</FONT> * @param width the width of the chart.<a name="line.204"></a><FONT color="green">205</FONT> * @param height the height of the chart.<a name="line.205"></a><FONT color="green">206</FONT> * @param session the HttpSession of the client (if <code>null</code>, the<a name="line.206"></a><FONT color="green">207</FONT> * temporary file is marked as "one-time" and deleted by <a name="line.207"></a><FONT color="green">208</FONT> * the {@link DisplayChart} servlet right after it is<a name="line.208"></a><FONT color="green">209</FONT> * streamed to the client).<a name="line.209"></a><FONT color="green">210</FONT> *<a name="line.210"></a><FONT color="green">211</FONT> * @return The filename of the chart saved in the temporary directory.<a name="line.211"></a><FONT color="green">212</FONT> *<a name="line.212"></a><FONT color="green">213</FONT> * @throws IOException if there is a problem saving the file.<a name="line.213"></a><FONT color="green">214</FONT> */<a name="line.214"></a><FONT color="green">215</FONT> public static String saveChartAsJPEG(JFreeChart chart, int width, <a name="line.215"></a><FONT color="green">216</FONT> int height, HttpSession session) <a name="line.216"></a><FONT color="green">217</FONT> throws IOException {<a name="line.217"></a><FONT color="green">218</FONT> <a name="line.218"></a><FONT color="green">219</FONT> return ServletUtilities.saveChartAsJPEG(chart, width, height, null, <a name="line.219"></a><FONT color="green">220</FONT> session);<a name="line.220"></a><FONT color="green">221</FONT> <a name="line.221"></a><FONT color="green">222</FONT> }<a name="line.222"></a><FONT color="green">223</FONT> <a name="line.223"></a><FONT color="green">224</FONT> /**<a name="line.224"></a><FONT color="green">225</FONT> * Saves the chart as a JPEG format file in the temporary directory and<a name="line.225"></a><FONT color="green">226</FONT> * populates the <code>ChartRenderingInfo</code> object which can be used <a name="line.226"></a><FONT color="green">227</FONT> * to generate an HTML image map.<a name="line.227"></a><FONT color="green">228</FONT> * <p><a name="line.228"></a><FONT color="green">229</FONT> * SPECIAL NOTE: Please avoid using JPEG as an image format for charts,<a name="line.229"></a><FONT color="green">230</FONT> * it is a "lossy" format that introduces visible distortions in the<a name="line.230"></a><FONT color="green">231</FONT> * resulting image - use PNG instead. In addition, note that JPEG output<a name="line.231"></a><FONT color="green">232</FONT> * is supported by JFreeChart only for JRE 1.4.2 or later.<a name="line.232"></a><FONT color="green">233</FONT> *<a name="line.233"></a><FONT color="green">234</FONT> * @param chart the chart to be saved (<code>null</code> not permitted).<a name="line.234"></a><FONT color="green">235</FONT> * @param width the width of the chart<a name="line.235"></a><FONT color="green">236</FONT> * @param height the height of the chart<a name="line.236"></a><FONT color="green">237</FONT> * @param info the ChartRenderingInfo object to be populated<a name="line.237"></a><FONT color="green">238</FONT> * @param session the HttpSession of the client (if <code>null</code>, the<a name="line.238"></a><FONT color="green">239</FONT> * temporary file is marked as "one-time" and deleted by <a name="line.239"></a><FONT color="green">240</FONT> * the {@link DisplayChart} servlet right after it is<a name="line.240"></a><FONT color="green">241</FONT> * streamed to the client).<a name="line.241"></a><FONT color="green">242</FONT> *<a name="line.242"></a><FONT color="green">243</FONT> * @return The filename of the chart saved in the temporary directory<a name="line.243"></a><FONT color="green">244</FONT> *<a name="line.244"></a><FONT color="green">245</FONT> * @throws IOException if there is a problem saving the file.<a name="line.245"></a><FONT color="green">246</FONT> */<a name="line.246"></a><FONT color="green">247</FONT> public static String saveChartAsJPEG(JFreeChart chart, int width, <a name="line.247"></a><FONT color="green">248</FONT> int height, ChartRenderingInfo info, HttpSession session)<a name="line.248"></a><FONT color="green">249</FONT> throws IOException {<a name="line.249"></a><FONT color="green">250</FONT> <a name="line.250"></a><FONT color="green">251</FONT> if (chart == null) {<a name="line.251"></a><FONT color="green">252</FONT> throw new IllegalArgumentException("Null 'chart' argument."); <a name="line.252"></a><FONT color="green">253</FONT> }<a name="line.253"></a><FONT color="green">254</FONT> <a name="line.254"></a><FONT color="green">255</FONT> ServletUtilities.createTempDir();<a name="line.255"></a><FONT color="green">256</FONT> String prefix = ServletUtilities.tempFilePrefix;<a name="line.256"></a><FONT color="green">257</FONT> if (session == null) {<a name="line.257"></a><FONT color="green">258</FONT> prefix = ServletUtilities.tempOneTimeFilePrefix; <a name="line.258"></a><FONT color="green">259</FONT> }<a name="line.259"></a><FONT color="green">260</FONT> File tempFile = File.createTempFile(prefix, ".jpeg", <a name="line.260"></a><FONT color="green">261</FONT> new File(System.getProperty("java.io.tmpdir")));<a name="line.261"></a><FONT color="green">262</FONT> ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height, info);<a name="line.262"></a><FONT color="green">263</FONT> if (session != null) {<a name="line.263"></a><FONT color="green">264</FONT> ServletUtilities.registerChartForDeletion(tempFile, session);<a name="line.264"></a><FONT color="green">265</FONT> }<a name="line.265"></a><FONT color="green">266</FONT> return tempFile.getName();<a name="line.266"></a><FONT color="green">267</FONT> <a name="line.267"></a><FONT color="green">268</FONT> }<a name="line.268"></a><FONT color="green">269</FONT> <a name="line.269"></a><FONT color="green">270</FONT> /**<a name="line.270"></a><FONT color="green">271</FONT> * Creates the temporary directory if it does not exist. Throws a <a name="line.271"></a><FONT color="green">272</FONT> * <code>RuntimeException</code> if the temporary directory is <a name="line.272"></a><FONT color="green">273</FONT> * <code>null</code>. Uses the system property <code>java.io.tmpdir</code> <a name="line.273"></a><FONT color="green">274</FONT> * as the temporary directory. This sounds like a strange thing to do but <a name="line.274"></a><FONT color="green">275</FONT> * my temporary directory was not created on my default Tomcat 4.0.3 <a name="line.275"></a><FONT color="green">276</FONT> * installation. Could save some questions on the forum if it is created <a name="line.276"></a><FONT color="green">277</FONT> * when not present.<a name="line.277"></a><FONT color="green">278</FONT> */<a name="line.278"></a><FONT color="green">279</FONT> protected static void createTempDir() {<a name="line.279"></a><FONT color="green">280</FONT> String tempDirName = System.getProperty("java.io.tmpdir");<a name="line.280"></a><FONT color="green">281</FONT> if (tempDirName == null) {<a name="line.281"></a><FONT color="green">282</FONT> throw new RuntimeException("Temporary directory system property " <a name="line.282"></a><FONT color="green">283</FONT> + "(java.io.tmpdir) is null.");<a name="line.283"></a><FONT color="green">284</FONT> }<a name="line.284"></a><FONT color="green">285</FONT> <a name="line.285"></a><FONT color="green">286</FONT> // create the temporary directory if it doesn't exist<a name="line.286"></a><FONT color="green">287</FONT> File tempDir = new File(tempDirName);<a name="line.287"></a><FONT color="green">288</FONT> if (!tempDir.exists()) {<a name="line.288"></a><FONT color="green">289</FONT> tempDir.mkdirs();<a name="line.289"></a><FONT color="green">290</FONT> }<a name="line.290"></a><FONT color="green">291</FONT> }<a name="line.291"></a><FONT color="green">292</FONT> <a name="line.292"></a><FONT color="green">293</FONT> /**<a name="line.293"></a><FONT color="green">294</FONT> * Adds a {@link ChartDeleter} object to the session object with the name <a name="line.294"></a><FONT color="green">295</FONT> * <code>JFreeChart_Deleter</code> if there is not already one bound to the <a name="line.295"></a><FONT color="green">296</FONT> * session and adds the filename to the list of charts to be deleted.<a name="line.296"></a><FONT color="green">297</FONT> *<a name="line.297"></a><FONT color="green">298</FONT> * @param tempFile the file to be deleted.<a name="line.298"></a><FONT color="green">299</FONT> * @param session the HTTP session of the client.<a name="line.299"></a><FONT color="green">300</FONT> */<a name="line.300"></a><FONT color="green">301</FONT> protected static void registerChartForDeletion(File tempFile, <a name="line.301"></a><FONT color="green">302</FONT> HttpSession session) {<a name="line.302"></a><FONT color="green">303</FONT> <a name="line.303"></a><FONT color="green">304</FONT> // Add chart to deletion list in session<a name="line.304"></a><FONT color="green">305</FONT> if (session != null) {<a name="line.305"></a><FONT color="green">306</FONT> ChartDeleter chartDeleter <a name="line.306"></a><FONT color="green">307</FONT> = (ChartDeleter) session.getAttribute("JFreeChart_Deleter");<a name="line.307"></a><FONT color="green">308</FONT> if (chartDeleter == null) {<a name="line.308"></a><FONT color="green">309</FONT> chartDeleter = new ChartDeleter();<a name="line.309"></a><FONT color="green">310</FONT> session.setAttribute("JFreeChart_Deleter", chartDeleter);<a name="line.310"></a><FONT color="green">311</FONT> }<a name="line.311"></a><FONT color="green">312</FONT> chartDeleter.addChart(tempFile.getName());<a name="line.312"></a><FONT color="green">313</FONT> }<a name="line.313"></a><FONT color="green">314</FONT> else {<a name="line.314"></a><FONT color="green">315</FONT> System.out.println("Session is null - chart will not be deleted");<a name="line.315"></a><FONT color="green">316</FONT> }<a name="line.316"></a><FONT color="green">317</FONT> }<a name="line.317"></a><FONT color="green">318</FONT> <a name="line.318"></a><FONT color="green">319</FONT> /**<a name="line.319"></a><FONT color="green">320</FONT> * Binary streams the specified file in the temporary directory to the<a name="line.320"></a><FONT color="green">321</FONT> * HTTP response in 1KB chunks.<a name="line.321"></a><FONT color="green">322</FONT> * <a name="line.322"></a><FONT color="green">323</FONT> * @param filename the name of the file in the temporary directory.<a name="line.323"></a><FONT color="green">324</FONT> * @param response the HTTP response object.<a name="line.324"></a><FONT color="green">325</FONT> * <a name="line.325"></a><FONT color="green">326</FONT> * @throws IOException if there is an I/O problem.<a name="line.326"></a><FONT color="green">327</FONT> */<a name="line.327"></a><FONT color="green">328</FONT> public static void sendTempFile(String filename, <a name="line.328"></a><FONT color="green">329</FONT> HttpServletResponse response) throws IOException {<a name="line.329"></a><FONT color="green">330</FONT> <a name="line.330"></a><FONT color="green">331</FONT> File file = new File(System.getProperty("java.io.tmpdir"), filename);<a name="line.331"></a><FONT color="green">332</FONT> ServletUtilities.sendTempFile(file, response);<a name="line.332"></a><FONT color="green">333</FONT> }<a name="line.333"></a><FONT color="green">334</FONT> <a name="line.334"></a><FONT color="green">335</FONT> /**<a name="line.335"></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -