struts2文件上传下载

Struts2文件上传下载
文件上传

/**
*
*/
package com.zdvictory.taurus.filesupload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
import com.zdvictory.taurus.common.util.*;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;

/** *
*/
public class UploadFileHandler {

private static int BUFFER_SIZE = 8192;

/**
  * 上传附件操作 传递参数:系统参数配置设置的参数名称
  */
@SuppressWarnings("unchecked")
public static List<Attachment> upload(String sysParaName) throws Exception {
  // 文件保存路径
  String path = SysParaFinder.getSysParaValue(sysParaName);
  List<Attachment> list = new ArrayList<Attachment>();
  MultiPartRequestWrapper request = (MultiPartRequestWrapper) ServletActionContext
    .getRequest();
  Enumeration enu = request.getFileParameterNames();
  while (enu.hasMoreElements()) { // 对每一个文件域进行遍历
   String controlName = (String) enu.nextElement();
   String[] fileNames = request.getFileNames(controlName);
   File[] uploadFiles = request.getFiles(controlName);
   for (int i = 0; i < uploadFiles.length; i++) {
    File uploadFile = uploadFiles[i];
    if (!uploadFile.exists())
     continue;
    // 如果文件夹不存在,创建文件夹,将文件保存到目录
    File dir = new File(request.getRealPath("/") + path);
    if (!dir.exists())
     dir.mkdirs();
    String ext = fileNames[i].substring(fileNames[i].indexOf("."),
      fileNames[i].length());// 获取文件扩展名
    String filename = UUID.randomUUID().toString() + ext;
    File file = new File(request.getRealPath("/") + path + filename);
    byte[] data = new byte[BUFFER_SIZE];
    int byteRead = -1;
    FileInputStream in = new FileInputStream(uploadFile);
    FileOutputStream out = new FileOutputStream(file);
    while ((byteRead = in.read(data)) != -1) {
     out.write(data, 0, byteRead);
     out.flush();
    }
    out.close();
    in.close();
    // 设置附件对象属性
    Attachment attach = new Attachment();
    attach.setFilename(fileNames[i]);
    attach.setContentType(ext);
    attach.setFilepathname(path + filename);
    attach.setFilesize(uploadFile.length());
    list.add(attach);
   }
  }
  return list;
}
}
文件下载

public String download() throws Exception {
  redheadTemplate = redheadTemplateManager.findById(Long
    .valueOf(getId()[0]));
  String name = redheadTemplate.getName()
    + redheadTemplate.getFilepathname().substring(
      redheadTemplate.getFilepathname().lastIndexOf("."),
      redheadTemplate.getFilepathname().length());
  this.setFilename(new String(name.getBytes(), "ISO8859-1"));
  this.setFilepathname(redheadTemplate.getFilepathname());
  return "download";
}
文件下载配置文件


   <result name="download" type="stream">
    <!-- 下载文件类型 -->
    <param name="contentType">
     application/octet-stream
    </param>
    <!-- 默认为 inline(在线打开),设置为 attachment 将会告诉浏览器下载该文件,filename 指定下载文         
     件保存时的文件名,若未指定将会是以浏览的页面名作为文件名,如以 download.action 作为文件名,
     这里使用的是动态文件名,${filename}, 它将通过 Action 的 getFilename() 获得文件名 -->
    <param name="contentDisposition">
     attachment;filename="${filename}"
    </param>
    <!-- 下载的InputStream流,Struts2自动对应Action中的getDownloadFile方法,该方法必须返回InputStream类型 -->
    <param name="inputName">downloadFile</param>
    <!-- 输出时缓冲区的大小 -->
    <param name="bufferSize">8192</param>
   </result>
 

猜你喜欢

转载自chenshengzun.iteye.com/blog/1167484