-1

How to upload a zip file to remote server using httpurlconnection? please provide me some url links for example code...Thnx

BhagyaNivi
  • 741
  • 6
  • 12

3 Answers3

1

Try following code and confirm that's workable solution:

StringBuffer responseBody=new StringBuffer();
 Log.i(Constants.TAG, "Ready to upload file...");
 HttpClient client=new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

   Log.i(Constants.TAG, "Set remote URL...");
 HttpPost post=new HttpPost("http://IP.IP.IP.IP/file_upload.php");
 MultipartEntity entity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

 Log.i(Constants.TAG, "Adding file(s)...");
entity.addPart("uploadedfile", new FileBody((FileObj), "application/zip"));
entity.addPart("uploadedfile2", new FileBody((FileObj), "application/zip"));

  Log.i(Constants.TAG, "Set entity...");
    post.setEntity(entity);

    BufferedReader bs=null;
   try
  {
    Log.i(Constants.TAG, "Upload...");
    HttpEntity hEntity=client.execute(post).getEntity();
    bs=new BufferedReader(new InputStreamReader(hEntity.getContent()));
      Log.i(Constants.TAG, "Response length - "+hEntity.getContentLength());
 String s="";
   while(s!=null)
{
responseBody.append(s);
s=bs.readLine();
Log.i(Constants.TAG, "Response body - "+s);
 }
bs.close();
 }
   catch(IOException ioe)
 {
    Log.i(Constants.TAG, "Error on getting response from Server, "+ioe.toString());
   ioe.printStackTrace();
   responseBody.append("...");
 }

My platform is Android 2.2, and use this solution need to get httpmime.jar as project library.

Priya
  • 1,763
  • 1
  • 12
  • 11
1

The following class works as well for me. You may upload a video,audio and any files in your device to remote server.

private class Upload extends AsyncTask<Void, Void, Void> {

    protected void onPreExecute() {
        mProgressDialog = ProgressDialog.show(ImageUpload.this,
                "Please wait...", "Loading...");
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Your upload Server SCRIPT
        String urlString = "http://192.168.1.176:1001/...  //  You server URL";
        // The file

         // The selected path is the location of the file in your device. 

        File file = new File(selectedPath);

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(urlString);

        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        // There are more examples above
        FileBody fb = new FileBody(file, "audio/3gpp");

        if (file.getName().endsWith(".xml")) {
            fb = new FileBody(file, "text/xml");
            reqEntity.addPart("xml_submission_file", fb);
            Log.v("Debug", "  file type,   adding file: " + file.getName());
        } else if (file.getName().endsWith(".jpg")) {
            fb = new FileBody(file, "image/jpeg");
            reqEntity.addPart(file.getName(), fb);
            Log.v("Debug", "  file type,   adding file: " + file.getName());
        } else if (file.getName().endsWith(".3gpp")) {
            fb = new FileBody(file, "audio/3gpp");
            reqEntity.addPart(file.getName(), fb);
            Log.v("Debug", "  file type,   adding file: " + file.getName());
        } else if (file.getName().endsWith(".3gp")) {
            fb = new FileBody(file, "video/3gpp");
            reqEntity.addPart(file.getName(), fb);
            Log.v("Debug", "  file type,   adding file: " + file.getName());
        } else if (file.getName().endsWith(".mp4")) {
            fb = new FileBody(file, "video/mp4");
            reqEntity.addPart(file.getName(), fb);
            Log.v("Debug", "  file type,   adding file: " + file.getName());
        } else {
            Log.w("Debug", "unsupported file type, not adding file: "
                    + file.getName());
        }

        FormBodyPart bodyPart = new FormBodyPart("uploadedfile", fb);
        reqEntity.addPart(bodyPart);
        httppost.setEntity(reqEntity);

        try {
            HttpResponse response = httpclient.execute(httppost);

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder mUploadResponse = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) {
                mUploadResponse = mUploadResponse.append(sResponse);
            }

            JSONObject mUploadResponseObject = new JSONObject(
                    mUploadResponse.toString());

            mUploadResponseObject.getJSONArray("response");

            try {
                JSONArray jsonArray = mUploadResponseObject
                        .getJSONArray("response");
                for (int i = 0; i < jsonArray.length(); i++) {
                    uploadStatus = jsonArray.getJSONObject(i)
                            .getJSONObject("send").getString("message");
                    uploadPhotoID = jsonArray.getJSONObject(i)
                            .getJSONObject("send").getString("id");
                }
            } catch (Exception e) {
                Log.d("DEBUG",
                        "The Json response message : " + e.getMessage());
            }

        } catch (ClientProtocolException e) {
            Log.d("DEBUG",
                    "The server ClientProtocolException response message : "
                            + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            Log.d("DEBUG", "The server  IOException response message : "
                    + e.getMessage());
            e.printStackTrace();
        } catch (JSONException e) {
            Log.d("DEBUG", "The  JSONException server response message : "
                    + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
}

protected void onPostExecute(Void result) {

    if (mProgressDialog.isShowing() && mProgressDialog != null) {
        mProgressDialog.dismiss();
    }
    mHandler.sendEmptyMessage(0);
}

It works well for me. Try this out.

Rethinavel
  • 3,912
  • 7
  • 28
  • 49
0

What have you tried, and what problems are you facing? Uploading a zip file is no different than uploading any other files.

There are a large number of examples that you can find online.

But again, it depends on how your remote server is expecting it to be uploaded. If you can update your question with more details, that would help.

For the time being, you can go through these links.

How to upload binary file using URLConnection

Community
  • 1
  • 1
Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68