日期:2014-05-16 浏览次数:21110 次
package com.topcheer.img; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.SimpleHttpConnectionManager; import org.apache.commons.httpclient.methods.GetMethod; /** * Servlet implementation class ServletTest */ public class ServletTest extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletTest() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpClient client = new HttpClient(); GetMethod get = null; try { // hasActive(); get = new GetMethod("http://172.16.104.78:8080/opticm-server/downloadImage?appId=kdkk&contentId=111111"); int i = client.executeMethod(get); if (200 == i) { File storeFile = new File("d:/test.jpg"); FileOutputStream output = new FileOutputStream(storeFile); // 得到网络资源的字节数组,并写入文件 output.write(get.getResponseBody()); output.close(); }else { System.out.println("no pic"); } } catch ( Exception e ) { System.out.println("no pic"); }finally{ get.releaseConnection(); client.getHttpConnectionManager().closeIdleConnections(0); } } }