简体   繁体   English

将照片从Android应用程序发送到服务器端

[英]Sending photo from Android application to server side

I'm about to write server side aplication(most probably it would be PHP but JAVA is also possible) and android client side aplication. 我将要编写服务器端应用程序(最有可能是PHP,但也可能是JAVA)和android客户端应用程序。 I try to figure out what is the best way to send photo from android aplication to server and receive it in server side. 我试图找出最好的方法是从Android应用程序向服务器发送照片并在服务器端接收照片的最佳方法。 And if this any way to optimize/serialize sending more than one picture at a time? 而且,是否可以通过这种方式优化/序列化一次发送多张图片?
Please provide me some reference or hint. 请提供一些参考或提示。
Thanks in advance. 提前致谢。

U can use HTTP post for this. 您可以使用HTTP发布。 get ByteArrayOutputStream and compress JPEG image and use ByteArrayBody and post it using HttpClient 获取ByteArrayOutputStream并压缩JPEG图像,然后使用ByteArrayBody并使用HttpClient将其发布

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        bm.compress(CompressFormat.JPEG, 75, bos);

        byte[] data = bos.toByteArray();

        HttpClient httpClient = new DefaultHttpClient();

        HttpPost postRequest = new HttpPost(

                "http://10.0.2.2/cfc/iphoneWebservice.cfc?returnformat=json&method=testUpload");

        ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");

        // 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("photoCaption", new StringBody("sfsdfsdf"));

        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 ((sResponse = reader.readLine()) != null) {

            s = s.append(sResponse);

        }

You can find related code here. 您可以在此处找到相关代码。 http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/ http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM