struts实现文件下载

利用struts实现文件下载

fileDownload.jsp代码如下:

 

<% @ page language = " java "  pageEncoding = " GB18030 " %>

<% @ taglib uri = " /WEB-INF/struts-bean.tld "  prefix = " bean "   %>
<% @ taglib uri = " /WEB-INF/struts-html.tld "  prefix = " html "   %>

<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >
< html:html >
  
< head >
    
    
< title > fileDownload.jsp </ title >

    
< meta http - equiv = " pragma "  content = " no-cache " >
    
< meta http - equiv = " cache-control "  content = " no-cache " >
    
< meta http - equiv = " expires "  content = " 0 " >     
    
< meta http - equiv = " keywords "  content = " keyword1,keyword2,keyword3 " >
    
< meta http - equiv = " description "  content = " This is my page " >
    
<!--
    
< link rel = " stylesheet "  type = " text/css "  href = " styles.css " >
    
-->

  
</ head >
  
  
< body >
    
< center >
    
< html:link page = " /fileDownload.do?id=1 " > 文件下载 </ html:link >
    
</ center >
  
</ body >
</ html:html >

 

FileDownloadAction.java代码如下:

 

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 
*/

package  com.iss.struts.action;

import  java.io.BufferedInputStream;
import  java.io.BufferedOutputStream;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileNotFoundException;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.OutputStream;
import  java.io.UnsupportedEncodingException;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  org.apache.struts.action.Action;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;

/** 
 * MyEclipse Struts
 * Creation date: 07-27-2007
 * 
 * XDoclet definition:
 * @struts.action validate="true"
 
*/

public   class  FileDownloadAction  extends  Action  {
    
/*
     * Generated Methods
     
*/


    
/** 
     * Method execute
     * 
@param mapping
     * 
@param form
     * 
@param request
     * 
@param response
     * 
@return ActionForward
     * 
@throws IOException 
     
*/

    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) 
throws IOException {
        String id
=request.getParameter("id");
        
if(id!=null&&!id.equals("")){
            id
=new String(id.getBytes("ISO-8859-1"),"GBK");
            
if(id.equals("1")){
                File file
=new File("E:/FileManage/doc命令.txt");
                String fileName
=file.getName();
                InputStream is
=new FileInputStream(file);
                OutputStream os
=response.getOutputStream();
                BufferedInputStream bis 
= new BufferedInputStream(is);
                BufferedOutputStream bos 
= new BufferedOutputStream(os);
                
                fileName 
= java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
                fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
                response.reset();
                response.setCharacterEncoding(
"UTF-8");
                response.setContentType(
"application/x-msdownload");// 不同类型的文件对应不同的MIME类型
                response.setHeader("Content-Disposition""attachment; filename="+fileName);
                
int bytesRead = 0;
                
byte[] buffer = new byte[1024];
                
while ((bytesRead = bis.read(buffer)) != -1{
                    bos.write(buffer, 
0, bytesRead);// 将文件发送到客户端
                }

                bos.flush();
                bis.close();
                bos.close();
                is.close();
                os.close();
            }

        }

        
return null;
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值