-3

I would like the users of my Android app to be able to send zip files to my server using http. The use case I am imagining looks like this:

  • The user sends a zip file and a String containing a password using a HttpClient and HttpPost (as described here) to my server.

  • By logging in to www.example.com/my_app with existing password, users can download the files that the people have sent to my server under the given password.

I don't understand how to do the second step. What code do I need to write on my website to receive files that the Android users have been sending? I have a shared server under my hosting plan and a simple website.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sid
  • 563
  • 1
  • 10
  • 28
  • Would be useful if someone explained what makes this a bad question instead of just downvoting it. – Sid Dec 23 '17 at 11:30
  • I think it's because you are looking for someone to teach you something rather than helping with a specific question or problem. Your question is too "big". – Warren Dec 23 '17 at 12:51

1 Answers1

1

You first need to modify the upload code(Since file upload is treated as multipart data). Here is the modified upload code--

String url = "http://localhost/upload.php";
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
                "file.txt");
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            Part[] parts = new Part[1];
            parts[0] =  new FilePart("fileToUpload", file);
            MultipartEntity reqEntity = new MultipartEntity(parts);
            reqEntity.setContentType("binary/octet-stream");
            reqEntity.setChunked(true); // Send in multiple parts if needed
            httppost.setEntity(reqEntity);
            HttpResponse response = httpclient.execute(httppost);
        } catch (Exception e) {
            e.printStackTrace();
        }

Here I have used the fileToUpload as the parameter key for uploaded file. On server code you can use the same key for your $_FILES["fileToUpload"]. Here is the simplest PHP code to accept the uploaded data from above android code--

<?php
$target_dir = "/Users/chauhan/Desktop/uploads/";
$target_file = $target_dir . basename("abc.txt");
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
?>
Mata Prasad Chauhan
  • 554
  • 2
  • 6
  • 15