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 6562783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:45:02+00:00 2026-05-25T13:45:02+00:00

im trying to send a file in android through sockets. i want to add

  • 0

im trying to send a file in android through sockets. i want to add the filename with the bytestream and then send it to the server. how do i do that? and then how do i seperate the filename on the receiving side?
this is the code to send file :

 Log.i("SocketOP", "sendFILE-1");
            File  f = new File(path);
            String filename=path.substring(path.lastIndexOf("/")+1);
            System.out.println("filename:"+filename); 
            fin.filename = "~"+filename;
            BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );

            FileInputStream fileIn = new FileInputStream(f);
            Log.i("SocketOP", "sendFILE-2");
            byte [] buffer  = new byte [(int)f.length()];
            System.out.println("SO sendFile f.length();" + f.length());
            int bytesRead =0;
            while ((bytesRead = fileIn.read(buffer)) > 0) {
                out.write(buffer, 0, buffer.length);
                System.out.println("SO sendFile" + bytesRead +filename);
            }
            out.flush();
            out.close();
            fileIn.close();
            Log.i("SocketOP", "sendFILE-3");
  • 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-05-25T13:45:03+00:00Added an answer on May 25, 2026 at 1:45 pm

    If this is your own protocol then you create a data packet that separate the two sections (filename and data). You need to denote clearly the separation via a particular boundary.

    On the server, since you understand the protocol, the server will read back the whole data packet and it will separate the filename and data based on the given boundary.

    MIME data format use exactly this kind of data exchange and widely use with HTTP protocol. If you use the same MIME Data Format, another advantage is you could use third party library to encode and decode your data such as HttpMime

    Below is the rough code to format the data using MIME data and send it through Socket

    File  f = new File(path);
    BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );
    String filename=path.substring(path.lastIndexOf("/")+1);
    
    // create a multipart message
    MultipartEntity multipartContent = new MultipartEntity();
    
    // send the file inputstream as data
    InputStreamBody isb = new InputStreamBody(new FileInputStream(f), "image/jpeg", filename);
    
    // add key value pair. The key "imageFile" is arbitrary
    multipartContent.addPart("imageFile", isb);
    
    multipartContent.writeTo(out);
    out.flush();
    out.close();
    

    Note that you would need org.apache.http.entity.mime.MultipartEntity and org.apache.http.entity.mime.content.InputStreamBody from HttpMime project. On the server, you need MIME parser that would get back the filename and all the bytes content

    To read the inputstream back on the server, you would need a class to parse the MIME message. You shouldn’t have to write the parser yourself as MIME is a popular message format already unless you want to learn about the MIME message structure.

    Below is the sample code using MimeBodyPart that is part of JavaMail.

    
    MimeMultipart multiPartMessage = new MimeMultipart(new DataSource() {
        @Override
        public String getContentType() {
            // this could be anything need be, this is just my test case and illustration
            return "image/jpeg";
        }
    
        @Override
        public InputStream getInputStream() throws IOException {
            // socket is the socket that you get from Socket.accept()
            BufferedInputStream inputStream = new BufferedInputStream(socket.getInputStream());
            return inputStream;
        }
    
        @Override
        public String getName() {
            return "socketDataSource";
        }
    
        @Override
        public OutputStream getOutputStream() throws IOException {
            return socket.getOutputStream();
        }
    });
    
    // get the first body of the multipart message
    BodyPart bodyPart = multiPartMessage.getBodyPart(0);
    
    // get the filename back from the message
    String filename = bodyPart.getFileName();
    
    // get the inputstream back
    InputStream bodyInputStream = bodyPart.getInputStream();
    
    // do what you need to do here....
    

    You could download JavaMail from Oracle Website which also has dependency on Java Activation Framework

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

Sidebar

Related Questions

I'm kind of new to php, and I'm trying to send a file through
I am trying to send data from an Android app to a PHP file
I am trying to output POST-data that I send from an android phone, however
I am trying to send a file through a java socket and receive it
I am trying to send an XML file that I created in my memorystream.
Im trying to send a Docx file via this form that I made, the
I'm trying to send a file on FTP server in my iPhone application. Everything
I'm trying to send a file between a client and a server in my
im trying to send a picture from a android device to a server and
I'm trying to send an image using a Java server to my Android device

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.