Tunneling a .gif or.jpg to an HttpServletResponse

We are trying to make a tunneling servlet that will retrieve URLs and pass them back to a user via an HttpServletResponse. Here's the process.
User calls my TunnelingServlet, passing it a parameter called "url" with a value of something like "www.yahoo.com".
Our servlet opens an HttpURLConnection to that URL and retrieves the data.
Our servlet then adds the response from the HttpURLConnection to the HttpServletResponse object.
I will attach the servlet we created. We are having no problem with any mime types of "text/*" or "application/x-javascript" since I'm using a PrintWriter and just writing all the chars from the HttpURLConnection's OutputStream.
However, for types if "image/*" (i.e. gifs, jpegs, etc.), I can't figure out how to tunnel that data back to the user's browser to be rendered correctly. The image gets rendered either incorrectly or as a missing image. I think it has something to do with how I read the HttpURLConnection's OutputStream and write those ints to the HttpServletResponse's ServletOutputStream.
I noticed that the HttpURLConnection returns data as a sun.awt.image.URLImageSource object...but I can't find ANY reference to that API on the Internet (not even on java.sun.com.)
Can you please help me to successfully pull image files (and other mime types such as PDF, DOC, etc.) through my tunnel servlet?
Thanks so much,

Why wouldn't you just use getInputStream for any kind of data

