下载方法一:通过struts2的方法进行下载文件
Action中设置:
package com.yuhong.see.action;
import java.io.FileInputStream;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import per.jemi.util.Utils;
import com.yuhong.see.action.qr.UserModelFileListQR;
import com.yuhong.see.bean.FileDownloadInfo;
import com.yuhong.see.service.FileService;
import com.yuhong.see.util.Message;
@Component
@Scope("prototype")
public class UserFilesDownloadAction extends BaseAction{
private static final String FILES_INFO_SESSION_KEY="files_info_session_key";
private static final long serialVersionUID = 1L;
@Autowired
private FileService fileService;
public FileInputStream file;
private String fileName;
//在页面中调用该方法,然后在该方法中找出需要下载的文件的file和fileName(真正的文件名)的参数值,并判断是否真实有值
public String download(){
Object slist=this.getSession().getAttribute(FILES_INFO_SESSION_KEY);
if(slist!=null){
List<FileDownloadInfo> li=(List<FileDownloadInfo>)slist;
for(FileDownloadInfo o:li){
if(o.getFileCode().equals(this.fileCode)){
this.file=o.getFileInputStream();
this.fileName=o.getFileName()+"."+o.getFileType();
break;
}
}
}
if(file!=null)
return "templates";
else{
this.message=new Message("0001","下载失败请重试!");
execute();
return SUCCESS;
}
}
public FileInputStream getFile() {
return file;
}
public void setFile(FileInputStream file) {
this.file = file;
}
public String getFileCode() {
return fileCode;
}
public void setFileCode(String fileCode) {
this.fileCode = fileCode;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
在struts中设置:
<action name="filesdownload" class="userFilesDownloadAction">
<result type="stream" name="templates">
<param name="inputName">file</param>//action中的FileInputStream file;
<param name="bufferSize">1024</param>
<param name="contentType">application/octet-stream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>//action中的fileName
<param name="bufferSize">1024</param>
</result>
<result>/user/modelfilesdownload.jsp</result>
</action>
在页面中设置:调用action中的调用方法
Action中设置:
package com.yuhong.see.action;
import java.io.FileInputStream;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import per.jemi.util.Utils;
import com.yuhong.see.action.qr.UserModelFileListQR;
import com.yuhong.see.bean.FileDownloadInfo;
import com.yuhong.see.service.FileService;
import com.yuhong.see.util.Message;
@Component
@Scope("prototype")
public class UserFilesDownloadAction extends BaseAction{
private static final String FILES_INFO_SESSION_KEY="files_info_session_key";
private static final long serialVersionUID = 1L;
@Autowired
private FileService fileService;
public FileInputStream file;
private String fileName;
//在页面中调用该方法,然后在该方法中找出需要下载的文件的file和fileName(真正的文件名)的参数值,并判断是否真实有值
public String download(){
Object slist=this.getSession().getAttribute(FILES_INFO_SESSION_KEY);
if(slist!=null){
List<FileDownloadInfo> li=(List<FileDownloadInfo>)slist;
for(FileDownloadInfo o:li){
if(o.getFileCode().equals(this.fileCode)){
this.file=o.getFileInputStream();
this.fileName=o.getFileName()+"."+o.getFileType();
break;
}
}
}
if(file!=null)
return "templates";
else{
this.message=new Message("0001","下载失败请重试!");
execute();
return SUCCESS;
}
}
public FileInputStream getFile() {
return file;
}
public void setFile(FileInputStream file) {
this.file = file;
}
public String getFileCode() {
return fileCode;
}
public void setFileCode(String fileCode) {
this.fileCode = fileCode;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
在struts中设置:
<action name="filesdownload" class="userFilesDownloadAction">
<result type="stream" name="templates">
<param name="inputName">file</param>//action中的FileInputStream file;
<param name="bufferSize">1024</param>
<param name="contentType">application/octet-stream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>//action中的fileName
<param name="bufferSize">1024</param>
</result>
<result>/user/modelfilesdownload.jsp</result>
</action>
在页面中设置:调用action中的调用方法