• Post Reply Bookmark Topic Watch Topic
  • New Topic

File copy is not performing correctly  RSS feed

 
Sumanth Prabhakar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!