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涓婁簡锛屾晥鏋滃浣曞憿?
相关链接:
Simple Compilers And Interpreters
for you Icons
CONVERT ipod to mpeg 3
What the future of "beneficial" booming
IDC how to charge only reasonable
VIP guests have the Start menu
Big Rabbit - Yuan Colombia
MKV to ZUNE
XviD To MP4
Students This Year, Real Salary Levels Jiemi Fresh
M2TS to MKV
Competitive Strategy: Legal spying
The Production Of A Three-dimensional Characters
Hot Terminal And Telnet Clients
Different OPINIONS