问题描述
我的sdcard上有一个图像(JPG类型),我试图通过httppost将图像发送到我在apache tomcat上运行的servlet 7.0
到目前为止,我有Google-ing可以做到这一点,但我似乎找不到完美的方法.
你们中的任何一个可能对此问题有一些建议或解决方案?
预先感谢您 Sammy Stevan DJAP
推荐答案
httpclient 使用和有一个教程.在图片之后,请参阅使用HTTP客户端的部分.
. 更新
接下来的是对 developerLife.com 的评论.该示例的一件好事是,它通过将一种类型编码到另一种类型来演示如何发送各种类型的数据.可以通过与要发送的数据类型相匹配的链中开始发送该数据转换链中使用的任何类型:
strings 放在A hashtable 中,该写入 ObjectOutputStream ,并由A bytearrayOututputstream 支持被转换为横向上方,又转换为传输的旁观性.
仅发送 bytearray ,请跳过数据变为a bytearray 之前发生的所有步骤.在第26行中跳入 bytearray 使用toByteArray().
创建用于发送其他类型的其他类型(按照示例):
第26行: bytearray ,只需使用它来制作 bytrearentity
第26行: bytearrayoutputstream ,将其转换为 bytearray
第24行: ObjectOutputStreams :在 bytearrayoutputstreams
上创建它们
第25行:对象:Write strings , Hashtables ,等于 ObjectOutputputStream .
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
其他推荐答案
我以稍微清洁的方式做了此操作,但使用ASP.NET'Web form'做到了这一点.我相信有人可以轻松地在servlet中对此进行改头换面.您可以找到我的解决方案在这里
欢呼 法哈尔
问题描述
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?
Thank you in advance, Sammy Stevan Djap
推荐答案
HttpClient is the class to use and there's a tutorial for it. See the section Using HTTP client, right after the pictures.
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:
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.
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().
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
其他推荐答案
I have done this in a little bit cleaner way but with asp.net 'web form'. I am sure someone can easily do a makeover of that in servlet. You can find my solution here.
cheers
Fahar