struts2上传下载文件

Struts 2中实现文件下载(修正中文问题)http://pengranxiang.iteye.com/blog/259401

上传多附件:

jsp:

<input type="file" name="upload" />

<input type="file" name="upload" />

 

action:

 
     private File[] upload;
     private String[] uploadContentType;
     private String[] uploadFileName;
     private String savePath =File.separator+"uploadFile"+File.separator+"appendix";

     public void setSavePath(String value{
         this.savePath = value;
     }
     private String getSavePath() throws Exception{
         return ServletActionContext.getRequest().getRealPath(savePath);
     }

  public void setUpload(File[] upload) {
   this.upload = upload;
  }
  public void setUploadContentType(String[] uploadContentType) {
   this.uploadContentType = uploadContentType;
  }
  public void setUploadFileName(String[] uploadFileName) {
   this.uploadFileName = uploadFileName;
  }
 
  public File[] getUpload() {
   return (this.upload);
  }
  public String[] getUploadContentType() {
   return (this.uploadContentType);
  }
  public String[] getUploadFileName() {
   return (this.uploadFileName);
     

  

 

 

//获取文件
  File[] files = getUpload();
  FileOutputStream fos = null;
  FileInputStream fis = null;
  String dataPath = getSavePath()+File.separator;
  
  
  if(files!=null){
   
   for (int i = 0 ; i < files.length ; i++){
    //把文件保存到磁盘
    fos =  new FileOutputStream( dataPath+getUploadFileName()[i]);
    fis = new FileInputStream(files[i]);
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = fis.read(buffer)) > 0){
        fos.write(buffer , 0 , len);
    }
    
    
    
   }
   fos.close();
      fis.close();
  }

 

 

下载:

jsp:

 

<a href="down_load.action?fileName=${uploadFileName}">${uploadFileName}</a>

aciton:

 

package com.ptnetwork.demandManagerSystem.web.action.admin;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;


 
@ParentPackage("admin")
@Results({ @Result(name = "success", type = "stream", params = { 

"contentType", "application/octet-stream;charset=ISO8859-1", 

"inputName", "inputStream", 

"contentDisposition", "attachment;filename="${downloadFileName}"", 

"bufferSize", "4096" }) }) 

public class DownLoadAction extends ActionSupport {
 
 
 private static final long serialVersionUID = 1L;
 private String fileName;

 private String inputPath="/uploadFile/appendix/";// 指定要被下载的文件路径  

 

  public void setFileName(String fileName) throws UnsupportedEncodingException { 
//   System.out.println("---------fileName-----"+fileName);

       this.fileName = fileName;

     } 


 public void setInputPath(String inputPath) { 
       this.inputPath = inputPath; 
      } 

   // 如果下载文件名为中文,进行字符编码转换  
      public String getDownloadFileName() { 
         String downloadFileName = fileName; 
  
          try { 
             downloadFileName = new String(downloadFileName.getBytes(),  "ISO8859-1"); 
        } catch (UnsupportedEncodingException e) { 
              e.printStackTrace(); 
         } 
   
          return downloadFileName; 
      } 
  
    public InputStream getInputStream() throws Exception { 
         

        return ServletActionContext.getServletContext().getResourceAsStream( 
          inputPath+fileName);

      } 
   
    public String execute() throws Exception { 
  
          return SUCCESS; 
      } 

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值