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

  • Home
  • SEARCH
  • 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 8825735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:57:01+00:00 2026-06-14T06:57:01+00:00

I am sending a file from sd card by using the following code. HttpURLConnection

  • 0

I am sending a file from sd card by using the following code.

 HttpURLConnection conn = null;
         DataOutputStream dos = null;
         DataInputStream inStream = null;
         String existingFileName = Environment.getExternalStorageDirectory().getAbsolutePath() 
                 + "/111";

     File f = new File(existingFileName);
     String lineEnd = "\r\n";
     String twoHyphens = "--";
     String boundary =  "*****";
     int bytesRead, bytesAvailable, bufferSize;
     byte[] buffer;
     int maxBufferSize = 1*1024*1024;
     String responseFromServer = "";
     String urlString = "http://192.178.1.7/Geo/Prodect(filename)";
     try
     {
      //------------------ CLIENT REQUEST
     FileInputStream fileInputStream = new FileInputStream(f);
      // 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);
      // 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=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
      dos.writeBytes(lineEnd);
      // create a buffer of maximum size
      bytesAvailable = fileInputStream.available();
      bufferSize = Math.min(bytesAvailable, maxBufferSize);
      buffer = new byte[bufferSize];
      // read file and write it into form...
      bytesRead = fileInputStream.read(buffer, 0, bufferSize);
      //while (bytesRead > 0)
      Log.v("info",".size."+bytesRead);
      for(int n1=0;n1<bytesRead;n1++)
      {


       dos.write(buffer, 0, bufferSize);
       bytesAvailable = fileInputStream.available();
       bufferSize = Math.min(bytesAvailable, maxBufferSize);
       bytesRead = fileInputStream.read(buffer, 0, bufferSize);

      }

      dos.writeBytes(lineEnd);
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
      // close streams
      Log.v("info","File is written");
      fileInputStream.close();
      dos.flush();
      dos.close();
     }
     catch (MalformedURLException ex)
     {
          Log.v("info", "error: " + ex.getMessage(), ex);
     }
     catch (IOException ioe)
     {
          Log.v("info", "error: " + ioe.getMessage(), ioe);
     }
     //------------------ read the SERVER RESPONSE
     try {
           inStream = new DataInputStream ( conn.getInputStream() );
           String str;

           while (( str = inStream.readLine()) != null)
           {
                Log.v("info","Server Response "+str);
           }
           inStream.close();

     }
     catch (IOException ioex)
     {
          Log.v("info", "error: " + ioex.getMessage(), ioex);
     }

It works fine but,the file contains 20 lines it saved in the sever with 22 lines.

My actual file looks like as follows.

android app info
app name
app time

when i will send the flle to server from sd card the file look like as follows

1)**mnt/sdcard/111**

android app info
app name
app time

2)**

1 and 2 lines are added.

How i can send a file without those two lines .

If any one know the solution please help me .

Thanks in advance..

  • 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-14T06:57:02+00:00Added an answer on June 14, 2026 at 6:57 am
    public static StringBuffer post(String url,InputStream in) throws IOException {
            InputStream bufferInputStream = null;
            InputStreamReader responseInputStream = null;
            HttpURLConnection conn = null;
            OutputStream requestOutputStream = null;
            StringBuffer responseString = new StringBuffer();
            int bufferSize = 8192;
            byte[] byteBuffer  = new byte[bufferSize];
            int postDataSize = 0;
            try {
                if (in != null) {
                    bufferInputStream = new BufferedInputStream(in);
                }
                if (bufferInputStream != null) {
                    postDataSize = bufferInputStream.available();
                }
                //send request
                conn = getConnecttion(url);
                if (postDataSize > 0) {
                    requestOutputStream = conn.getOutputStream();
                    int position = 0;
    
                    while ((position = bufferInputStream.read(byteBuffer)) > -1) {
                        requestOutputStream.write(byteBuffer, 0, position);
                    }
                    requestOutputStream.flush();
                    requestOutputStream.close();
                    requestOutputStream = null;
                    byteBuffer = null;
                }
                // get response
                int responseCode = conn.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    responseInputStream = new InputStreamReader(conn.getInputStream(),"UTF-8");
    
                    char[] charBuffer = new char[bufferSize];
                    int _postion = 0;
                    while ((_postion=responseInputStream.read(charBuffer)) > -1) {
                        responseString.append(charBuffer,0,_postion);
                    }
                    responseInputStream.close();
                    responseInputStream = null;
                    charBuffer = null;
                }else{
                    throw new IOException("Respsone code:"+responseCode);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new IOException(e.getMessage());
            } finally {
                byteBuffer = null;
    
                if (responseInputStream != null) {
                    responseInputStream.close();
                    responseInputStream = null;
                }
                if (conn != null) {
                    conn.disconnect();
                    conn = null;
                }
                if (requestOutputStream != null) {
                    requestOutputStream.close();
                    requestOutputStream = null;
                }
                if (bufferInputStream != null) {
                    bufferInputStream.close();
                    bufferInputStream = null;
                }
            }
            return responseString;
        }
    

    Please reference to above simple code,

    send file like this:post(“http://youurl.com”,new FileInputStream(” you file path”))

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

Sidebar

Related Questions

I have got following code witch are sending xml file on HTTP protocol and
I am sending file from client to server using TCP. To mark the end
I'm sending the file file.txt from my Rails controller using send_file , and then
I am using XMLHttpRequest to send a file from javascript code to a django
I sending ajax request from a jQuery file like below, which expects response in
I am using this for sending file to user header('Content-type: application/zip'); header('Content-Length: ' .
I am sending an audio file with extension .m4a on the server using ftp
I'm sending a ms-word file using rails. i.e when I click on a link,
i write below code for sending id to another php file function selectCheckBox(k) {
I have created a text file in Unix environment using Java code. For writing

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.