Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
    Bookmark Topic Watch Topic
  • New Topic

Sending file over network through sockets using serializable object

Gayatri Shende
Posts: 11
  • Mark post as helpful
  • send pies
  • Report post to moderator
I am writing a code to transfer a file from server machine to client machine.
I am using Object protocol for communication. ie client sends a filerequest object , server recognizes it and send the response back in form of object again.
My problem is :

I read the file on server machine , in chunks of 1024 bytes.
I try to write an object to Objectoutputstram of socket.
For doing this, If I store the data read from file in any outputstream/Bufferedoutputstream etc then it throws exception : NOTserializable. Please guide me, what should I use to store the data from file and write it as object.

Here is my code:


ObjectOutputStream objOut = new ObjectOutputStream (socket.getoutputStream());


public Object processfile()

byte[] packet = new byte[1024];
int bytes_trans=0; //transfered bytes
int bytes_read;//read bytes
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream out;

try {
FileInputStream fin = new FileInputStream(filename);

while( (bytes_read =, 0,packet.length)) >= 0)

bytes_trans = bytes_trans + bytes_read;
System.out.println("Total bytes transferred :"+ bytes_trans);
System.out.println("Done! ");
//out = new ObjectOutputStream(bout);


//return out;

Nitesh Kant
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Report post to moderator
Kindly do not Cross post the same question multiple times.
This is the duplicate
    Bookmark Topic Watch Topic
  • New Topic