• Post Reply Bookmark Topic Watch Topic
  • New Topic

FTP Uploading  RSS feed

 
Nick Kompas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have created an application that uploads images to an FTP server using a simple fileUpload() method. However, is there a way to upload images or imageIcons without having to write them to a file first?

For example I am manipulating some images and then wish them to send them to the FTP server straight from memory. Having to write them all to a file first, send them and delete them seems unnecessary trouble to me, but on the other side how can you use FTP (File Transfer Protocol) when there are no files.

the ftpUpload method supports only files and inputStreams.

Let me know if you know of a way to do this, but I myself assume that it's probably not so easy.

Thank you in advance!
Nick
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Nick.
Your task is quite simple. Java provides ByteArrayInputStream and ByteArrayOutputStream which can read and write to a byte array. ImageIO can read and write Image instances (and ImageIcon is backed by an Image). All you have to do is hook them up in the right order.
 
Nick Kompas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

thank you for the swift reply.

This is what I tried doing:

(buff is a BuggeredImage)

ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(buff, "jpg", out);
byte[] bytes = out.toByteArray();

ftpClient.put(new ByteArrayInputStream(bytes), filename);
(OR)
ftpClient.put(bytes, filename, false);

None of these seem to work. Any ideas?

The put method supports byte[] and inputStreams, but nothing is written to the ftp server. Howerver when I try to write a file on Disk, it works well.

Thank you again
Nick
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nick Kompas:
(buff is a BuggeredImage)


If your image is buggered, that's your problem

Seriously, what did you write to disk? The byte array or directly using ImageIO? Did you check the size of the byte array returned by out.toByteArray()? That code looks fine.
What API are you using for FTP? Does it have any debug options? Do you get an exception or error or is it failing silently? Maybe the FTP is going through, but not going to where you expect it?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!