0

This is my Java code in eclipse:

   @GET
    @Path("request=getTile")
    @Produces({"image/png"})
    public BufferedImage getTileGET(@QueryParam("id_photo")String id_photo,
                          @QueryParam("x")String x, 
                          @QueryParam("y")String y, 
                          @QueryParam("zoom")String zoom) throws SQLException, IOException  {

        BufferedImage BI = null;
        connection dbs = new connection();
        Statement statement = null;
        Connection con = dbs.getConnection();
        PreparedStatement ps = null;

        try {
            ps = con.prepareStatement("SELECT image from tiles where id_photo = '"+id_photo+"' "
                    + "and x ='"+x+"' and y ='"+y+"' and zoom = '"+zoom+"';");

            ResultSet rs = ps.executeQuery();

                while (rs.next()) {
                    InputStream input = rs.getBinaryStream("image");
                    BI=ImageIO.read(input); 
                    input.close();  
                }

        }     
        catch (SQLException e )
        {
            e.printStackTrace();
            System.out.println("Bad data");
        }
        finally {
            if (statement != null) {
                statement.close();
            }

            if (con != null) {
                con.close();
            }
        }       
        return BI;
        }

My problem is that the image is not displayed in the Browser ( Google Chrome ). My image in database is in bytea. What am I doing wrong? Thank you for any help.

Satyam Koyani
  • 4,236
  • 2
  • 22
  • 48
wojas.
  • 15
  • 6

1 Answers1

0

You cannot simply return an image like that. See this example: https://stackoverflow.com/a/9204824/1256583

@GET
@Path("request=getTile")
@Produces({"image/png"})
public Response getTileGET(...) {

    BufferedImage image = ...;

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", baos);
    byte[] imageData = baos.toByteArray();

    // uncomment line below to send non-streamed
    // return Response.ok(imageData).build();

    // uncomment line below to send streamed
    // return Response.ok(new ByteArrayInputStream(imageData)).build();
Community
  • 1
  • 1
darijan
  • 9,725
  • 25
  • 38