标签:
1.客户端
1 package com.jingle.requestimg; 2 3 import org.apache.http.Header; 4 5 import com.loopj.android.http.AsyncHttpClient; 6 import com.loopj.android.http.AsyncHttpResponseHandler; 7 import com.loopj.android.http.RequestParams; 8 9 import android.app.Activity; 10 import android.graphics.Bitmap; 11 import android.graphics.BitmapFactory; 12 import android.os.Bundle; 13 import android.view.View; 14 import android.view.View.OnClickListener; 15 import android.widget.Button; 16 import android.widget.ImageView; 17 18 public class MainActivity extends Activity { 19 private String url = "http://192.168.1.101:8080/Login/servlet/ReturnImg"; 20 21 private ImageView imageView; 22 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_main); 27 28 initImgView(); 29 initBtn(); 30 } 31 32 private void initImgView() { 33 imageView = (ImageView) findViewById(R.id.img); 34 } 35 36 private void initBtn() { 37 Button btn = (Button) findViewById(R.id.btn); 38 btn.setOnClickListener(new OnClickListener() { 39 40 @Override 41 public void onClick(View v) { 42 getServerImg(); 43 44 } 45 }); 46 } 47 48 private void getServerImg() { 49 AsyncHttpClient client = new AsyncHttpClient(); 50 client.setTimeout(3000); 51 RequestParams params = new RequestParams(); 52 params.put("img", "img"); 53 client.post(url, params, new AsyncHttpResponseHandler() { 54 55 @Override 56 public void onSuccess(int statusCode, Header[] headers, 57 byte[] responseBody) { 58 if (responseBody != null) { 59 Bitmap bmp = BitmapFactory.decodeByteArray(responseBody, 0, 60 responseBody.length); 61 imageView.setImageBitmap(bmp); 62 } 63 } 64 65 public void onFailure(int statusCode, Header[] headers, 66 byte[] responseBody, Throwable error) { 67 error.printStackTrace(); 68 } 69 }); 70 } 71 72 }
2.服务器端
1 package action; 2 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 6 import java.io.OutputStream; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 public class ReturnImg extends HttpServlet { 14 15 private static final long serialVersionUID = -70601451108821183L; 16 private static final String JPG = "image/jpeg;charset=GB2312"; 17 18 public void doGet(HttpServletRequest request, HttpServletResponse response) 19 throws ServletException, IOException { 20 21 doPost(request, response); 22 } 23 24 public void doPost(HttpServletRequest request, HttpServletResponse response) 25 throws ServletException, IOException { 26 System.out.println("do post"); 27 String webRootUrl = request.getServletContext().getRealPath("") 28 + "//guan.jpg"; 29 if ("img".equals(request.getParameter("img"))) { 30 31 FileInputStream fis = new FileInputStream(webRootUrl); 32 int size = fis.available(); 33 byte[] data = new byte[size]; 34 fis.read(data); 35 fis.close(); 36 response.setContentType(JPG); 37 OutputStream os = response.getOutputStream(); 38 os.write(data); 39 os.flush(); 40 os.close(); 41 42 } 43 44 } 45 46 }
标签:
原文地址:http://www.cnblogs.com/jinglecode/p/4376538.html