Android上傳圖片至服務器Servlet端

本人初學者,寫東西的最根本目的也是加深印象, 先來效果圖:

做這個小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();
	}

};




發佈了32 篇原創文章 · 獲贊 20 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章