前台jsp页面:putImage.jsp
<body>
<form action="putImage.do" method="post"
ENCTYPE="multipart/form-data">
<input name="file" type="file">
<input type="submit" name="submit" value="上传">
</form>
</body>
Struts中action中的代码
FormFile file = pif.getFile();
InputStream photoStream = file.getInputStream();//这里要特别注意和别的资料的区别
try {
new Image().putimg(file, photoStream,file.getFileSize());
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("success");
底层的一个dao方法
public void putimg(FormFile file,InputStream photoStream,int size) {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=cmtony";
Connection conn = DriverManager.getConnection(url, "sa", "sa");
PreparedStatement ps = null;
String sql = "insert into image(img) values (?)";
ps = conn.prepareStatement(sql);
ps.setBinaryStream(1, photoStream, size);
ps.executeUpdate();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
本文介绍了一个使用Struts框架实现文件上传的例子,并详细展示了如何将上传的文件存储到SQL Server数据库的过程。该过程涉及从前端页面到后台Action处理再到DAO层的具体实现。
4396

被折叠的 条评论
为什么被折叠?



