Results 1 to 8 of 8
  1. #1
    dudejonne is offline Member
    Join Date
    Oct 2009
    Posts
    56
    Rep Power
    0

    Red face BufferedImage through FTP

    I can choose between one of these lines to send an image to my ftp server:

    ftp.stor(new File("img.jpg"));
    ftp.stor(new FileInputStream(new File("img.jpg")), "img.jpg");
    ftp.stor(someSocket.getInputStream(), "img.jpg");

    But all I have is a BufferedImage called bimg. How can I create a jpg out of that bimg or send the BufferedImage as jpg in one of those three lines?

    Note: I will use this application on my server so I cannot first save it and then send it.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See the API docs for ImageIO

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    Java Code:
      /**
       * puts an image to a ftp server. assumes ftp is already open / connected.
       * @param bimg the image
       * @param ftp that ftp client handle you're using there 
       * @param fileName the name to stor as
       */
      public void ftpPutImage(final BufferedImage bimg, FTP ftp, String fileName) {
          final PipedOutputStream pout = new PipedOutputStream();
          PipedInputStream pin = new PipedInputStream(pout);
          
          // i don't know if the image io will block or until the ftp stor is finished, so thats why  i run it in a thread here.
          Thread writerThread = new Thread(new Runnable() {
            public void run() {
              ImageIO.write(bimg, "png", pout);
            }
          });
          writerThread.start();
          
          ftp.stor(pin, fileName);
      }

  4. #4
    dudejonne is offline Member
    Join Date
    Oct 2009
    Posts
    56
    Rep Power
    0

    Default

    Hm, it gives me an error:
    image.java:206: local variable bimg is accessed from within inner class; needs t
    o be declared final
    ImageIO.write(bimg, "png", pout);
    ^

    And also, should I change file name to something like "image.jpg" for example?

  5. #5
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    yea, you see in my example, i did that
    ftpPutImage(final BufferedImage bimg

  6. #6
    dudejonne is offline Member
    Join Date
    Oct 2009
    Posts
    56
    Rep Power
    0

    Default

    I use this code for ftp:

    Java Code:
    SimpleFTP ftp = new SimpleFTP();
        
    ftp.connect("clipboard.site11.com", 21, "myusername", "mypass");
        
    ftp.bin();
        
    ftp.cwd("public_htm");
    
    String fileName = "image.jpg";
        
    ftpPutImage(bimg, ftp, fileName);
    
    ftp.disconnect();
    But I get this error:

    image.java:181: cannot find symbol
    symbol : class FTP
    location: class image
    public void ftpPutImage(final BufferedImage bimg, FTP ftp, String fileName) {
    ^
    1 error

  7. #7
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    right, i was just using FTP as example, this should be that SimpleFTP thing
    Java Code:
    public void ftpPutImage(final BufferedImage bimg, SimpleFTP ftp, String fileName)

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See, that's why you don't just simply provide code dumps. They usually expect to be able to just block copy cut-n-paste them and don't bother to even try to understand them. IOW they don't learn anything and you can settle back and expect to continue doing there work for them.

Similar Threads

  1. BufferedImage: Uneven rescale
    By legsmacgee in forum Java 2D
    Replies: 2
    Last Post: 04-24-2009, 11:48 AM
  2. Converting BufferedImage Into ImageIcon
    By hitmen in forum AWT / Swing
    Replies: 9
    Last Post: 03-25-2009, 09:10 AM
  3. Replies: 0
    Last Post: 07-02-2008, 09:06 PM
  4. Convert Byte [] to BufferedImage
    By Smily in forum Advanced Java
    Replies: 3
    Last Post: 04-28-2008, 06:54 PM
  5. BufferedImage to Byte
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-22-2008, 09:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •