使用Http协议获取网络图片,采用servlet输出详解编程语言

package com.sudytech.activity.web.action; 
 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import com.sudytech.activity.web.uitl.ActivityConfigUtil; 
import com.sudytech.data.mgr.IManager; 
import com.sudytech.log.ILog; 
import com.sudytech.log.LogFactory; 
import com.sudytech.portal.server.WebplusEnv; 
import com.sudytech.site.cms.api.ISiteCmsManager; 
import com.sudytech.site.cms.api.SiteCmsMgrFactory; 
import com.sudytech.site.cms.api.om.SiteArticle2; 
 
public class ActivityPictureServlet extends HttpServlet { 
	private static final long serialVersionUID = 1L; 
	private static ILog _log = LogFactory.getInstance().getLog( 
			ActivityPictureServlet.class); 
 
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException { 
		processRequest(request, response); 
	} 
 
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException { 
		processRequest(request, response); 
	} 
 
	public void processRequest(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException { 
		response.setContentType("image/jpeg; charset=utf-8"); 
		 
		OutputStream os = null; 
		try { 
			os = response.getOutputStream(); 
			 
			URL url = new URL("http://127.0.0.1:8080/photo/2012-02/09/122675973_51n.jpg"); 
			HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
			conn.setRequestMethod("GET"); 
			conn.setConnectTimeout(5 * 1000); 
			conn.connect(); 
			InputStream in = conn.getInputStream(); 
			byte[] buffer = new byte[1024]; 
			int len = 0; 
			while ((len = in.read(buffer)) != -1) { 
				os.write(buffer, 0, len); 
			} 
			os.flush(); 
			os.flush(); 
		} catch (Exception e) { 
			_log.error("activity picture error!"); 
		}  
 
	} 
 
 
} 


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16339.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论