文件下载
package com.yutu.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yutu.biz.FileInfoBiz;
import com.yutu.biz.impl.FileInfoBizImpl;
import com.yutu.entity.FileInfo;
public class downloadAction extends ActionSupport {
private InputStream is;// 用来读取下载的文件
// private String SavePath;// 下载文件保存的路径
private String fileName; // 下载的文件的文件名
private FileInfoBiz fb = new FileInfoBizImpl();
private long size;// 文件字节大小
private String no;// 文件编号
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public InputStream getIs() {
return is;
}
public void setIs(InputStream is) {
this.is = is;
}
// public String getSavePath() {
// return SavePath;
// }
//
// public void setSavePath(String savePath) {
// SavePath = savePath;
// }
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String downloadFile() {
FileInfo info = fb.queryFile(no);
// String
// dir=ServletActionContext.getServletContext().getRealPath(SavePath);
File dowmloadFile = new File(info.getRealPath());
fileName = info.getFileName();
size = info.getSize();
if (dowmloadFile.exists()) {
try {
is = new FileInputStream(dowmloadFile);// 如果存在用流读取
return SUCCESS;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "error";
}
return "error";
}
}
action 配置
<action name="download" class="com.yutu.action.downloadAction"
method="downloadFile">
<!-- <param name="savePath">/uploadDir</param> -->
<result name="success" type="stream">
<param name="inputName">is</param>
<param name="contentType">application/octet-stream</param>
<param name="bufferSize">4096</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="contentLength">${size}</param>
</result>
<result name="error">erroe.jsp</result>
</action>
实体类
package com.yutu.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "file_info")
public class FileInfo implements Serializable {
@Id
@Column(name = "no",length=100)
private String no;
@Column(name="file_name",length=100)
private String fileName;
@Column(name="path",length=1000)
private String path;
@Column(name="real_path",length=1000)
private String realPath;
@Column(name="size")
private Long size;
@Column(name="content_type",length=100)
private String contentType;
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getRealPath() {
return realPath;
}
public void setRealPath(String realPath) {
this.realPath = realPath;
}
public Long getSize() {
return size;
}
public void setSize(Long size) {
this.size = size;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}