67,513
社区成员
发帖
与我相关
我的任务
分享
<!--下载请求-->
<form action="fileDownload.do" method="post">
<input type="text" name="title"/>
<input type="submit" value="下载文件">
</form>
<!--struts-config.xml动态ActionForm配置信息-->
<form-bean name="fileDynaForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="title" type="java.lang.String"/>
<form-property name="file" type="org.apache.struts.upload.FormFile"/>
</form-bean>
<!--struts-config.xml中Action相应的配置信息-->
<action path="/fileDownload"
type="cn.edu.sjzri.title.action.FileDownloadAction"
name="fileDynaForm"
scope="request"
>
<forward name="success" path="/downloadSuccess.jsp"/>
</action>
//下载Action
public class FileDownloadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//获取数据
DynaActionForm daf = (DynaActionForm)form;
String title = (String) daf.get("title");
//下载操作
Connection conn = null;
PreparedStatement pst = null;
ResultSet rst = null;
InputStream ins = null;
conn = ConnectionDB.buildconnection();
try {
pst = conn.prepareStatement("select * from fileInfo where title = ?");
pst.setString(1, title);
rst = pst.executeQuery();
if(rst.next()) {
ins = rst.getBinaryStream("fileinfo");
response.setContentType("application/jpg");
int size = ins.available(); //注释1:size经测试为0
byte[] total = new byte[size];
ins.read(total);
ServletOutputStream souts = response.getOutputStream();
souts.write(total);
souts.flush();
souts.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
--数据库中存储文件表的信息
create table fileinfo
(
title varchar2(40) not null,
fileinfo blob not null
)
int ch;
int size;
StringBuffer sb = new StringBuffer();
while((ch = ins.read()) != -1) {
sb.append((char)ch);
}
size = sb.length();
byte[] total = new byte[size];
int ch;
int size;
StringBuffer sb = new StringBuffer();
while((ch = ins.read()) != -1) {
sb.append((char)ch);
}
size = sb.length();
byte[] total = new byte[size];