Hey

I am having problems making a exact copy of a file. It seems that it changes the headers of the file.

I tried using the Apache fileutils copyfile method but it seems to change the header completely. The rest of the file looks pretty much OK.

I think it works but the problem is that the FTP transfer fails.......It generates the copy correctly I think


Any way to do it?

Dani AI

Generated

Short summary: the problem turned out not to be FileUtils but the FTP transfer mode. As discovered, sending the file in ASCII/text mode can change bytes at the start of the file (and elsewhere); switching the transfer to binary/raw mode prevents that. ’s NIO suggestion is useful when making local, byte-for-byte copies, but the corruption described here is typical of text-mode FTP translation. (See the FTP spec on ASCII vs image transfer.) (datatracker.ietf.org)

Practical checklist to guarantee an exact, byte-for-byte copy:

  • When using FTP, force binary (image) transfer before every upload/download. Some libraries and servers default to ASCII; with Apache Commons Net you must call setFileType(FTP.BINARY_FILE_TYPE) and be careful because certain calls (connect/login/list) can reset the type. (commons.apache.org)
  • If the copy is local, use raw byte streams or NIO (no readers/writers that do character conversions) so bytes are preserved.
  • Verify equality after transfer by comparing checksums (SHA‑256 or MD5) or a binary diff/hex dump. In Java you can compute a digest with MessageDigest (SHA‑256 is recommended). (docs.oracle.com)

Quick Java checksum example (compute before and after transfer and compare):

MessageDigest md = MessageDigest.getInstance("SHA-256");
try (InputStream in = Files.newInputStream(path)) {
  byte[] buf = new byte[8192]; int r;
  while ((r = in.read(buf)) > 0) md.update(buf, 0, r);
}
byte[] digest = md.digest();

Finally, if exact preservation is critical and FTP tooling is flaky, prefer SFTP/SCP or an HTTP(S) upload that treats the payload as binary. Also repeatedly set binary mode immediately after connect/login to avoid accidental resets. (commons.apache.org)

Recommended Answers

All 3 Replies

See what James has posted, check the java tutorials.

Typical roseindia - hardly an expert solution, and more important to this thread, does nothing about the file headers.
riahc3 - have you had a look at the NIO (new I/O) classes in Java 7? They have lots of new stuff for copying files and handing file attributes - even OS-dependent headers.
http://docs.oracle.com/javase/tutorial/essential/io/fileio.html

See especially Files.copy(source, target, COPY_ATTRIBUTES);

I'm using Java 6 but I solved it: had to set the file type to binary instead of the default of ASCII. My fault.

commented: In every thread of yours, you find the solution at the very end yourself!. :P +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.