package com.action;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.org.apache.regexp.internal.REUtil;
public class DownloadAction extends ActionSupport
{
/**
* serialVersionUID:TODO(用一句话描述这个变量表示什么)
*
* @since Ver 1.1
*/
private static final long serialVersionUID = 1L;
private Dao dao=new Dao();
private List<FileItem>list;
private String fileId;
private FileItem fileItem;
//获得list
public String list()
{
list=dao.getFileList();
return "list-success";
}
//获得文件
public String get()
{
fileItem=dao.getFileItem(fileId);
return "get-success";
}
//获得输入流
public InputStream getInputStream()
{
try
{
String path=ServletActionContext.getServletContext().getRealPath("/");
String fileName=path+fileItem.getLocationPath();
FileInputStream fis=new FileInputStream(fileName);
return fis;
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//获得文件类型
public String getContentType()
{
return fileItem.getContentType();
}
//获得文件下载位置
public String getContentDisposition()
{
try
{
return "attachment;filename="+URLEncoder.encode(fileItem.getFileName(),"utf-8");
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//获得文件字节大小
public int getContentLength()
{
return fileItem.getContentLength();
}
public List<FileItem> getList()
{
return list;
}
public void setList(List<FileItem> list)
{
this.list = list;
}
public String getFileId()
{
return fileId;
}
public void setFileId(String fileId)
{
this.fileId = fileId;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页