嗨,我在谷歌上搜索了很多有关从 Android 手机到服务器的图像上传的信息,并且我已经完成了以下代码
例如
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();
不知道为什么,但图像没有上传到服务器,我正在关注 logcat
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): )
这里来自服务器TYPE没有返回任何值,所以我无法提供更新
请任何人都可以帮助解决这个问题
最佳答案
我已经测试过,它完全可以工作
你只需要改变
这段代码
MultipartEntity 实体 = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE);
至
MultipartEntity 实体 = new MultipartEntity( HttpMultipartMode.STRICT);
一切都会完成,所有其他代码都很好
关于java - 图片上传到服务器 android httppost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23549222/