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">&nbsp;</td>
                <td class="td1value" colspan="3">
                <table>
                        <tr><td>&nbsp;&nbsp;
                                <html:file property="data" size="50" />
                        </td></tr>
                        </tbody>
                </table>
        </tr>
        <tr>
                <td colspan="4" class="td1bottom" align="center" >&nbsp;
                        <html:button property="save" value="保存" onclick="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();
              }
        }
}


------------------说明:最好是右键 目标另存为,迅雷下载比较慢------------------

你可能感兴趣的:(java,apache,Hibernate,jsp,struts)