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);
Eutherpy
  • 4,471
  • 7
  • 40
  • 64
  • Please show us the code for what is happening on the JSP line starting with `List pics`. Also, which library are you using for base 64 conversion? – Tim Biegeleisen Dec 24 '17 at 13:47
  • @TimBiegeleisen I've added more details. – Eutherpy Dec 24 '17 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). – Tim Biegeleisen Dec 24 '17 at 14:05
  • @TimBiegeleisen I think there might be something wrong with my JSP, when I inspect element the `` tags aren't displayed at all, and "view page source" gives me a blank page. – Eutherpy Dec 24 '17 at 14:25
  • @TimBiegeleisen Is there maybe some kind of size restriction? I just tried with a reeeeeally tiny .jpg image, and it worked! – Eutherpy Dec 24 '17 at 15:26
  • Is this relevant to you? https://stackoverflow.com/questions/15855222/java-lang-abstractmethoderror-com-mysql-jdbc-preparedstatement-setblobiljava-i – Tim Biegeleisen Dec 24 '17 at 23:28

0 Answers0