如何从Android发送图像的servlet,然后存放 [英] How to send an image from android to servlet and then store it
问题描述
我试图发送照片通过HTTP到我的服务器。所以,我将图像转换为字节,然后我把它作为整个一个名称值对。这里是我的下面,这样做code。现在我的问题是在服务器端,我怎么能重新和图像从字节收到字符串存储
I am trying to send a photo over http to my server. So i convert the image to bytes and then i send it across as a name value pair. Here is my code below for doing so. Now my trouble is the server side, how can i recreate and store the image from the string of bytes recieved
我也是使用Java servlet
i also am using java servlets
在Android code
Code on 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();
}
在服务器上code
Code on server
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();
}
这上面code写入一个文件,但它不会打开为图像。也就是这个字节[] B = fromClientphoto.getBytes();
转换回相同的字节Android手机上的正确方法是什么?任何想法?
This above code writes a file but it will not open as an image. also is this byte[] b = fromClientphoto.getBytes();
the correct way to convert back to same bytes as on the android phone? any ideas?
推荐答案
不要工具箱形象花心,只是写的内容到一个文件中。
Don't bother with the toolkit image, just write the contents out to a file..
你能张贴图片到Android之外的Servlet?设置,通过一个基本的HTML表单做同样的事伪上,分解问题成越来越小的碎片,直到你找出whhich片引起的问题...
can you post an image to the Servlet outside of android? setup a dummy page that does the same thing through a basic HTML Form, break down the problem into smaller and smaller pieces until you figure out whhich piece is causing the problem...
这篇关于如何从Android发送图像的servlet,然后存放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!