-1

I'm using FTPClient for uploading files on ftp server. first, I have a TempFile object(no matters what is this object), that have a getBytes() method. this method returns List of bytes:

List<Byte> byteList = tempFile.getBytes();
Byte[] byteObjects= new Byte[byteList.size()];
byte[] byteArray = new byte[byteList.size()];
byteObjects = byteList.toArray(byteObjects);
byteArray = ArrayUtils.toPrimitive(byteObjects);
InputStream stream = new ByteArrayInputStream(byteArray);
ftpClient.storeFile(file.getName()+"."+file.getExtension(), stream);

After executing the last line of code, a file creates successfully with expected name, but when i want to open that file, i see "The image test.jpg can not be displayed, because it contains error" in the browser. What is the problem?

hamed
  • 7,939
  • 15
  • 60
  • 114
  • Please add more code. What kind of object is `ftpClient` how you create it ... – Jens Jan 14 '15 at 06:15
  • That kind of error occures when upload binary file like Jpeg image file in ASCII mode. Does your "ftpClient.storeFile" set mode BINARY/ASCII according to the contents of file to be transffered. – Fumu 7 Jan 14 '15 at 06:28
  • Why, why, oh why are you converting bytes to `Byte[]` and back again to `byte[]`? – user207421 Jan 14 '15 at 06:50

1 Answers1

3

It sounds like you needs to set your ftp client to transmit as binary

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

As per the docs

If the current file type is ASCII, line separators in the file are transparently converted to the NETASCII format

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64