用户管理的设计--7.文件下载的两种方式

页面效果


实现步骤

1.Jsp页面要求

<TD class="ta_01" align="center" bgColor="#f5fafe">附件(下载):</TD>
<TD class="ta_01" bgColor="#ffffff" colSpan="3">
   <s:if test="elecUserFiles!=null && elecUserFiles.size()>0">
      <s:iterator value="elecUserFiles">
          <a href="#" onclick="openWindow('${pageContext.request.contextPath }/system/elecUserAction_download.do?fileID=<s:property value="fileID"/>','700','400');">
               <s:property value="fileName"/>
          </a>
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<s:date name="progressTime" format="yyyy-MM-dd HH:mm:ss"/>
          <br>
      </s:iterator>
   </s:if>         
 </TD>

2.VO对象中添加非持久化javabean属性

//附件ID
private String fileID;
public String getFileID() {
     return fileID;
}
public void setFileID(String fileID) {
   this.fileID = fileID;
}

 

3.两种文件下载方式

方式一:不使用struts2提供的文件下载(普通方式)

  Action类中添加方法:

    /**  
    * @Name: download
    * @Description: 文件下载(普通方式)
    * @Parameters: 无
    * @Return: 无
    */
    public String download(){
        try {
            //1.获取文件ID
            String fileID = elecUser.getFileID();
            
            //2.根据文件ID,查询附件表,获取路径path
            ElecUserFile elecUserFile = elecUserService.findUserfileByID(fileID);
            //(1)获取路径
            String path = ServletActionContext.getServletContext().getRealPath("") + elecUserFile.getFileURL();
            //(2)获取文件名
            String fileName = elecUserFile.getFileName();
            //(3)可能出现中文,解决中文乱码问题
            fileName=new String(fileName.getBytes("gbk"),"iso8859-1");
            //(4)填写下载文件的头部信息
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            
            //3.使用路径path,查找到对应的文件,转化成InputStream
            FileInputStream fileInputStream = new FileInputStream(new File(path));
            
            //4.从响应对象response中获取输出流
            OutputStream outputStream = response.getOutputStream();
            
            //5.将输入流数据读出写到输出流
            byte[] buf=new byte[1024];
            int length=0;
            while((length=fileInputStream.read(buf))!=-1){
                outputStream.write(buf, 0, length);
            }
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return NONE;
    }

方式二:使用struts2提供的文件下载

第一步:配置struts.xml

<result name="download" type="stream">
      <param name="inputName">inputStream</param>
      <param name="contentDisposition">attachment;filename="${#request.fileName}"</param>
      <param name="bufferSize">1024</param>
</result>

第二步:VO对象中,添加InputStream类型的属性

在模型驱动的对象中,添加InputStream类型的属性主要用来存放文件的输入流,其中属性名称要与struts.xml中定义的inputName的值一致。

//文件下载的流的属性
private InputStream inputStream;
public InputStream getInputStream() {
    return inputStream;
}
public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; }

第四步:Action类中添加代码

    /**  
    * @Name: download
    * @Description: 文件下载(struts2的方式)
    * @Parameters: 无
    * @Return: struts2的结果类型
    */
    public String download(){
        try {
            //1.获取文件ID
            String fileID = elecUser.getFileID();
            
            //2.根据文件ID,查询附件表,获取路径path
            ElecUserFile elecUserFile = elecUserService.findUserfileByID(fileID);
            //(1)获取路径
            String path = ServletActionContext.getServletContext().getRealPath("") + elecUserFile.getFileURL();
            //(2)获取文件名
            String fileName = elecUserFile.getFileName();
            //(3)可能出现中文,解决中文乱码问题
            fileName=new String(fileName.getBytes("gbk"),"iso8859-1");
            request.setAttribute("fileName", fileName);
            
            //3.使用路径path,查找到对应的文件,转化成InputStream
            FileInputStream fileInputStream = new FileInputStream(new File(path));
            
            //4.与栈顶的inputStream关联,将查询的文件输入流放置到模型驱动定义的inputStream属性中,用来输出文件
            elecUser.setInputStream(fileInputStream);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "download";
    }
原文地址:https://www.cnblogs.com/zhstudy/p/7163070.html