Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:46:59+00:00 2026-06-15T19:46:59+00:00

I am uploading a file on server using this code, But I want such

  • 0

I am uploading a file on server using this code, But I want such a functionality: if it stops due to lost a network or any other interruption during the process, then it shouldn’t be start uploading from beginning at second time. response from server is also customizable. is it possible in android? what approach should I use to do this? please guide me.
and if possible please suggest me any sample piece of code.

Thanks!

public String uploadFile(InputStream is) {
    HttpURLConnection conn;
    String jsonResponse = "";
    int streamSize = 0;
    DataOutputStream dos = null;
    DataInputStream inStream = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesAvailable, bufferSize;
    byte[] buffer;

    String urlString = "*******<My URL>*******";
    try {
        // ------------------ CLIENT REQUEST
        // FileInputStream fileInputStream = new FileInputStream(new
        // File(selectedPath) );

        // open a URL connection to the Servlet

        URL url = new URL(urlString);
        // Open a HTTP connection to the URL
        conn = (HttpURLConnection) url.openConnection();
        // Allow Inputs
        conn.setDoInput(true);
        // Allow Outputs
        conn.setDoOutput(true);
        // Don't use a cached copy.
        conn.setUseCaches(false);

        conn.setChunkedStreamingMode(0);
        // Use a post method.
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type",
                "multipart/form-data;boundary=" + boundary);
        dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"wheeze_file\";filename="
                + fileName + lineEnd);
        dos.writeBytes(lineEnd);

        // create a buffer of maximum size
        bytesAvailable = is.available();
        streamSize = bytesAvailable;
        bufferSize = 2048;
        buffer = new byte[bufferSize];
        // read file and write it into form...
        int i = 0;
        int length = 0;
        while ((length = is.read(buffer, 0, bufferSize)) > 0) {
            if (isCancelled()) {
                Toast.makeText(getApplicationContext(), "Cancelled",
                        Toast.LENGTH_SHORT).show();
                return null;
            }
            dos.write(buffer, 0, length);
            bytesAvailable = is.available();

            // bufferSize = Math.min(bytesAvailable, maxBufferSize);

            publishProgress(streamSize, bytesAvailable);
            // Log.v("Progress",""+streamSize+" : "+bytesAvailable);
        }
        // send multipart form data necesssary after file data...
        dos.writeBytes(lineEnd);

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"data[authentication][email]\""
                + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(username);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"data[authentication][password]\""
                + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(enc_password);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"method\""
                + lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes("uploadWheezeFile");
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        // close streams
        Log.e("Debug", "File is written");

        // Log.v("Conn status", "Disconnected");

    } catch (MalformedURLException ex) {
        ex.printStackTrace();
        Log.e("Debug", "MURLExerror: " + ex.getMessage(), ex);
    } catch (IOException ioe) {
        ioe.printStackTrace();
        Log.e("Debug", "IOEx error: " + ioe.getMessage(), ioe);
        uploadTask.cancel(true);
        return null;
    } finally {
        try {
            is.close();
            dos.flush();
            dos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    // ------------------ read the SERVER RESPONSE
    try {
        inStream = new DataInputStream(conn.getInputStream());

        if ((jsonResponse = inStream.readLine()) != null) {
            Log.e("Debug", "Server Response " + jsonResponse);

        } else {
            jsonResponse = "";
        }

        inStream.close();
        conn.disconnect();
        //
    } catch (IOException ioex) {
        ioex.printStackTrace();
        Log.e("Debug", "error: " + ioex.getMessage(), ioex);
    }

    return jsonResponse;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:47:00+00:00Added an answer on June 15, 2026 at 7:47 pm

    If the receiving server supports it, you can use the Content-Range header to identify a resuming upload. The Google-Drive API supports it. If you roll your own, I’d follow the pattern Google uses:

    • Begin upload and get a session identifier.
    • When upload is interrupted, wait until internet is available to resume.
    • When resuming upload, first inquire of the server how many bytes it has received. (*)
    • Resume upload with the next byte after the server’s status.

    (*) Note that when I rolled my own before, I added an encoded response from the server with the last KB of upload, just to verify it wasn’t corrupted in transfer. However, I’ve never, in production, seen a case where a server had received corrupt data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to run code from this site http://stunningco.de/2010/04/25/uploading-files-to-http-server-using-post-android-sdk/ but I have a problem,
I used this example (http://reecon.wordpress.com/2010/04/25/uploading-files-to-http-server-using-post-android-sdk/) to upload file from android device to server, it
I am currently looking in to some file uploading using Java Server Faces. I've
I am uploading audio file from server, I want the name of files should
I am uploading a stream to server.But my Input-stream contain a big video file.So
am I uploading this file right? Here is my code: Deleted Code it is
I'm uploading files into FTP using this code: http://msdn.microsoft.com/en-us/library/ms229715.aspx . It's all good, but
I have this form for uploading images to the server using HttpPost. I did
GETTING THIS ERROR WHEN UPLOADING A FILE : LoadError (no such file to load
I am trying to upload a file to a ftp server using libcurl but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.