这个问题是从same question借来的,因为我面临同样的问题 . 在服务器端发布图像时,图像细节无法进入服务器端 . 像这样:
无法发布图像文件信息php服务器,userid ok但图像文件信息无法发布 . 在这种情况下,Image在服务器上成功保存,我无法获取有关图像的php服务器的信息 .
这是代码:
公共类UploadImageHttpPost {
String testpath="/mnt/sdcard/14111.jpg";
String url = "http://andtuts.com/uploadImage.php";
public static void sendPost(String url, String imagePath,String userId)
throws IOException, ClientProtocolException {
String responseBody;
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(imagePath);
FileBody encFile = new FileBody(file,"image/jpeg");
entity.addPart("images", encFile);
entity.addPart("UserId", new StringBody(userId));
HttpPost request = new HttpPost(url);
request.setEntity(entity);
HttpClient client = new DefaultHttpClient();
ResponseHandler<String> responsehandler = new BasicResponseHandler();
responseBody = client.execute(request, responsehandler);
if (responseBody != null && responseBody.length() > 0) {
Log.w("TAG", "Response from image upload->" + responseBody);
}
}
我喜欢这个:
Array
(
[UserId] => 1
)
我在php中使用它进行测试,来自multipart:
$ filedata = $ _FILES ['images']; $ f = fopen(time() . “ . txt”,'wb'); fwrite($ f,print_r($ _ REQUEST,true)); FCLOSE($ F);
但是下面的部分缺失意味着我没有得到,多部分post方法中是否有任何遗漏的代码请检查上面的java代码:
[images] => Array
(
[name] => ball.png
[type] => image/png
[tmp_name] => /tmp/phpiQHIXQ
[error] => 0
[size] => 1664972
)
我也遵循这个教程:[http://www.webspeaks.in/2012/08/upload-files-from-android-to-server.html][2]
[2]:http://www.webspeaks.in/2012/08/upload-files-from-android-to-server.html和 googled 中的很多其他,但找不到合适的解决方案 .
1 回答
我最近做的就像你的问题这是解决方案:
来自SD卡和相机的图像:
现在,您调用要上传图片的函数:
这是上传图片类别:
我希望你对这段代码感到满意 . 我成功运行了 .