Similar Messages

  • Creating a .gif or .jpg File

    I have written an eBook and am sending it for eBook publication and sale.
    One of the items required in the process before uploading the eBook is to provide:
    A. An image of the book cover. That image is now in Pages format and it contains text and a line art image. I need to convert that cover page to a .gif or .jpg format. How can I do this?
    B. The process also requires me to create 'Banners' of varying size. How do I create a Banner? All the material [text and art] are contained in the existing Pages document.
    Thank you,
    Leo
    <Edited by Moderator>

    Leo Gendernalik1 wrote:
    I have written an eBook and am sending it for eBook publication and sale.
    One of the items required in the process before uploading the eBook is to provide:
    A. An image of the book cover. That image is now in Pages format and it contains text and a line art image. I need to convert that cover page to a .gif or .jpg format. How can I do this?
    As far as I know, the only way to get a gif or jpg would be to make a screenshot.
    Maybe a PDF may fit your needs. If it does use the Print to PDF tool and print the 1st page.
    B. The process also requires me to create 'Banners' of varying size. How do I create a Banner? All the material [text and art] are contained in the existing Pages document.
    You may open a blank Numbers document and remove every table.
    You will get a huge blank surface which may be more than one meter in width.
    Copy objects from Pages and paste then on the Numbers sheet.
    Yvan KOENIG (from FRANCE samedi 14 juin 2008 21:25:00)

  • About gif and jpg

    who can give me the website about gif and jpg format?
    thanks guy.

    This is a crosspost.
    Chances are that you won't get a resonable response when you do that. Try reading How to Ask Questions the Smart Way. That will give you plenty of pointers on getting reasonable responses.
    The response in your other post is a literal answer to you question, google is your friend.
    For a direct answer, here's the the Jpeg FAQ. If what you are actualling requesting is "how do I use images in Java" take a look at the ImageIcon class, the javax.imageio package and search this forum for examples. You could also check the How to use Icons in the Java Tutorial.

  • I have a pdf file 50 pages and want add to 1st page a logo (icon png or gif or jpg), well I have cs4

    I have a pdf file 50 pages and want add to 1st page a logo (icon png or gif or jpg), well I have cs4 master collection... how do it?

    what is "pinch"? what cs4 program(s)
    to use for this?

  • How can I invalidate everything except .gif and .jpg

    Hi
    I need to invalidate every page except the static objects as gif jpg. Can I create a rule that invalidates everything except gif and jpg.
    Thanks

    currently webcache invalidation does not have the expressive power to describe "anything but".
    however, you can give a list of rules in single invalidation message, each matching one type of pages, such as under particular directory, and with fixed suffix. and the whole list of criteria effectively achieves "anything but".

  • GIF and JPG become damaged

    I have a weird problem with some GIF and JPG images which I
    use in a RoboHelp project. They appear to get damaged, but I nor
    anyone else changes them. The bottom part becomes scrambled or
    entirely black. The strange thing is that when I put the project on
    my local hard disk, nothing happens, but when they are on the
    server (with RAID disk sytem), then trouble begins.
    Until now, nobody else who's wordking on the server reported
    damaged files, so it seems that I'm the only one so far.
    The trouble began on Sept. 11. Some projects I worked on
    since that day have problem files. Most projects are RoboHelp, but
    there's also a Word-project involved with a damaged JPG-file.
    Anyone?

    Hi Peter
    I've indeed already experienced that some RoboHelp projects,
    mainly large onces, are better run on the local hard disk.
    But this is a different problem I think. The project is run
    locally, which goes well, but when I copy it to the server disk for
    backup reasons, then some GIF-files get damaged after a while,
    while nobody opens the project on the server. When I copy the same
    files to another server's disk, then the files do not get damaged.
    I also have a Word project that has a JPG file that has got
    damaged.

  • Some GIFs and JPGs appearing with yellow "wash" when loading in Safari

    Hi,
    I've flicked through forums, google, and called my top graphics people but nobody can seem to help, or has ever even heard of, my issue.
    Here it is in a nutshell:
    Some GIFs and JPGs appear with a yellow "wash" when being loaded in Safari. I can confirm this is happening on 3 different Macs, and even on a PC running Safari.
    Safari 3.1.1 on Windows XP
    Safari 3.1.2 on Mac OSX 10.5.5
    Here's some examples of it occurring:
    http://staging.tomatosource.com.au/test/bad.jpg
    http://staging.tomatosource.com.au/test/bad.gif
    You might need to refresh the page in Safari a couple of times to see the flicker but it's there.
    I've tried:
    - resaving images with and without "progressive" and "interlaced"
    - a variety of different colour palettes
    - taking a screenshot of the image, putting back into Photoshop and resaving
    Has anybody seen this issue before? Unfortunately I can't go live with this site until I fix this very strange error.
    Any comments would be greatly appreciated!
    Cheers,
    Jase.

    Totally independently from your current problem, Safari is cr@p. Dump it.
    All Apple applications, from the Finder through Preview and iPhoto to Safari, assign Monitor RGB to ALL untagged images, even if they were created or saved in sRGB.
    The only current properly color-managed web browser is Firefox 3.0.1. Just keep in mind that you have to enable color management deep inside the guts of Firefox.
    See this existing thread in the Adobe Photography forum:
    Ramón G Castañeda, "Beware of using Adobe Web Galleries" #49, 14 Sep 2008 4:52 pm

  • How to get animated gifs or jpgs to work

    thanks for reading!
    how do you get animated gifs or jpgs to work in iweb? i have a few that i made and worked when i had the old site up. but under iweb they wont, if you go to
    my site use my username as the domain. www.myusername.com you will see a open 24hr sign that s animated but wont work in iweb, also thier is a little gif of a radio at the buttom the dials move and the numbers change, under i web they dont work why?
    thank for reading this

    Not sure exactly what is happening over there on your end. Animated GIFs can be drag and dropped into iWeb, no problem. Inside iWeb when you drop them onto your page, YOU WILL NOT SEE THEM ANIMATE. You may even see them show up with a QT controller bar underneath them. But don't worry....once you publish your page, the GIF will be animated and the controller bar will disappear. Just try it!

  • How can I load a gif or jpg in a JFrame (background)

    I wanna load a gif or jpg image as a background at a JFrame. How can do this?
    Thanks

    I have tried that but my image always paints over the swing components. Could you give more a code snippet?

  • Using imageio to convert .gif to .jpg gives bad image

    Hi,
    I'm looking at the 1.4 javax.imageio API, and wrote a naive
    program to convert images from one format to another.
    It will read .gif, .jpg or .png and correctly write .gif and .png
    images, but when I try to write a .jpg from a .gif or .png
    the result is very black-looking - actually most of the colour
    is there but changed to a very dark level and red switched to blue
    for example. Presumably there is a reason that this doesn't work,
    or is it a bug? I would've though an exception would be thrown
    if the image types selected were incompatible.
    Also, how does one specify the JPEG quality setting?
    Thanks for any clues,
    Ed
    import javax.imageio.*;
    import java.awt.*;
    import java.io.*;
    import java.awt.image.*;
    public class ImageTest
    public static void main(String[] argv)
    try
    File f1 = new File(argv[0]);
    BufferedImage bi = ImageIO.read(f1);
    System.out.println("Read "+f1+" OK.");
    System.out.println("Image="+bi);
    File f2 = new File(argv[1]);
    String extn = f2.getName();
    extn = extn.substring(extn.lastIndexOf('.')+1);
    ImageIO.write(bi, extn, f2);
    System.out.println("Wrote "+f2+" OK format="+extn);
    catch (Exception ex)
    ex.printStackTrace();

    Interesting, the program works correctly on Windows 2000
    and Solaris Sparc; The .jpg file creating problem only
    occurs on my Solaris x86 machine.
    Ed

  • Gif and JPG save for web file sizes in CS2 and CS3

    Gif 64 dittered default CS2=2.63KB CS3=4.83KB
    JPG CS2Low=1.42KB CS3=4.03KB
    JPG CS2Medium=1.90KB CS3=4.51KB
    JPG CS2High=3.38KB CS3=5.99KB
    Why? Is there any solution besides keep using CS2 for web?
    Thank's in advance.
    regards
    Fernando Bergamaschi

    Each of the three JPG files increases by the same amount -- 2.61 Kbytes. That sure sounds like some common header block is being added in CS3. Did you check the "Optimize menu" button next to "Preset" just to make double-sure "Include XMP" isn't enabled? Or some other setting like that? Or check both images in Bridge to see if something is being added.

  • Gif to jpg help needed

    Hi,
    I have to convert a gif image to a jpg image but when I convert, the image size increases from 22 kbytes to 50 kbytes. is there a way I could convert the image without increasing the size in java?
    can somebody help me with this. I would be very glad if somebody could help me with this.
    I am using Jmagick for this.
    MagickImage.setFormat("jpg");
    I don't need it to be done using Jmagick, but if there is a way I would definitely like to know.
    Thanks,
    Samanth.

    The size of the JPG depends on the content of the image itself; in most cases, the JPG is smaller than the GIF, even with high quality images. The size of the JPG can be controlled by how much information you are willing to lose; this is controlled by a parameter called Quality.
    ImageMagick (and therefore JMagick, since this is only the Java interface to ImageMagick) has a parameter for setting the quality; in command-line tools, it is the parameter -quality; in programming, I think it is MagickSetCompressionQuality(); check the documentation. Quality ranges from 0 (lowest, barely recognizable) to 100 (highest)
    Without JMagick, you can use the built-in ImageIO
    //Read the file to a BufferedImage
    BufferedImage image = ImageIO.read(new File("c:/temp/image.gif"));
    Once you have the BufferedImage, you can write the image as a JPG. You will need to create a File object for the destination image. When calling the write() method, specify the type string as "jpg" (other formats are supported).
    ImageIO.write(image, "jpg", new File("c:/temp/image.jpg"));

  • Saving a .gif  or .jpg image

    hello,
    in the code sample below you can see how I try to make a "thumbnail"-like image of a gif, jpg.
    Afterwards they are showed in a JTable.
    But this takes quite a lot of time to show the, because they all needs to be resized on the fly each time.
    Can I opent he picture, resize it, save it as another one, and use this new file to show as a thumbnail?
    code:
         //setIcon
         //Zet de foto in de picPanel
         public void setIcon(File file){
              try{
                   FileReader test = new FileReader(file);
                   image = file;
                   test.close();
                   ImageIcon icon = new ImageIcon (image.getPath());
                   if (icon.getIconWidth() > icon.getIconHeight())
                        icon.setImage(icon.getImage().getScaledInstance(150,-1,Image.SCALE_FAST));
                   else
                        icon.setImage(icon.getImage().getScaledInstance(-1,150,Image.SCALE_FAST));
                   foto.setIcon(icon);
              catch(Exception e){
                   Image = new File("img/wrong.gif");
                   ImageIcon icon = new ImageIcon (image.getPath());
                   if (icon.getIconWidth() > icon.getIconHeight())
                        icon.setImage(icon.getImage().getScaledInstance(150,-1,Image.SCALE_FAST));
                   else
                        icon.setImage(icon.getImage().getScaledInstance(-1,150,Image.SCALE_FAST));
                   foto.setIcon(icon);
              changed();
         }

    found an method saveComponentAsImage in one of the topics, and changed it a bit, so ti would be usefull in my current project.
    //saveIconAsImage
        //slaat de gegeven icon op met gegeven max lengte/breedte
        public static void saveIconAsImage(ImageIcon icon, String name,int width) {
              if (icon.getIconWidth() > icon.getIconHeight())
                             icon.setImage(icon.getImage().getScaledInstance(width,-1,Image.SCALE_REPLICATE));
              else
                             icon.setImage(icon.getImage().getScaledInstance(-1,width,Image.SCALE_REPLICATE));          
              JLabel c = new JLabel(icon);               
            int w = c.getPreferredSize().width;
            int h = c.getPreferredSize().height;
            // use TYPE_INT_ARGB if you want alpha (transparency)
            BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            // don't do this if you want ugly graphics ;)
            HashMap hints = new HashMap();
            hints.put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            hints.put(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
            Graphics g = image.getGraphics();
            ((Graphics2D)g).addRenderingHints(hints);
            // make the component believe its visible and do its layout
            c.addNotify();
            c.setSize(w,h);
            c.setVisible(true);
            c.validate();
            // draw the graphics
            c.print(g);
            // write it out
            try {
                ImageIO.write(image, "jpg", new File("pic/"+name+"_"+width+".jpg"));
            } catch (IOException ioe) {
                System.out.println(ioe.getMessage());
            // cleanup
            g.dispose();
        }

  • GIF to JPG problem - Orange tint

    I have tried numerous attempts to reliably create a JPG from a GIF ( I need JPG not PNG).
    The code I have does work MOST times but ... for some GIFs an orange tint is produced.
    My environment is:
    Win XP
    Java 1.4.2_03
    JAI 1.1.2 --- today's version
    ImageIO --- today's version
    Sample images which fail are at:
    http://www.franchisebusiness.com.au/tst/product.10010.large.gif
    http://www.franchisebusiness.com.au/tst/product.FCALST_233.medium.gif
    http://www.franchisebusiness.com.au/tst/product.FCALST_532.large.gif
    http://www.franchisebusiness.com.au/tst/product.FCALST_595.large.gif
    http://www.franchisebusiness.com.au/tst/product.FCALST_595.medium.gif
    http://www.franchisebusiness.com.au/tst/product.FCALST_87.medium.GIF
    http://www.franchisebusiness.com.au/tst/product.FCALST_87.small.GIF
    My test code is as follows:
    public class UtilImage
         static Logger logger = Logger.getLogger(UtilImage.class);
         static String module= UtilImage.class.toString();
         public static void main(String[] args) throws IOException
              String baseDir= "f:/ccdev2/ofbiz/ecommerce/images/catalog/tst";
              File dir= new File(baseDir);
              String[] readerMIMETypes= ImageIO.getReaderMIMETypes();
              String[] writerMIMETypes= ImageIO.getWriterMIMETypes();
              for (int i= 0; i < readerMIMETypes.length; i++)
                   System.out.println("Reader: "+readerMIMETypes);
              for (int i= 0; i < writerMIMETypes.length; i++)
                   System.out.println("Writer: "+writerMIMETypes[i]);
              File[] fileList= dir.listFiles();
              for (int i= 0; (i < fileList.length) && (fileList[i].isFile()); i++)
         int idx;
         idx= fileList[i].getName().lastIndexOf(".gif");
         if ( idx < 0 ) idx= fileList[i].getName().lastIndexOf(".GIF");
    if ( idx == fileList[i].getName().length()-4 )
                        System.out.println("file= "+fileList[i].getName());
         String base= (fileList[i].getName()).substring(0, fileList[i].getName().length()-4 );
    //                    RenderedOp src = JAI.create("fileload", fileList[i].getAbsolutePath());
    //                    FileOutputStream stream= new FileOutputStream(baseDir+"/res/"+base+".jai.jpg");
    //                    JAI.create("encode", src, stream, "JPEG", null);
    //                    JAI.create("filestore", src, baseDir+"/res/"+base+".jai.jpg", "JPEG", null);
                        BufferedImage bi= ImageIO.read(fileList[i]);
                        ImageIO.write(bi, "JPEG", new File(baseDir+"/res/"+base+".iio.jpeg"));
                        ImageIO.write(bi, "JPG", new File(baseDir+"/res/"+base+".iio.jpg"));
                        ImageIO.write(bi, "PNG", new File(baseDir+"/res/"+base+".iio.png"));
                        bi.flush();
                        bi= null;
                        bi= ImageIO.read(new File(baseDir+"/res/"+base+".iio.png"));
                        ImageIO.write(bi, "JPEG", new File(baseDir+"/res/"+base+".iio2.jpg"));
                        bi= null;
              System.out.println("Done!");

    By the way, the reason I converted to TYPE_INT_RGB was that your GIFs (at least the first one) had an
    IndexColorModel with a transparent color (transparency BITMASK) -- this means you color model
    has four channels: red, green, blue and alpha (even if you happen not to have transparent pixels in the
    image). While jpeg images are allowed to have 4 channels, most other applications mishandle them
    when they do -- perhaps the source of the tinting.
    It just occurred to me that perhaps a more effficient way to get rid of the alpha channel is to create an
    OPAQUE IndexColorModel out of the given one and reuse the raster:
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    import javax.swing.*;
    public class GifToJpeg{
        public static void main(String[] args) throws IOException {
            URL[] urls = {
                new URL("http://www.franchisebusiness.com.au/tst/product.10010.large.gif"),
                new URL("http://www.franchisebusiness.com.au/tst/product.FCALST_532.large.gif "),
                new URL("http://www.franchisebusiness.com.au/tst/product.FCALST_595.large.gif "),
            JPanel cp = new JPanel(new GridLayout(0,2));
            for(int i=0; i<urls.length; ++i)
                process(cp, urls, i);
    JFrame f = new JFrame("GifToJpeg");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new JScrollPane(cp));
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
    static void process(Container cp, URL url, int offset) throws IOException {
    BufferedImage image1 = ImageIO.read(url);
    addImage(cp, image1, "original image");
    BufferedImage image2 = convertImage(image1);
    File file = new File("junk" + offset + ".jpeg");
    file.delete();
    ImageIO.write(image2, "jpeg", file);
    BufferedImage image3 = ImageIO.read(file);
    addImage(cp, image3, "jpeg file image");
    static void addImage(Container cp, BufferedImage image, String title) {
    JLabel label = new JLabel(new ImageIcon(image));
    label.setBorder(BorderFactory.createTitledBorder(title));
    cp.add(label);
    static BufferedImage convertImage(BufferedImage image) {
    IndexColorModel icm = (IndexColorModel) image.getColorModel();
    int[] cmap = new int[icm.getMapSize()];
    icm.getRGBs(cmap);
    IndexColorModel opaqueIcm = new IndexColorModel(8, cmap.length, cmap, 0, false, -1, DataBuffer.TYPE_BYTE);
    return new BufferedImage(opaqueIcm, image.getRaster(), false, null);

  • GIF to JPG always saving as CMYK

    Hi,
    I'm having very little luck trying to save a transparent GIF to a JPG.
    Try executing the code below to pull the GIF image im testing with
    and saving as a JPG. For me this image always ends up as a
    CMYK JPG which my browser can't open but Photoshop can.
    Any help would be appreciated.
    Thanks
    Kallen
    public static void main(String args[]) throws Exception
            BufferedImage  image = ImageIO.read(new URL("http://mry-img002.catalogcity.com/lotr.gif").openStream());
            ImageIO.write(image,"jpeg",new FileOutputStream("C:\\lotr.jpg"));
    }

    Thanks for all your help, but I eventually found the problem. You need to ColorConvert the BufferedImage that is read in to be RGB like so.
    BufferedImage dst = new BufferedImage(src.getWidth(),src.getHeight(),BufferedImage.TYPE_INT_RGB);
    ColorConvertOp xformOp=new ColorConvertOp(null);
    xformOp.filter(src,dst);

Maybe you are looking for

  • Can no longer access my podcasts

    Until about three or four days ago, I had no trouble listening to my podcasts. Now, however, whenever I click on the Music icon and then choose "Podcasts", it closes out and goes back to my home screen. I've tried doing a reset by holding down the po

  • CAN'T INSTALL A PROGRAM CALLED TOUCH DVS

    WHEN I PRESS THE INSTALL ICON I GET THIS MESSAGE: (com.apple.installer.pagecontroller error -1.) HOW CAN I SOLVE THIS PROBLEM PLESE????? THANKS

  • Want to make a trigger arrow icon of graphic chat

    How to make a trigger arrow icon of graphic chat as attached. When drag it up and down, the value of trigger voltage level changed follow it. Any suggestion, thanks.

  • Associating a Navigation Item with a View Controller in a XIB

    According to Xcode: The 'navigationItem' outlet of a UIViewController has been deprecated. Navigation items are required prior to loading a view controller's view. Instead of an outlet, consider embedding the navigation item in the view controller. W

  • Port 995

    Hi, I have just set up web hosting and I am attempting to set-up my email so that it can be received and sent from Mail. I have been in constant correspondence with their support desk, but they don't seem to be able to assist any further. They tell m