Java 图像上传到服务器android httppost
嗨,我在谷歌上搜索了很多关于从android手机上传到服务器的图片,我完成了以下代码 比如说Java 图像上传到服务器android httppost,java,android,Java,Android,嗨,我在谷歌上搜索了很多关于从android手机上传到服务器的图片,我完成了以下代码 比如说 HttpClient httpClient = new DefaultHttpClient(); HttpContext localContext = new BasicHttpContext(); HttpPost httpPost = new HttpPost(url); try { MultipartEntity entity = new Multipart
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
try {
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
for (int index = 0; index < nameValuePairs.size(); index++) {
if (nameValuePairs.get(index).getName()
.equalsIgnoreCase("image")) {
entity.addPart(nameValuePairs.get(index).getName(),new FileBody(new File(
nameValuePairs.get(
index)
.getValue()),
"image/jpg"));
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost,
localContext);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
HttpClient-HttpClient=newdefaulthttpclient();
HttpContext localContext=新的BasicHttpContext();
HttpPost HttpPost=新的HttpPost(url);
试一试{
多方实体=新多方实体(
HttpMultipartMode.BROWSER_兼容);
对于(int index=0;index
不知道为什么,但图像并没有上传到服务器,我得到以下日志
05-08 23:20:17.452: I/System.out(2501): Response from serveer <pre>Array
05-08 23:20:17.454: I/System.out(2501): (
05-08 23:20:17.455: I/System.out(2501): [image] => Array
05-08 23:20:17.455: I/System.out(2501): (
05-08 23:20:17.455: I/System.out(2501): [name] => IMG_20130101_164850.jpg
05-08 23:20:17.455: I/System.out(2501): [type] =>
05-08 23:20:17.455: I/System.out(2501): [tmp_name] => C:\Windows\Temp\php5CD4.tmp
05-08 23:20:17.456: I/System.out(2501): [error] => 0
05-08 23:20:17.456: I/System.out(2501): [size] => 1988151
05-08 23:20:17.456: I/System.out(2501): )
05-08 23:20:17.456: I/System.out(2501): )
05-08 23:20:17.452:I/System.out(2501):服务器阵列的响应
MultipartEntity entity = new MultipartEntity();
FileBody fileBody = new FileBody(...)
entity.addPart("nameOfThePartTheServerExpects", fileBody);
entity.addPart(...) // additional parts if the server needs stuff.
httpPost.setEntity(entity);
httpClient.execute(httpPost, localContext);
05-08 23:20:17.454:I/系统输出(2501):(
05-08 23:20:17.455:I/System.out(2501):[图像]=>阵列
05-08 23:20:17.455:I/系统输出(2501):(
05-08 23:20:17.455:I/System.out(2501):[name]=>IMG_20130101_164850.jpg
05-08 23:20:17.455:I/System.out(2501):[类型]=>
05-08 23:20:17.455:I/System.out(2501):[tmp_name]=>C:\Windows\Temp\php5CD4.tmp
05-08 23:20:17.456:I/System.out(2501):[错误]=>0
05-08 23:20:17.456:I/System.out(2501):[size]=>1988151
05-08 23:20:17.456:I/系统输出(2501):)
05-08 23:20:17.456:I/系统输出(2501):)
这里从服务器类型没有返回任何值,因此我无法提供更新
请任何人都可以帮助解决这个问题我想到两种方法: 坏(消耗内存) 好(有利于记忆) 我强烈建议您从一个网页上检查帖子的正常web实现,这样您就可以看到所有部分的名称,这些部分将对应于所有的entity.addPart()
另外,您不推荐使用FileBody,请参阅。我已经测试过,它完全正常工作 你只需要改变一下 此代码用于 多方实体=新多方实体( HttpMultipartMode.BROWSER_兼容) 到 多方实体=新多方实体( HttpMultipartMode.STRICT) 所有其他代码都可以
MultipartEntity entity = new MultipartEntity();
InputStreamBody inputStreamBody = new InputStreamBody(is, someRandomFilename);
entity.addPart("nameOfThePartTheServerExpects", inputStreamBody);
entity.addPart(...) // additional parts if the server needs stuff.
httpPost.setEntity(entity);
httpClient.execute(httpPost, localContext);