示例如何使用 php 从 Android 设备上传

发布于 2024-11-19 12:18:56 字数 1453 浏览 1 评论 0原文

我是android新手,但检查了网络和一些书籍,我制作了一个必须发送图像和另外两个参数的应用程序。我只想制作一个 php 服务脚本来检查数据是否上传/发送正常。

这是我的代码:

public void executeMultipartPost() //throws Exception
    {
        try {
            //convert image to stream, strImage is an image converted to string
            InputStream is          = new ByteArrayInputStream( this.strImage.getBytes("UTF-8") );  
            HttpClient httpClient   = new DefaultHttpClient();
            HttpPost postRequest    = new HttpPost("http://192.168.12.12/androidlistener/test.php");
            byte[] data             = IOUtils.toByteArray(is);
            InputStreamBody isb     = new InputStreamBody(new ByteArrayInputStream(data), "uploadedFile");
            //These params were filled in before this process

            StringBody sPhoneId     = new StringBody(strPhoneId);
            StringBody sMessage     = new StringBody(strMessage);

            MultipartEntity multipartContent = new MultipartEntity();
            multipartContent.addPart("uploadedFile", isb);
            multipartContent.addPart("phoneid", sPhoneId);
            multipartContent.addPart("msg", sMessage);
            postRequest.setEntity(multipartContent);
            HttpResponse response   = httpClient.execute(postRequest);
            response.getEntity().getContent().close();
        } catch (Throwable e)
        {
            // Manejar error
        }
    }

所以我需要帮助来制作 php 脚本。先感谢您。

i am new in android, but checking the web, and some books i make an app that must send an image and two more params. I just want to make a php serve script that checks if the data was uploaded/sent fine.

So this is my code:

public void executeMultipartPost() //throws Exception
    {
        try {
            //convert image to stream, strImage is an image converted to string
            InputStream is          = new ByteArrayInputStream( this.strImage.getBytes("UTF-8") );  
            HttpClient httpClient   = new DefaultHttpClient();
            HttpPost postRequest    = new HttpPost("http://192.168.12.12/androidlistener/test.php");
            byte[] data             = IOUtils.toByteArray(is);
            InputStreamBody isb     = new InputStreamBody(new ByteArrayInputStream(data), "uploadedFile");
            //These params were filled in before this process

            StringBody sPhoneId     = new StringBody(strPhoneId);
            StringBody sMessage     = new StringBody(strMessage);

            MultipartEntity multipartContent = new MultipartEntity();
            multipartContent.addPart("uploadedFile", isb);
            multipartContent.addPart("phoneid", sPhoneId);
            multipartContent.addPart("msg", sMessage);
            postRequest.setEntity(multipartContent);
            HttpResponse response   = httpClient.execute(postRequest);
            response.getEntity().getContent().close();
        } catch (Throwable e)
        {
            // Manejar error
        }
    }

So i need a help to make the php script. Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨洒年华 2024-11-26 12:18:56

您应该添加一个要上传的实体,它是一个校验和(不慢于 md5,记住电池寿命)。然后,您将在服务器端将图像校验和与“uploadedFile”实体进行比较。我有点忘记 HTTP 是如何工作的,但建议使用 Base64 上传文件。

一个简单的校验和看起来像 multipartContent.addPart("checksum", isb.getContentLength);

这将检查上传的字节,而不实际检查一致性,在您的平均环境中,这很好,因为位在传输过程中不太可能发生变化,除非它们是中间人。

公平地说,您也可以几乎只是检查以确保消息包含在发布值中,如果是这样,连接不太可能断开,因为它会最后发送,但最佳实践是通过。

You should add an Entity to upload that is a checksum (not slower than md5 remember battery life). You would then compare server-side the image check-sum with the "uploadedFile" entity. I somewhat forget how HTTP works but it may be advisable to use Base64 to upload the file.

A simple checksum would look like multipartContent.addPart("checksum", isb.getContentLength);

This will check the bytes uploaded without actually checking for consistency, in your average environment this is fine as bits aren't likely to shift in transit unless their is a man-in-the middle.

To be fair you could also just almost just check to make sure that msg is included in the post values, if so it's unlikely that the connection dropped as it would be sent last, but best practice is to be through.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文