通过HttpPost从Android的发送图像(JPG格式)来的Servlet(Web服务器) [英] Send image(jpg) via HttpPost from Android to Servlet(WebServer)

查看:221
本文介绍了通过HttpPost从Android的发送图像(JPG格式)来的Servlet(Web服务器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形象的坐在我的SD卡(类型为JPG),我试图通过HttpPost将图像发送给我的servlet在Apache Tomcat 7.0

I have an image sitting on my SDcard (of type jpg) and I am trying to send the image via HttpPost to my servlet running on Apache Tomcat 7.0

到目前为止,我有谷歌,荷兰国际集团,以做到这一点的方式,但我似乎无法找到完美的方式。

So far I have google-ing the way to do this but I can't seem to find the perfect way.

做任何你可能有一些建议或解决这个问题呢?

Do any of you might have some suggestion or solution to this problem?

感谢你在前进, 萨米斯蒂文Djap

Thank you in advance, Sammy Stevan Djap

推荐答案

HttpClient的是使用类,有一个教程它。见一节的使用HTTP客户端的,照片后面。

HttpClient is the class to use and there's a tutorial for it. See the section Using HTTP client, right after the pictures.

更新
以下是评论从 Developerlife.com 教程的例子。有关示例的好处是,它演示了如何通过编码一种类型变成另一种发送各种类型的数据。一个可以发送任何在于链的数据转换所使用的类型的,由起始于相匹配的数据的类型被发送链中的点:

UPDATE
What follows is commentary on the tutorial example from Developerlife.com. A good thing about that example is that it demonstrates how to send various types of data by encoding one type into another. One can send any of the types used in that chain of data conversions, by starting at the point in the chain that matches the type of data to be sent:

字符串的都放在一个的的Hashtable 的被写入到的的ObjectOutputStream 的是由一个支持的 ByteArrayOutputStream 的是被转换成的ByteArray依次转换成ByteArrayEntity用于传输。

Strings are put in a Hashtable which is written to an ObjectOutputStream which is backed by a ByteArrayOutputStream that gets converted into a ByteArray that is in turn converted into a ByteArrayEntity for transmission.

要发送短短的的ByteArray 的,跳过所有出现的步骤将数据变成的的ByteArray 的面前。跳转在第26行,其中一个的的ByteArray 的与的toByteArray()创建的。

To send just a ByteArray, skip all the steps that occur before the data becomes a ByteArray. Jump in at line 26 where a ByteArray is created with toByteArray().

有关发送其他类型执行以下操作(根据为例):
第26行:的ByteArray 的,只是用它来制作的 ByteArrayEntity
第26行: ByteArrayOutputStream 的,将其转换为的的ByteArray
24号线: ObjectOutputStreams 的:创建他们的 ByteArrayOutputStreams
第25行:对象的:写的字符串散列表的,等到的的ObjectOutputStream 的。

For sending other types do the following(as per the example):
Line 26: ByteArray, just use it to make a ByteArrayEntity
Line 26: ByteArrayOutputStream, convert it to a ByteArray
Line 24: ObjectOutputStreams: create them on ByteArrayOutputStreams
Line 25: Objects: Write Strings, Hashtables, etc to an ObjectOutputStream.

 1 /** this method is called in a non-"edt" thread */
 2 private void _doInBackgroundPost() {
 3   Log.i(getClass().getSimpleName(), "background task - start");
 4 
 5 
 6   Hashtable<String, String> map = new Hashtable();
 7   map.put("uid", uid);
 8   map.put("pwd", pwd);
 9 
10   try {
11     HttpParams params = new BasicHttpParams();
12 
13     // set params for connection...
14     HttpConnectionParams.setStaleCheckingEnabled(params, false);
15     HttpConnectionParams.setConnectionTimeout(params, NetworkConnectionTimeout_ms);
16     HttpConnectionParams.setSoTimeout(params, NetworkConnectionTimeout_ms);
17     DefaultHttpClient httpClient = new DefaultHttpClient(params);
18 
19     // create post method
20     HttpPost postMethod = new HttpPost(LoginServiceUri);
21 
22     // create request entity
23     ByteArrayOutputStream baos = new ByteArrayOutputStream();
24     ObjectOutputStream oos = new ObjectOutputStream(baos);
25     oos.writeObject(map);
26     ByteArrayEntity req_entity = new ByteArrayEntity(baos.toByteArray());
27     req_entity.setContentType(MIMETypeConstantsIF.BINARY_TYPE);
28 

这篇关于通过HttpPost从Android的发送图像(JPG格式)来的Servlet(Web服务器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