• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File copy is not performing correctly

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am uploading a file from client to server using sockets and below is the code for that.

BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(file));
int data = in.read();
while(data != -1)
{
fos.write(data);
data = in.read();
}
fos.flush();

Where in refers to socket inputstream. After this I am closing the in and fos stream in the finally block.

The problem is that at the server file is not getting copied completely. For eg if the file size is 5MB, only 4520KB is getting copied and remaining 80KB is lost. As a result I am not been able to open the files. This cannot be reproduced frequently, but it happens.

Does anybody know what is the reason behind this.

Thanks,
Sumanth
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any problems in this reading code. Are you sure that the server provides the correct data?
 
Sumanth Prabhakar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

The code attached in the previous post is on the server side. The code on the client side which puts file data on the socket stream is

fin = new BufferedInputStream(new FileInputStream(file));
out = new BufferedOutputStream(socket.getOutputStream());
int data = fin.read();
System.out.println("File uploading.");
while(data != -1)
{
out.write(data);
data = fin.read();
}
out.flush();

I didn't understand the your question. What data are you taking about? Can you please explain it.

Thanks,
Sumanth
 
Sumanth Prabhakar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sumanth Prabhakar wrote:Hi,

The code attached in the previous post is on the server side. The code on the client side which puts file data on the socket stream is

fin = new BufferedInputStream(new FileInputStream(file));
out = new BufferedOutputStream(socket.getOutputStream());
int data = fin.read();
System.out.println("File uploading.");
while(data != -1)
{
out.write(data);
data = fin.read();
}
out.flush();

I didn't understand the your question. What data are you talking about? Can you please explain it.

Hi all I need help in solving the above problem. Please help me out.

Thanks,
Sumanth

 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic