获取Assets目录下的图片显示并上传

  1 package com.jingle.getlocal;
  2 
  3 import java.io.ByteArrayOutputStream;
  4 
  5 import java.io.InputStream;
  6 
  7 import org.apache.http.Header;
  8 
  9 import sun.misc.BASE64Encoder;
 10 
 11 import com.loopj.android.http.AsyncHttpClient;
 12 import com.loopj.android.http.AsyncHttpResponseHandler;
 13 import com.loopj.android.http.RequestParams;
 14 
 15 
 16 import android.app.Activity;
 17 import android.content.res.AssetManager;
 18 import android.graphics.Bitmap;
 19 import android.graphics.BitmapFactory;
 20 
 21 import android.os.Bundle;
 22 import android.util.Log;
 23 import android.view.View;
 24 import android.view.View.OnClickListener;
 25 import android.widget.Button;
 26 import android.widget.ImageView;
 27 
 28 public class MainActivity extends Activity {
 29     private String UPLOAD_URL = "http://192.168.1.101:8080/Login//servlet/UploadImg";
 30 
 31     @Override
 32     protected void onCreate(Bundle savedInstanceState) {
 33         super.onCreate(savedInstanceState);
 34         setContentView(R.layout.activity_main);
 35 
 36         initImg();
 37         initBtnUploadImg();
 38 
 39     }
 40 
 41     private void initBtnUploadImg() {
 42         Button btn = (Button) findViewById(R.id.btn);
 43         btn.setOnClickListener(new OnClickListener() {
 44 
 45             public void onClick(View v) {
 46                 uploadImg();
 47             }
 48         });
 49     }
 50 
 51     private void uploadImg() {
 52         AsyncHttpClient client = new AsyncHttpClient();
 53         client.setTimeout(3000);
 54         RequestParams params = new RequestParams();
 55         //获取Bitmap对象
 56         Bitmap bmp = getImg("orca.jpg");
 57         //获取字节数组输出流
 58         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 59         //压缩Bitmap对象到字节数组输出流
 60         bmp.compress(Bitmap.CompressFormat.JPEG, 90, baos);
 61         //将字节数组输出流转为字节数组
 62         byte[] ba = baos.toByteArray();
 63         //将字节数组编码成字符串
 64         String str = new BASE64Encoder().encode(ba);
 65         params.put("img", str);
 66 
 67         client.post(UPLOAD_URL, params, new AsyncHttpResponseHandler() {
 68             public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
 69                 Log.i("ace", "post ok..");
 70             }
 71 
 72             public void onFailure(int arg0, Header[] arg1, byte[] arg2,
 73                     Throwable arg3) {
 74                 arg3.printStackTrace();
 75             }
 76         });
 77 
 78     }
 79 
 80     private void initImg() {
 81         ImageView img = (ImageView) findViewById(R.id.img);
 82         img.setImageBitmap(getImg("orca.jpg"));//assets目录下文件名
 83     }
 84 
 85     //获取图片的Bitmap对象
 86     private Bitmap getImg(String file) {
 87         Bitmap bmp = null;
 88         //获取AssetsMng对象
 89         AssetManager am = getResources().getAssets();
 90         try {
 91             //打开文件,返回输入流
 92             InputStream is = am.open(file);
 93             //Bitmap工厂解码输入流,得到bmp对象
 94             bmp = BitmapFactory.decodeStream(is);
 95             is.close();
 96         } catch (Exception e) {
 97             e.printStackTrace();
 98         }
 99         return bmp;
100     }
101 
102 }

服务器端:

 1 package action;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.io.OutputStream;
 6 import java.io.PrintWriter;
 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 import sun.misc.BASE64Decoder;
14 
15 
16 
17 
18 public class UploadImg extends HttpServlet {
19 
20     private static final long serialVersionUID = -8236824851962797021L;
21 
22     public void doGet(HttpServletRequest request, HttpServletResponse response)
23             throws ServletException, IOException {
24         response.setContentType("text/html");
25         doPost(request, response);
26     }
27 
28     public void doPost(HttpServletRequest request, HttpServletResponse response)
29             throws ServletException, IOException {
30         
31         System.out.println("do post..");
32         String imgStr = request.getParameter("img");
33         //将字符串解码为字节数组
34         byte[] imgByte = new BASE64Decoder().decodeBuffer(imgStr);
35         //新建文件输出流
36         OutputStream os = new FileOutputStream("E:\\up.jpg");
37         //输出流写入字节数组
38         os.write(imgByte);
39         os.close();
40         
41         PrintWriter out = response.getWriter();
42         out.flush();
43         out.close();
44     }
45 
46 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。