Thanks
I have a java struts text which gives an example how to display images in a jsp.
They accessed the image files on the server and created a byte[]/OutputStream from these. And they used this same Get servlet for gif and jpg files.
Should I be using the OutputStream rather than creating a bufferedimage?
try{
FileInputStream fis = new FileInputStream();
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] bytes = new byte[bis.available()];
response.setContentType(co
OutputStream os = response.getOutputStream()
bis.read(bytes);
os.write(bytes);
}
catch(IOException e){
}
Main Topics
Browse All Topics





by: rrz@871311Posted on 2006-09-12 at 15:34:00ID: 17507498
There is a lot of code already on this site. Search for ImageIO class. For gif I used code from acme.com but in Java 6 I think( ? ) this will no longer be necessary. You can run this code just to see the output. ---------- ---------- ---------- ---------- ---------- -------- (); (); s: " + ); s: " + )); ---------- ---------- ---------- ---------- ---------- ---------- ----- ; etRealPath ("/Images/ smile.gif" )); mage/gif") ;
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----- ---------- ---------- ---------- ---------- ---------- ---- javax.imag eio.stream .*" %><% ); mage/jpg") ; ormatName( "jpg"); ); tream(resp onse.getOu tputStream ()); ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------ ---------- ---------- ---------- ---------- ---------- ---------- ---- ageio.Imag eIO"%><% (new File(application.getRealPa th("/") + "MyPic.jpg")),"jpg",respon se.getOutp utStream() );%> ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------
--------------------------
import javax.imageio.*;
import java.util.Arrays;
public class GetFormats {
public static void main(String args[]) {
String readFormats[] = ImageIO.getReaderMIMETypes
String writeFormats[] = ImageIO.getWriterMIMETypes
System.out.println("Reader
Arrays.asList(readFormats)
System.out.println("Writer
Arrays.asList(writeFormats
}
}
--------------------------
package tester. import Acme.JPM.Encoders.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.imageio.*;
public class Img2 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException
{
ServletOutputStream sos = response.getOutputStream()
//The "Images" folder is presumed to be in your context's root folder.
File imgFile = new File(getServletContext().g
BufferedImage bi = ImageIO.read(imgFile);
response.setContentType("i
GifEncoder encoder = new GifEncoder(bi,sos);
encoder.encode();
}
}
--------------------------
For jpeg I had the following JSP handy for testing.
--------------------------
<%@ page import="java.awt.*, java.awt.image.*, java.util.*, java.io.*,javax.imageio.*,
int width=200, height=200;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB
Graphics g = bi.getGraphics();
g.setColor(Color.BLUE);
g.fillRect(0, 0, width, height);
g.dispose();
response.setContentType("i
Iterator writers = ImageIO.getImageWritersByF
ImageWriter writer = (ImageWriter)writers.next(
ImageOutputStream ios = ImageIO.createImageOutputS
writer.setOutput(ios);
writer.write(bi);
ios.close();
if(out!=null)return; %>
--------------------------
Here is a another JSP. You can rewrite to servlet.
--------------------------
<%@ page import="java.io.*,javax.im
ImageIO.write(ImageIO.read
--------------------------
>Should I be thinking of writing a separate servlet for each file type?
You could do it either way. You will have to tell us more of your requirements. rrz