Android中的图像上传到休息服务

时间:2013-11-13 17:01:52

标签: android http-post restful-url

请帮我编写android代码,将图片上传到服务器。

我的服务器端代码是

function uploadFiles( fileOrBlob ) {
var name = 'unspecified';
if (fileOrBlob['name']) {
name = fileOrBlob.name;
}
var xhr = new XMLHttpRequest();
xhr.open('POST', '/eskom/gallery/imagepost', true);
xhr.setRequestHeader('plxImgName', name);
xhr.setRequestHeader('plxName', 'Sean Williams');
xhr.setRequestHeader('plxEmail', 'email@address.com');
xhr.setRequestHeader('plxTel', '+25665567');
xhr.setRequestHeader('plxCompany', 'IBM');
xhr.onload = onload;
xhr.send(fileOrBlob);
} 

我的Android代码

 ByteArrayOutputStream bos = new ByteArrayOutputStream();
            im_bitmap.compress(CompressFormat.JPEG, 60, bos);
            byte[] data = bos.toByteArray();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(url);
            ByteArrayBody bab = new ByteArrayBody(data, simagename);
            // File file= new File("/mnt/sdcard/forest.png");
            // FileBody bin = new FileBody(file);
            MultipartEntity reqEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("uploaded", bab);
            reqEntity.addPart("plxEmail", new StringBody("email@address.com"));
            reqEntity.addPart("plxImgName", new StringBody(simagename));
            reqEntity.addPart("plxName", new StringBody(pref.getString("username", "")));
            reqEntity.addPart("plxTel", new StringBody("+27111234567"));
            reqEntity.addPart("plxComany", new StringBody("IBM"));
            postRequest.setEntity(reqEntity);
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();

            while ((response = reader.readLine()) != null) {
                s = s.append(response);
            }

但是图片没有上传..我也收到了错误信息 “评估资源模板时出错:POST gallery / imagepost /”

由于

0 个答案:

没有答案