1. 程式人生 > >smartupload實現 jsp頁面上傳檔案 將檔案以二進位制形式儲存在資料庫

smartupload實現 jsp頁面上傳檔案 將檔案以二進位制形式儲存在資料庫

<%@page import="com.sys.utils.DBConnection"%>
<%@ page language="java" import="java.sql.*,com.jspsmart.upload.*"%>


<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />


<HTML>


<!-- 上傳到資料庫   以二進位制存-->
<BODY BGCOLOR="white">


<H1>jspSmartUpload : Sample 4</H1>
<HR>


<%


// Variables
int count=0;



Connection con = DBConnection.connection();

// SQL Request
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY ,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM tfiles WHERE ID=1");


// if the resultset is not null
if (rs.next()){


// Initialization
mySmartUpload.initialize(pageContext);


// Upload
mySmartUpload.upload();


// upload file in the DB if this file is not missing
if (!mySmartUpload.getFiles().getFile(0).isMissing()){


try {


rs.updateString("FILENAME",mySmartUpload.getFiles().getFile(0).getFileName());

// Add the current file in the DB field
mySmartUpload.getFiles().getFile(0).fileToField(rs,"FILE");


// Update
rs.updateRow();
count++;

} catch(Exception e) {
out.println("An error occurs : " + e.toString());
}


}


}


// Display the number of files uploaded
out.println(count + " file(s) uploaded in the database.");


rs.close();
stmt.close();
con.close();


%>


</BODY>
</HTML>