oracle,weblogic読み書きblob
開発環境:XP+Myeclipse 6.6+Weblogic92
2アップロードコンポーネント:smart-upload
3つのサンプルコード
1 JSPコード
2 javaコード:DBへのファイルの保存と読み込み
3 DBバイト配列を読み出す画像形式にマッピングする:web.xml対応マッピングの追加:/readImg
4 JSでのDB画像の読み出し操作
5添付ボックスの値をクリアするコンポーネント
clearFile.jsは添付ファイルを参照してください.対応する例があります.
2アップロードコンポーネント:smart-upload
3つのサンプルコード
1 JSPコード
<%@ page language="java" pageEncoding="UTF-8"%>
<form name=form1 method=post enctype="multipart/form-data" action="/testFile">
<input type="file" name="f" id="f"/>
<img src="" id="img" name="img" width="100%" border="0" height="100%"/>
</form>
2 javaコード:DBへのファイルの保存と読み込み
request.setCharacterEncoding("UTF-8");
try {
com.jspsmart.upload.SmartUpload mySmartUpload = new com.jspsmart.upload.SmartUpload();
mySmartUpload.initialize(getServletConfig(), request, response);
mySmartUpload.upload();
com.jspsmart.upload.Request rq = mySmartUpload.getRequest();
byte[] bytes = null;
//
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing()) {
int fileLength = myFile.getSize();
bytes = new byte[fileLength];
for (int j = 0; j < fileLength; j++) {
bytes[j] = myFile.getBinaryData(j);
}
}
}
// DB
if(null != bytes){
try {
java.sql.Connection conn = DbUtils.getConn();
//test fcontext :BLOB
String sql = "update test set fcontext=empty_blob()";
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
sql = "select fcontext from test FOR UPDATE";
PreparedStatement ps = conn.prepareStatement(sql);
ps.executeUpdate();
java.sql.ResultSet rs = ps.getResultSet();
if (rs.next()) {
if (bytes.length > 0) {
weblogic.jdbc.vendor.oracle.OracleThinBlob content = (weblogic.jdbc.vendor.oracle.OracleThinBlob)
rs.getBlob("fcontext");
OutputStream outstream = content.getBinaryOutputStream();
try {
outstream.write(byteContent, 0, byteContent.length);
outstream.flush();
outstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
returnValue = true;
DbUtils.close(rs);
DbUtils.close(ps);
DbUtils.close(stmt);
DbUtils.close(conn);
} catch (SQLException ex) {
ex.printStackTrace();
}
}
// DB
bytes = new byte[0];
try {
String sql = "select fcontext from test";
java.sql.Connection conn = DbUtils.getConn();
PreparedStatement ps = conn.prepareStatement(sql);
ps.executeUpdate();
java.sql.ResultSet rs = ps.getResultSet();
if(rs.next()){
java.sql.Blob bolbObject = rs.getBlob("fcontext");
if (bolbObject != null) {
int length = (int) bolbObject.length();
BufferedInputStream inStream = new BufferedInputStream(bolbObject.getBinaryStream());
returnValue = new byte[length];
inStream.read(bytes);
inStream.close();
}
}
DbUtils.close(rs);
DbUtils.close(ps);
DbUtils.close(conn);
} catch (Exception ex) {
ex.printStackTrace();
}finally{
//
}
}catch (SmartUploadException e) {
e.printStackTrace();
}
}
3 DBバイト配列を読み出す画像形式にマッピングする:web.xml対応マッピングの追加:/readImg
class ReadDbImage extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
java.io.ByteArrayInputStream in = null;
java.io.OutputStream outStream = null;
// 。
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
// DAO Blog
byte[] fcontext = getFilContext();
if (fcontext != null) {
response.reset();
in = new ByteArrayInputStream(fcontext);
outStream = response.getOutputStream();
byte[] buf = new byte[1024];
int bytes = 0;
while((bytes = in.read(buf)) != -1)
outStream.write(buf, 0, bytes);
in.close();
outStream.close();
outStream.flush();
in = null;
outStream = null;
}
}
}
4 JSでのDB画像の読み出し操作
document.getElementById("img").src="/readImg?time="+new Date();
5添付ボックスの値をクリアするコンポーネント
clearFile.jsは添付ファイルを参照してください.対応する例があります.