Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - display image on jsp file

my model store image described with file name (as String) and data (as byte array). I use Hibernate and here's my model:

@Entity
public class Image {

    private Long id;
    private String name;
    private byte[] data;

    @Id
    @GeneratedValue
    @Column(name = "IMAGE_ID")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(nullable = false, length = 100)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Lob
    @Column(nullable = false)
    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }
}

But I want to display my stored image, on web site like:

<img src="${image.data}" alt="car_image"/>

How could I do that?

Should I write controller that serve requests for images?

Any code examples?


UPDATE

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>

<bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/configs/tiles.xml</value>
        </list>
    </property>
</bean>
like image 742
bontade Avatar asked Apr 08 '12 20:04

bontade


People also ask

How to display image in JSP using Jstl?

You can display image by keeping image name in request scope and fetch it by its name JSTL. String imageName = "test. jpg"; request.

Where to put images in spring mvc?

It is generally preferred to hide the . css/ . js files from direct public access by placing them into the main/java/webapp folder instead of the WEB-INF.


2 Answers

You cannot do it like this. Your image must be exposed somehow via normal URL. In Spring MVC create a controller that returns an image (raw data) under particular URL:

@RequestMapping(value = "/imageController/{imageId}")
@ResponseBody
public byte[] helloWorld(@PathVariable long imageId)  {
  Image image = //obtain Image instance by id somehow from DAO/Hibernate
  return image.getData();
}

Now useit in your JSP page. This is how HTTP/HTML work:

<img src="/yourApp/imageController/42.png" alt="car_image"/>

In Spring MVC before 3.1 you might need to do a little bit more coding on controller side. But the principle is the same.

like image 126
Tomasz Nurkiewicz Avatar answered Oct 14 '22 01:10

Tomasz Nurkiewicz


File file = new File("home/user/test.jpg");
FileInputStream fis=new FileInputStream(file);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int b;
byte[] buffer = new byte[1024];
while((b=fis.read(buffer))!=-1){
   bos.write(buffer,0,b);
}
byte[] fileBytes=bos.toByteArray();
fis.close();
bos.close();


byte[] encoded=Base64.encodeBase64(fileBytes);
String encodedString = new String(encoded);

ModelMap map = new ModelMap();
map.put("image", encodedString);

Now use it in your JSP page following as

<img src="data:image/jpeg;base64,${image}" alt="..." width="200" height="200">`
like image 35
govind Avatar answered Oct 14 '22 00:10

govind