2

I store images in my MySql database as blobs using

preparedStatement.setBinaryStream(1, photo);

where photo is an InputStream.

They are correctly inserted (displayed normally in MySql workbench).

I retrieve them using

resultSet.getBytes("Photo");

and try to display them on a .jsp page like this:

<%
List<byte[]> pics = PictureTable.getConcertPictures(concertBean.getId());
%>
<%
for (byte[] p : pics) {
String encoded = ImageHelper.encodeImage(p);
String source = "data:image/jpg;base64,";
source = source.concat(encoded.toString());
%>
<img src=<%=source%>>
<%
}
%>

PictureTable.java:

public static List<byte[]> getConcertPictures(int id) {
        List<byte[]> pictures = new ArrayList<byte[]>();

        try (Connection connection = ConnectionPool.getConnectionPool().checkOut()) {

            PreparedStatement ps = connection.prepareStatement("SELECT * FROM concertphototable WHERE ConcertId=?");

            ps.setInt(1, id);

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                pictures.add(rs.getBytes("Photo"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return pictures;
    }

ImageHelper.java:

public class ImageHelper {
    public static String encodeImage(byte[] img) {
        return Base64.encode(img); //com.sun.org.apache.xerces.internal.impl.dv.util.Base64
    }

but the images are not displayed.

Edit: For the sake of completeness, this is how I store the images:

<form
action="GalleryController?action=add_concert_picture&concertID=<%=concertBean.getId()%>"
method="post" class="panel panel-success"
enctype="multipart/form-data">
<div class="col-md-6 form-group">
<input type='file' name="image" class="form-control" />
</div>
<button type="submit" class="btn">Submit</button>
</form>

controller: (@MultipartConfig)

Part filePart = request.getPart("image");
InputStream fileContent = filePart.getInputStream();
PictureTable.insertConcertPicture(cid, user.getUser().getId(), fileContent);
6
  • Please show us the code for what is happening on the JSP line starting with List<byte[]> pics. Also, which library are you using for base 64 conversion? Commented Dec 24, 2017 at 13:47
  • @TimBiegeleisen I've added more details. Commented Dec 24, 2017 at 13:52
  • 1
    Nothing seems obviously wrong to me. My advice: Step through your result set in debug mode, grab a byte array, and manually try to marshal into a base 64 encoded image string by hand. Then, try to load that in a dummy web page. Something is wrong, but it is not clear what just by looking at your code (which looks correct). Commented Dec 24, 2017 at 14:05
  • @TimBiegeleisen I think there might be something wrong with my JSP, when I inspect element the <img> tags aren't displayed at all, and "view page source" gives me a blank page. Commented Dec 24, 2017 at 14:25
  • @TimBiegeleisen Is there maybe some kind of size restriction? I just tried with a reeeeeally tiny .jpg image, and it worked! Commented Dec 24, 2017 at 15:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.