简体   繁体   中英

Send files from android device to server?

How to send all files from sdcard to server i can push file from device to server through this Code :

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
        }
    }
}

This is working cool for uploading any type of file to server with single push but what is need is to loop all files in sdcard and send one by one. Thank you

You can do this with a Multipart post request:(This way, you dont need to create 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);

and add Mime4 library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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