strust2 文件下载

struts.xml配置:

  <action name="downloadDocument" class="mainAction" method="downloadDocument">
        <result name="downFile" type="stream">
           <param name="contentType">application/x-msdownload;charset=utf-8</param>
    <param name="inputName">is</param>
    <param name="contentDisposition">attachment;filename="${fileName}"</param>
    <param name="bufferSize">2048</param>
            </result>
    <interceptor-ref name="defaultStack" />
   <interceptor-ref name="SessionValidation" />
  </action>

 

downloadDocument 方法

 private InputStream is;       
 private String fileName;

 public String downloadDocument(){
  try{
   String filePath = "\\a.doc";
   String path=new String(filePath.getBytes("iso-8859-1"), "UTF-8");    
   String savePath = getRequest().getSession().getServletContext().getRealPath("/file");
   File file = new File(savePath+path);
   is = new FileInputStream(file);
   fileName ="信息预算上报系统用户操作手册.doc";
   fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
  }catch(Exception e){
   e.printStackTrace();
  }
  return "downFile";
 }

 

 public InputStream getIs() {
  return is;
 }

 public void setIs(InputStream is) {
  this.is = is;
 }

 public String getFileName() {
  return fileName;
 }

 public void setFileName(String fileName) {
  this.fileName = fileName;
 }
 

你可能感兴趣的:(strust2 文件下载)