Android上传图片至服务器Servlet端

这篇博客介绍了Android初学者如何实现点击ImageView打开相册或拍照,并将选中图片上传到服务器。通过使用Base64编码,将图片转化为字符串字节数组在Android客户端和Servlet端传递。涉及到的关键步骤包括获取相册图片输入流和使用android-async-http库进行异步上传。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人初学者,写东西的最根本目的也是加深印象, 先来效果图:

做这个小Demo,主要是有两步需要实现:

第一:是点击ImageView控件,弹出对话框,选择相册或者拍照后的图片设置到imageview上

第二:是把该图片上传到服务器,(在网上找了好多例子,大都是设置请求头信息什么的),这里我用的是根据Base64 上传。需要在Android 和Servlet段导入jar包(android-async-http-1.4.3.jar) ;  Baser基本思想是在Android段把图片解析为String类型的字节数组,发送到服务器端,解密


 说一下具体方法的实现:

第一步:利用onResultActivity拿到相册图片的输入流


          private byte[] readStream(InputStream openInputStream) {

		byte[] buffer = new byte[1024];
		int len = -1;
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			while ((len = openInputStream.read(buffer)) != -1) {// 不等于-1 继续读取
				out.write(buffer, 0, len);
			}
			byte[] data = out.toByteArray();
			out.close();
			openInputStream.close();
			return data;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;

	}
         private Bitmap getPicFromBytes(byte[] mContent2, Object object) {
 		if (mContent2 != null) {
			if (object != null) {
				return BitmapFactory.decodeByteArray(mContent2, 0,
						mContent2.length);
			} else {
				return BitmapFactory.decodeByteArray(mContent2, 0,
						mContent2.length);
			}
		}
		return myBitMap;
	}

 String proj[]={MediaStore.Images.Media.DATA};
			Cursor cursor=managedQuery(uri, proj, null, null, null);//uri就是上述的uri
			String ss= uri.getEncodedPath();
			System.out.println("ssssssssss"+ss);
			int coulun_index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			cursor.moveToFirst();
			String realPath=cursor.getString(coulun_index);
			System.out.println("realPath"+realPath);
			Bitmap bit = BitmapFactory.decodeFile(realPath);

 接下俩就开始根绝Base64上传(很简单)<pre name="code" class="html">public static void  reg(final Context cont,final Bitmap bit) throws IOException{
		//这边,我们已经拿到Bitmap. 要根据Base 64.
		
		ByteArrayOutputStream baso=new ByteArrayOutputStream();
		bit.compress(Bitmap.CompressFormat.JPEG, 90, baso);
		//		    baso.close();
			 byte aa[]=baso.toByteArray();
			
		      System.out.println("图片的大小为:"+aa.length);			
		    String photo=Base64.encodeToString(aa, 0, aa.length, Base64.DEFAULT);
		     // String photo=Base64.encodeToString(aa, 0);
		       RequestParams parms=new RequestParams();
		       parms.put("photo", photo); parms.put("name", "wjjer");
		       String url="http://10.203.1.51:8080/Test/User";
		       AsyncHttpClient client=new AsyncHttpClient();
		       client.post(url, parms,new AsyncHttpResponseHandler(){};

      });
}

看这个文章的上述代码都肯定明白;至此我们已经完成了上传服务器的工作;接下来是在Servlet 接收;
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");
	 response.setContentType("text/html");
	 String photo=request.getParameter("photo");
	 String name=request.getParameter("name");
	 System.out.println(photo);
	//对发送来的Base64数据进行解码。生成字节数组
	  byte []photos=new BASE64Decoder().decodeBuffer(photo);
	  for (int i=0;i<photos.length;i++){
		  if(photos[i]<0){
			  photos[i]+=256;
		  }
	  }
	  //F
	  File file=new File("d:","decode.png");
	  File filename=new File("d:\\name.txt");
	  if(!file.exists()){file.createNewFile();}if(!filename.exists()){filename.createNewFile();}
	  FileOutputStream out=new FileOutputStream(file);
	  FileOutputStream out1=new FileOutputStream(filename);
	  out.write(photos);out.flush();out.close();
	}

};




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值