显示MySql BLOB中的HTML图像

时间:2017-12-24 13:40:51

标签: java html mysql jsp

我使用

将图像作为blob存储在MySql数据库中
angular.module("moduleName")

    .directive("datepicker", function () {
        return {
            restrict: "C",
            link: function($scope,element){
                element.datepicker();
            }
        }
    })

其中preparedStatement.setBinaryStream(1, photo); photo

它们被正确插入(通常在MySql工作台中显示)。

我使用

检索它们
InputStream

并尝试在.jsp页面上显示它们:

resultSet.getBytes("Photo");

PictureTable.java:

<%
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%>>
<%
}
%>

ImageHelper.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;
    }

但不显示图像。

编辑:为了完整起见,这就是我存储图片的方式:

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

控制器:(<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>

@MultipartConfig

0 个答案:

没有答案