smartupload实现 jsp页面上传文件 将文件以二进制形式保存在数据库

本文介绍了一个使用jspSmartUpload组件将文件上传至数据库的示例代码。该示例展示了如何初始化上传组件、执行文件上传过程,并将文件保存为数据库中的二进制数据。此外还介绍了如何更新数据库中的文件名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值