前台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();
}
}