下载方法

下载方法一:通过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中的调用方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值