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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:21:39+00:00 2026-05-25T12:21:39+00:00

this is regarding android instant messenger. im trying to send a file, and this

  • 0

this is regarding android instant messenger. im trying to send a file, and this is how i send it :

@Override
public boolean sendFile(String path,String ip, int port) {
// TODO Auto-generated method stub


try {

String[] str = ip.split("\\.");

byte[] IP = new byte[str.length];

for (int i = 0; i < str.length; i++) {

    IP[i] = (byte) Integer.parseInt(str[i]);


}
Socket socket = getSocket(InetAddress.getByAddress(IP), port);
if (socket == null) {
    Log.i("SO sendFILE","null");

    return false;
}

Log.i("SocketOP", "sendFILE-1");
File  f = new File(path);


BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );

FileInputStream fileIn = new FileInputStream(f);
Log.i("SocketOP", "sendFILE-2");




byte [] buffer  = new byte [1024];
    int bytesRead =0;
    while ((bytesRead = fileIn.read(buffer)) > 0) {
        out.write(buffer, 0, bytesRead);
        System.out.println("SO sendFile" + bytesRead);
    }
out.flush();
out.close();
fileIn.close();
Log.i("SocketOP", "sendFILE-3");






    } catch (IOException e) {
        return false;           
        //e.printStackTrace();
    }
    //  Toast.makeText(this, "Lvbvhhging...", Toast.LENGTH_SHORT).show();

    return true;        
}

this is how i receive connection and seperate text from file (i concatenate “text” to the output stream for the text)

public ReceiveConnection(Socket socket) 
        {
            this.clientSocket = socket;
            this.fileSocket=socket;
            SocketOperator.this.sockets.put(socket.getInetAddress(), socket);
        }

        @Override
        public void run() {
             try {
    //          PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                // PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
                    BufferedReader in = new BufferedReader(
                               new InputStreamReader(
                                        clientSocket.getInputStream()));
                     InputStream is=clientSocket.getInputStream();




                    String inputLine;
                    while ((inputLine = in.readLine()) != null)                  {



                         if (inputLine.contains("Text") == true)
                         {


                            appManager.messageReceived(inputLine);  
                    Log.i("SocketOP","text");}

                         else  if 
                        (inputLine.contains("Text") == false)
                        {

                            Log.i("SocketOP","filee");
                             appManager.fileReceived(is);

                         }



                    else{

                         clientSocket.shutdownInput();
                         clientSocket.shutdownOutput();
                         clientSocket.close();

                         fileSocket.shutdownInput();
                         fileSocket.shutdownOutput();
                         fileSocket.close();
                         SocketOperator.this.sockets.remove(clientSocket.getInetAddress());
                         SocketOperator.this.sockets.remove(fileSocket.getInetAddress());

                         Log.i("SocketOP", "CLOSING CONNECTION");
                     }                       
            }       

            } catch (IOException e) {
                Log.e("ReceiveConnection.run: when receiving connection ","");
        }           
        }   
    }

and this is how i finally receive the file in a service called imService :

public void fileReceived(InputStream is)
    throws FileNotFoundException, IOException {
    Log.i("IMSERVICE", "FILERECCC-1");


    if (is!= null) {
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fos = new FileOutputStream("/sdcard/chats/ffffff.txt");
            bos = new BufferedOutputStream(fos);
            byte[] aByte = new byte[1024];
            int bytesRead = 0;
            System.out.println("imService fileReceive" + bytesRead);

            while ((bytesRead = is.read(aByte)) != -1) {
                bos.write(aByte, 0, bytesRead);
                System.out.println("imService fileReceive" + bytesRead);

            }
            bos.flush();
            bos.close();
            Log.i("IMSERVICE", "FILERECCC-2");

        } catch (IOException ex) {
            // Do exception handling
        }
    }

right where i receive the file connection and forward the inputstream IS to the file receive method, i see that its getting the bytes but once filereceived is called it isnt receving any bytes.

it uses tcp/ip.

  • 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-25T12:21:39+00:00Added an answer on May 25, 2026 at 12:21 pm

    ok, try it on your method

    public void fileReceived(InputStream is)
     throws FileNotFoundException, IOException 
      {
        string result; 
       FileOutputStream fos = null;        
        fos = new FileOutputStream("/sdcard/chats/ffffff.txt"); 
    
       Log.i("IMSERVICE", "FILERECCC-1");       
     if (is!= null) 
    {  
    
     // result = convertStreamToString(is);              
     // result = result.replace("\n", ""); 
     // Log.e("InputStream output",result);
    
    
      //IOUtils.copy(is,fos);
    
       byte[] buffer = new byte[1024]; 
         int length;     
       while ((length = is.read(buffer)) > 0)
                 {     
           fos.write(buffer, 0, length);     
       }      
          // Close the streams     
           fos.flush();     
           fos.close();     
            is.close();     
    
    
    }     
    
    } 
    
      /*    public static String convertStreamToString(InputStream is) 
      throws Exception 
       {      
       BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        StringBuilder sb = new StringBuilder();     
         String line = null;     
          while ((line = reader.readLine()) != null) 
                  {         
                 sb.append(line + "\n");     
                  }     
                is.close();     
           return sb.toString(); 
           } 
       */
    

    EDIT: make other stuff in fileReceived method as comment.. just paste my suggested code.

    EDIT: its a IOUtils from apache use it..

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

Sidebar

Related Questions

Regarding Android >= 2.0.1. This speaks for Application type of project: The provided sample
I have a question this time around regarding the Android custom camera, NOT the
No matter what I seem to try and do regarding this listview and trying
I have a few questions regarding android source code download, repo / git. This
I'm back with another question regarding Android databases. I'm storing a simple string into
is it better to do this (regarding performance, not readability...): $('a.updateCartButton').click(function() { $('form[name=updateCartForm]').attr('action', $(this).attr('href')
This is regarding Visual Studio pro 2008 sp1, with resharper and testdriven installed. I've
(this is regarding the Ramaze.net framework) I ran into some really strange problems while
This is regarding AES algorithm. Suppose i have implemented a AES algorithm and encrypt
H Regarding this URL http://www.codeproject.com/KB/aspnet/FlashUpload.aspx User.Identity as System.Web.Security.FormsIdentity is always null, because the Identity

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.