Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File Transfer in Java Using Sockets  RSS feed

 
Ankur Jaiswal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
This is Ankur and i wanted to transfer an entire file(the size keeps varying) to a remote server which is listenning to a port for an imcoming file. Also, the file has to be transferred as a whole and not as streams.
Kindly, suggest a way to do the same.
Ankur
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankur
I don't think it would be possible to transfer the file "as a whole" and not using streams.
One or the otherway streams will be used because its after all "network" which does only understand stream of bits
Now, probably, you can read the whole file and put it in Byte array and then transfer that byte array as an ObjectOutputStream or something once you open a connection to the server and then get the output stream from the communication socket. By this I mean,
Socket s;
byte[] fileBytes;
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.write(fileBytes);
oos.close();
assuming you have proper Socket created and read the file in fileBytes array.
Btw, why you want to avoid streams?
Regards
Maulin
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Closing this dulpicate post; followupshere.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!