Wednesday, October 20, 2010

Servlet Programming with Servlet Display Pictures



Servlet servlet used to do programming in a tracking image click technology, this module large one, here to talk about with the Servlet display picture part. First talk show with a picture of a process Servlet:

1. Servlet programming in response to set the output type:

The corresponding statement - response. SetContentType ("image / gif; charset = GB2312"),

銆??response 渚胯兘杈撳嚭gif鍥剧墖锛?amp;quot;image/gif;charset=GB2312"渚挎槸杈撳嚭绫诲瀷锛屽綋鐒朵綘鍙互杈撳嚭 "image/jpg;charset=GB2312"绫诲瀷鏂囦欢銆?br />
銆??2. 瀹炵幇Servlet鏄剧ず鍥剧墖涔嬪緱鍒版枃浠舵祦锛?br />
銆??servlet鏄互娴佺殑褰㈠紡浠跺浘鐗囨枃浠朵粠鏈嶅姟鍣ㄨ鍑猴紝閫氳繃response灏嗘祦鍙戝埌娴忚鍣ㄧ殑銆?br />
銆??3. 瀹炵幇Servlet鏄剧ず鍥剧墖涔嬪緱鍒拌緭鍑烘祦锛?br />
銆??瀵瑰簲鐨勮鍙?-OutputStream output = response.getOutputStream(); 褰撶劧锛屽鐞嗗浘鐗囨枃浠堕渶瑕佷互浜岃繘鍒跺舰寮忕殑娴併?

銆??4. 瀹炵幇Servlet鏄剧ず鍥剧墖涔嬫枃浠舵祦鐨勭紪鐮?浣嗕篃涓嶄竴瀹氬繀椤荤紪鐮佺殑锛屽鏋滀笉鏄枃浠舵祦锛屽垯蹇呴』缂栫爜) 鎵?互鎴戠粰澶у涓?釜鐢ㄧ紪鐮佺殑浠g爜鍜屼笉鐢ㄧ紪鐮佺殑浠g爜.

銆??椤轰究璇翠竴鍙ワ紝sun鍏徃浠呮彁渚涗簡jpg鍥剧墖鏂囦欢鐨勭紪鐮乤pi銆?br />
銆??鎴戞兂鍩烘湰娴佺▼閮借瀹屼簡锛屼笅闈㈡垜鎶婁唬鐮佹嬁缁欏ぇ瀹剁湅涓?笅锛屽ぇ瀹惰嚜鐒朵竴鐩簡鐒朵簡锛?br />
銆??package xjw.personal.servet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import com.sun.image.codec.jpeg.*;

銆??Sun鍏徃浠呮彁渚涗簡jpg鍥剧墖鏂囦欢鐨勭紪鐮乤pi

銆??import javax.imageio.stream.*; import java.awt.*; import java.awt.image.BufferedImage; public class ShowPicture extends HttpServlet{ private static final String GIF="image/gif;charset=GB2312";

銆??瀹炵幇Servlet鏄剧ず鍥剧墖涔嬭瀹氳緭鍑虹殑绫诲瀷

銆??private static final String JPG="image/jpeg;charset=GB2312"; public void init() throws ServletException { } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String spec=request.getParameter("spec"); //杈撳嚭鍥剧墖鐨勭被鍨嬬殑鏍囧織 int int_spec=Integer.parseInt(spec); if(spec==1) { String imagePath="/jfgg/b1.jpg"; //鍥剧墖鐩稿web搴旂敤鐨勪綅缃?} else { String imagePath="/jfgg/b2.gif";

銆??鍥剧墖鐩稿web搴旂敤鐨勪綅缃?br />
銆??} OutputStream output = response.getOutputStream();//寰楀埌杈撳嚭娴?if(imagePath.toLowerCase().endsWith(".jpg"))//浣跨敤缂栫爜澶勭悊鏂囦欢娴佺殑鎯呭喌锛?{ response.setContentType(JPG);//璁惧畾杈撳嚭鐨勭被鍨?//寰楀埌鍥剧墖鐨勭湡瀹炶矾寰?imagePath = getServletContext().getRealPath(imagePath);

銆??寰楀埌鍥剧墖鐨勬枃浠舵祦

銆??InputStream imageIn = new FileInputStream(new File(imagePath)); //寰楀埌杈撳叆鐨勭紪鐮佸櫒锛屽皢鏂囦欢娴佽繘琛宩pg鏍煎紡缂栫爜 JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn); //寰楀埌缂栫爜鍚庣殑鍥剧墖瀵硅薄 BufferedImage image = decoder.decodeAsBufferedImage(); //寰楀埌杈撳嚭鐨勭紪鐮佸櫒 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output); encoder.encode(image);//瀵瑰浘鐗囪繘琛岃緭鍑虹紪鐮?imageIn.close();//鍏抽棴鏂囦欢娴?} if(imagePath.toLowerCase().endsWith(".gif"))//涓嶄娇鐢ㄧ紪鐮佸鐞嗘枃浠舵祦鐨勬儏鍐碉細 { response.setContentType(GIF); ServletContext context = getServletContext();//寰楀埌鑳屾櫙瀵硅薄 InputStream imageIn=context.getResourceAsStream(imagePath);//鏂囦欢娴?BufferedInputStream bis=new BufferedInputStream(imageIn);//杈撳叆缂撳啿娴?BufferedOutputStream bos=new BufferedOutputStream(output);//杈撳嚭缂撳啿娴?byte data[]=new byte[4096];//缂撳啿瀛楄妭鏁?int size=0; size=bis.read(data); while (size!=-1) { bos.write(data,0,size); size=bis.read(data); } bis.close(); bos.flush();//娓呯┖杈撳嚭缂撳啿娴?bos.close(); } output.close(); } }

銆??鏈?悗鏄浣曡皟鐢紝浣犲彲浠ョ畝鍗曠殑鏄犲皠涓?笅servelt锛屾垜灏卞皢servet鐨勫悕鏄犲皠涓篠howPic, 浜庢槸涓嬩唬鐮佽皟鐢?br />
銆??锕tml锕ャ? 銆?工body锕ャ? 銆?工img src="ShowPic?spec=2"锕ワ工/a锕?锕?body锕?锕?html锕?br />
銆??Servlet缂栫▼涔嬬敤Servlet鏄剧ず鍥剧墖鐨勪粙缁嶅氨鏄繖鏍峰瓙浜嗭紝閫氳繃涓婅堪鎿嶄綔鍥剧墖渚挎樉绀哄湪htm涓婁簡锛屾晥鏋滃浣曞憿?







相关链接:



Sun Rushes to the fore: All Free



repair corrupted MEMORY card



OGM to AVI



Watch cool mechanical picture production process



WAP DESPERATELY short of qualified personnel



OGM to AVI



First Job After Training



Best System MAINTENANCE



Recommend Development



Green Ocean Is No Longer "lose"



MKV To IPod



Wang Yukun: Power in the Union Heavy Industry "butterfly"



Articles ABOUT Science Education



Games Action for you



12 Large companies in 2007-2008 compared the welfare state



No comments:

Post a Comment