java 配合 Hibernate ,struts 的上传下载的总结

java 配合 Hibernate ,struts 的上传下载的总结
用的数据库是 sqlserver2000 数据库中大对象的类型是 image

1,上传文件的页面,使用了Struts 标签

<html:form action="/dmdcollect/submit/slsave" method="post" enctype="multipart/form-data" >
<tr>
<td class="td1value"> </td>
<td class="td1value" colspan="3">
<table>
<tr><td>  
<html:file property="data" size="50" />
</td></tr>
</tbody>
</table>
</tr>
<tr>
<td colspan="4" class="td1bottom" align="center" > 
<html:button property="save" value="保存" οnclick="dosave()" />
</td>
</tr>
</html:form>

<script language="javascript">

function dosave(){
var dat = document.getElementById("data");
if(dat.value==""){
alert("请选择上传文件");
return ;
}else{
document.forms[0].submit();
}
}

</script>

Struts 的配置文件

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="DemandDataForm"
dynamic="true"
type="org.apache.struts.action.DynaActionForm">
<form-property name="data" type="org.apache.struts.upload.FormFile" />
</form-bean>
</form-beans>
<action-mappings>
<action path="/dmdcollect/submit/slsave"
type="com.htjc.web.struts.action.dmdcollect.submit.SlSave"
name="DemandDataForm"
scope="request"
validate="false">
<forward name="view" path="/jsp/dmdcollect/submit/sllist.jsp" />
</action>
<action path="/dmdcollect/submit/sldown"
type="com.htjc.web.struts.action.dmdcollect.submit.SlDownLoad"
name="DemandDataForm"
scope="request"
validate="false">
</action>
</action-mappings>
</struts-config>

struts de Action

public class SlSave extends CollectAction{
public ActionForward execute(ActionMapping mappings, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{

DynaActionForm dForm = (DynaActionForm)form;
FormFile file = (FormFile) dForm.get("data");

String fileName = file.getFileName();
//获得后缀
String suffix = "";
if(fileName.lastIndexOf(".")!=-1){
suffix = fileName.substring(fileName.lastIndexOf("."));
}
//DemandInfoData 的数据是类型是byte[]
//file.getFileData() 的返回类型也是byte[]
DemandInfoData data = new DemandInfoData();
data.setDataID(dataID);
data.setData(file.getFileData());

HibernateHelper.save(data);

}
}

-------------------------------数据保存成功了---下面是数据下载-----------------------------

jsp 页面

<td width="30%">
<a href="<%=imgpath %>/dmdcollect/submit/zldown.do?infoID=<%=info.getInfoID() %>&dataID=<%=info.getDataID() %>">
<%=info.getFileName() %>
</a>
</td>
public class SlDownLoad extends CollectAction{
public ActionForward execute(ActionMapping mappings, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{

String dataID=request.getParameter("dataID");
String infoID = request.getParameter("infoID");


DemandInfo info = collectSystem.getCollectManager().getDemandInfo(infoID);
DemandInfoData data = collectSystem.getCollectManager().getDemandInfoData(dataID);

response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","attachment;" +
" filename="+new String(info.getFileName().getBytes(), "ISO-8859-1"));
ServletOutputStream sout = response.getOutputStream();
try {
InputStream in = new ByteArrayInputStream(data.getData());
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) > 0) {
sout.write(b, 0, i);
}
sout.flush();
in.close();
sout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}


------------------说明:最好是右键 目标另存为,迅雷下载比较慢------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值