1

I'm working on FTP-Client tool for connection to ftp.

At this time, I need to upload to ftp via this tool. According to this post and first answer, it's possible to use FileInputStream for saving the files. But i want to store file as a byte array, not FileInputStream.

Is there any way to do that?

Community
  • 1
  • 1
hamed
  • 7,939
  • 15
  • 60
  • 114

2 Answers2

3

Just use a ByteArrayInputStream to wrap the byte array which containes the data you want to upload, and then use this stream in place of the FileInputStream. E.g. something like:

byte[] mydata = <get your data>;
InputStream stream = new ByteArrayInputStream(mydata);
ftpClient.storeFile("remoteName", stream);
stream.close(); // Not strictly needed for ByteArrayInputStream
BarrySW19
  • 3,759
  • 12
  • 26
-2

You can user ByteArrayOutputStream to store byte and can get the byte array.

 InputStream baos = new ByteArrayOutputStream();
 // write bytes here
 baos.write(bytes)

//to get bytes 

byte[] bArr = baos.toByteArray();
Vijay
  • 1,024
  • 6
  • 18