顯示之前,我已經將BLOB插入MYSQL併成功。我使用這段代碼來顯示BLOB到JSP。如何使用JSP在mysql中顯示BLOB?
Connection con = Koneksi.getKoneksi();
String id = request.getParameter("id_pengurus");
PreparedStatement ps = con.prepareStatement("select foto from pengurus where idpengurus = ?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
rs.next();
Blob b = rs.getBlob("foto");
response.setContentType("image/jpg");
response.setContentLength((int) b.length());
InputStream is = b.getBinaryStream();
OutputStream os = response.getOutputStream();
byte buf[] = new byte[(int) b.length()];
is.read(buf);
os.write(buf);
os.close();
當我使用此代碼來顯示SQL中的BLOB到JSP時,它已工作。 當我使用此代碼在MYSQL中將BLOB顯示爲JSP時,它不起作用。我應該做什麼解決方案?對不起,我的英語不好。以前感謝。
我不明白這個問題,但你不應該關閉你沒有打開的流(與問題無關,而是更多的一般規則)。 –
先生對不起。我在JSP中使用該代碼在ORACLE SQL中顯示BLOB。顯示blob SQL和mysql的代碼有可能不同嗎?謝謝 – Martin
當你調用'is.read(buf)'時,你永遠不會檢查讀了多少字節,所以你可能沒有收到所有的東西。如果你打算只將整個BLOB作爲一個字節數組,你應該調用'byte [] buf = rs.getBytes(「foto」)'而忘記'Blob'類。 – Andreas