我想通過http發送一張照片到我的服務器。所以我將圖像轉換爲字節,然後我將它作爲名稱值對發送。下面是我的代碼,這樣做。現在我的問題是服務器端,我怎麼能重新和圖像從字節的服務器上如何發送圖像從android到servlet然後存儲它
收到我也是使用Java servlet
代碼在Android
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pinnedV.getPhoto().compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("photo",new String(b)));
HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost(URL);
try {
request.setEntity(new UrlEncodedFormEntity(params));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
串碼存儲
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fromClientphoto= request.getParameter("photo");
byte[] b = fromClientphoto.getBytes();
FileOutputStream fos = new FileOutputStream("D:\\img.png");
fos.write(b);
fos.close();
}
上面的代碼寫入一個文件,但它不會作爲圖像打開。也是這byte[] b = fromClientphoto.getBytes();
正確的方式來轉換回android手機上相同的字節?有任何想法嗎?
問題是什麼?你有兩個操作的代碼。 – Haphazard 2011-06-10 18:11:08
@Haphazard基本上錯誤是我的bim對象是null - – molleman 2011-06-10 18:28:59
我對「ToolkitImage」一無所知,所以我懷疑我可以得到任何幫助。 – Haphazard 2011-06-10 18:29:41