Twelve of the JavaJSP Learning Series (JSP Generation JPEG Pictures)

zhaozj2021-02-16  53

First, preface This article is Tony Wang, which involves the reading and writing of the document and the automatic generation of JPG images. With JSP Servlet technology, the JSP calls the servlet to generate a picture.

Second, the first file index.jsp is as follows: <% - author: tony wang e-mail: lucky_tony@163.net date: 2001-01-01 If you have any questions about the program, you can contact me, if there is anything Bug, trouble pointed out! ! -%> <% @ page contenttype = "text / html; charSet = GB2312"%> <% response.setheader ("cache-control", "no-store"); response.setdateHeader ("expires", 0) ;%> <%! public string [] getQuestion (string s) {string [] strq = new string [4]; string straTemp = null; int i; java.io.randomaccessfile rf = null; try {rf = new Java .io.randomaccessfile (s, "r");} catch (exception e) {system.out.println (e); system.exit (0);} for (i = 0; i <4; i ) {TRY {straTemp = rf.readline ();} catch (exception e) {straTemp = "none questions";} if (strTemp == null) straTemp = "none questions"; strq [i] = strtemp;} return; %> <% String s = null; string [] questions = new string [4]; s = request.getRealPath ("Question.txt"); questions = getquestion (s);%> </ title> <link href = "css.css" rel = "stylesheet" type = "text / css"> </ link> </ head> <body> <table width = "180" border = "1" Bordercolor = "# 999999"> <tr> <td align = center> ice sail survey </ td> </ tr> <form name = frm method = post action = write.jsp> <TR> <TD> <% string SS = NULL; for (int i = 0; i <4; i ) {ss = "<input type = /" radio / "name = /" choice / "VA Lue = " i " (char) ('a' i) "," Question [i] "<br>"; out.println (ss);}%> </ td></p> <p></ tr> <tr> <TD align = center> <input type = submit value = "I vote"> </ td> </ tr> <tr> <td align = center> <img src = "/ vote / servlet / voteImage "width = 150 height = 100> </ td> </ tr> </ form> </ table> </ body> </ html> three, write file write.jsp <% - Author: Tony wange-mail: lucky_tony@163.netdate: 2001-01-01 If you have any questions about the program, you can contact me, if you have any bugs, trouble points out! ! -%> <%! public int [] getnumber (string s) {int [] mcount = new int [4]; string straTemp = null; int i; java.io.randomaccessfile rf = null; try {rf = new Java.io.randomaccessfile (s, "r");} catch (exception e) {system.out.println (e); system.exit (0);} for (i = 0; i <4; i ) { Try {straTemp = rf.readline ();} catch (exception e) {straTemp = "0";} if (strTemp == null) straTemp = "0"; McOUNT [i] = new integer (strTemp) .intValue }} return mcount;} public void setnumber (string s, int [] x) {try {java.io.printwriter pw = new java.io.printwriter (new java.io.fileoutputStream (s)); for (int i = 0; i <4; i ) {PW.Println (x [i] "");} pw.close ();} catch (exception e) {system.out.println ("Write File error:" E.GetMessage ());}}%> <% string tmp = null; int choice = -1; int choice = -1; int = -1; int = -1; int = new int [4]; tmp = request.getParameter ("choice"); if (TMP == NULL) {} else {choice = new integer (tmp) .intValue ();} / string s = request.getRealPath ("count.txt"); if (choxt = 0) {count = GetNumber (s) Count [choice] ; setNumber (s, count);} response.sendredirect ("index.jsp");%> four, servlet original code: vote Image.java: / * Author: tony wange-mail: lucky_tony @</p> <p>163.Netdate: 2001-01-01 If you have any questions about the program, you can contact me, if there is any bug, trouble point out! ! * / Import java.io. *; import java.util. *; import com.sun.image.codec.jpeg. *; import javax.servlet. *; import javax.servlet.http. *; import java.awt. *; import java.awt.geom *;. import java.awt.image *;. public class VoteImage extends HttpServlet {private String strFile = null; private Color color [] = {Color.red, Color.black, Color.orange , Color.green}; private int baseAng = 30; public void doGet (HttpServletRequest request, HttpServletResponseresponse) throws ServletException, IOException {strFile = request.getRealPath ( "count.txt"); float [] [] xy = new float [4 ] [2]; XY = getnumandpercent (strfile); int [] ang = new int [4]; ang [0] = (int) (xy [0] [1] * 360); ang [1] = (int) (xy [1] [1] * 360); ang [2] = (int) (xy [2] [1] * 360); ang [3] = 360-ang [0] -ang [1] - ang [2]; response.setHeader ( "Cache-Control", "no-store"); response.setDateHeader ( "Expires", 0); response.setContentType ( "image / jpeg"); ServletOutputStream out = response.getOutputStream (); Bufferedimage image = new bufferedimage (150, 100, bufferedimage.type_int_rgb); graphics2d g = (graphics2d) Image.GETGRA phics (); g.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor (Color.white); g.fillRect (0,0,150,100); AffineTransform at = null; Arc2D arc = null; int fromAng = baseAng; AT = AffineTransform.GtrotateInstance ((- 20 * java.lang.math.pi) / 180, 45, 37); G.SetTransform (at); int R = 6; int dx = (int) (r * java.lang .Math.cos ((BaseAng Ang [0]) / 2.0 * java.lang.math.pi / 180)); int Dy = (int) (r * java.lang.math.sin ((Baseang Ang [ 0]) / 2.0 * java.lang.math.pi / 180)); arc = new arc2d.double (10 </p> <p>DX, 24-DY, 80, 50, Fromang, Ang [0], Arc2D.pie; G.SetColor (Color [0]); g.fill (arc); fromang = ang [0]; for (int I = 1; i <4; i ) {g.setcolor (color [i]); arc = new arc2d.double (10, 24, 80, 50, fromang, ang [i], arc2d.pie; g.fill (arc); fromang = ang [i]; if (fromang> 360) {fromang- = 360;}} at = AffineTransform.GtrotteInstance (0, arc.getCenterx (), arc.getCenter (); g.setTransform At); for (int i = 0; i <4; i ) {g.setcolor (color [i]); g.fillRect (100, 15 * i 20, 10, 10); g.drawstring ((charwstring) ) ('A' i) "", 120, 15 * i 20 8); JPEGIMAGEENCODER ENCODER = JPEGCODEC.CREATEJPEGENCODER (OUT); Encoder.Encode (Image); out.close ();} public void doPost (HttpServletRequest request, HttpServletResponseresponse) throws ServletException, IOException {doGet (request, response);} public synchronized float [] [] getNumAndPercent (String sFileName) {float xx [] [] = new float [4] [2]; int Totalnum = 0; string straTemp = null; int i = 0; java.io.randomaccessfile rf = null; try {rf = new java.io.randomaccessfile (sfilename, "r");} catch (exception e) {system. Out.println (E); System.exit (0);} for (i = 0 i <4; i ) {INT m = 0; try {straTemp = rf.readline ();} catch (exception e) {strTemp = "0";} if (strTemp == null) strTemp = "0"; M = new integer (strtemp) .intValue (); xx [i] [0] = m; TotalNum = m;} if (totalnum == 0) TotalNum = 1; for (i = 0; i <4; i ) {xx [i] [1] = xx [i] [0] / totalnum;} Return XX;}} 5, establish questions.txt and count.txt files to save voting in index.jsp directory And the number of votes, after the user votes, modify the value of count.txt.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-19783.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="19783" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.047</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '3dNaYc1WCBSWP537dwZ3P3nFtMytrQLta6g1uVmKQIQeq4qjspRe7X5BqKCp5r5koZP8xLsdekv21vND_2FffgBA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>