4

I have a program that reads a file from a webpage and writes it to a file. Most of the time this works good, but on occasions the file gets corrupted. I guess this has something to do with network issues. What could I do to make my code more stable?

String filename = "myfile.txt";
File file = new File(PROFilePath+"/"+filename);
//Open the connection
URL myCon = new URL("url to a page");
URLConnection uc = myCon.openConnection();
FileOutputStream outputStream = new FileOutputStream(file);

int read = 0;
byte[] bytes = new byte[1024];
while ((read = uc.getInputStream().read(bytes)) != -1) {
    outputStream.write(bytes, 0, read);
}
uc.getInputStream().close();
outputStream.close();
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
user727507
  • 275
  • 1
  • 2
  • 11
  • What kind of exception do you get? – Eypros Jul 04 '14 at 09:08
  • I dont get any exception but the file gets "corrupt"/incorrect codepage/characters. If just restarts my app it works. And it never fails locally only on the server. Therefore I guess its network related. Should I change byte[1024] to another value? Should I use HttpUrlConnection instead? – user727507 Jul 04 '14 at 09:18

1 Answers1

0

You are not using an explicit encoding for your copies, you are merely copying all bytes and write these bytes to a file which might later be read with a different decoding. An easy way to find this out is to compare the bytes of the document at the remote address and the copied file after you discover a "broken" file. However, with the information you provide is not detailed enough to provide you more specific help. Is there an example document are you having struggles with? Check out this related question and answer as well as this thread for a deeper discussion of this issue.

As to your suspicion: The connection should not simply lose bytes while you are reading from the remote address. This would be a very serious bug in the implementation as you connect via TCP (I guess the URL's protocol is HTTP) where lost packages are automatically compensated. And if the connection breaks, the connection should issue an exception instead of failing silently. I do not think that this is the source of your error.

Community
  • 1
  • 1
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192