【问题标题】:Send files from android device to server?将文件从android设备发送到服务器?
【发布时间】:2014-02-13 12:47:49
【问题描述】:

如何将所有文件从 sdcard 发送到服务器我可以通过这个将文件从设备推送到服务器 代码:

public void btnclick(View v) {

        String pathToOurFile = "/mnt/sdcard/" + "q.3gp";
        String urlServer = "http://www.google.com/upload.php";
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;

        try {

            FileInputStream fileInputStream = new FileInputStream(new File(
                    pathToOurFile));

            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("ENCTYPE", "multipart/form-data");
            connection.setRequestProperty("Content-Type",
                    "multipart/form-data;boundary=" + boundary);
            connection.setRequestProperty("uploaded_file", "gopivideo");
            outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream
                    .writeBytes("Content-Disposition: form-data; name=\"sample\";filename=\""
                            + pathToOurFile + "\"" + lineEnd);
            outputStream.writeBytes(lineEnd);

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens
                    + lineEnd);

            // Responses from the server (code and message)
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            Toast.makeText(getApplicationContext(),
                    serverResponseCode + "," + serverResponseMessage,
                    Toast.LENGTH_LONG).show();

            fileInputStream.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception ex) {
            // Exception handling
        }
    }
}

这对于通过单次推送将任何类型的文件上传到服务器非常有用 但需要的是循环sdcard中的所有文件并一一发送。 谢谢

【问题讨论】:

    标签: php android file-upload


    【解决方案1】:

    您可以通过 Multipart post 请求来做到这一点:(这样,您不需要创建 json)

       HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(serverURL);
        MultipartEntity postEntity = new MultipartEntity();
        File file = new File("Your File path on SD card");
        postEntity.addPart("fileupload", new FileBody(file, "image/jpeg"));
        postEntity.addPart("loginKey", new StringBody(""+loginKey));
        postEntity.addPart("message", new StringBody(message));
        postEntity.addPart("token", new StringBody(token));
        post.setEntity(postEntity);
        response = client.execute(post);
    

    并添加Mime4

    【讨论】:

    • 感谢您的帖子,但您的代码中没有循环我已经使用了这个我需要的是循环存储卡中的所有类型的文件并通过单击一个按钮一一发送或执行我是 android 的初学者,请帮帮我。
    • 通过字节数组可以发送文件,并且可以从服务器检索,你必须解码文件
    猜你喜欢
    • 2016-07-28
    • 2012-12-09
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-06
    • 2011-11-20
    相关资源
    最近更新 更多