使用struts2如何实现文件下载功能
本篇文章为大家展示了使用struts2如何实现文件下载功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
10年的宏伟网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站的优势是能够根据用户设备显示端的尺寸不同,自动调整宏伟建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“宏伟网站设计”,“宏伟网站推广”以来,每个客户项目都认真落实执行。
1. 项目结构

2. web.xml
struts 2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts 2 /* index.jsp 30
3.DownloadAction.java
package com.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport{
private static final long serialVersionUID = 1L;
//文件路径
private String path;
//path属性的getter方法
public String getPath(){
return path;
}
//path属性的setter方法
public void setPath(String path){
this.path = path;
}
//返回inputstream,文件下载关键方法
public java.io.InputStream getDownloadFile() throws Exception{
InputStream in = ServletActionContext.getServletContext().getResourceAsStream(getPath());
return in;
}
public String execute() throws Exception{
return SUCCESS;
}
}4.struts.xml
image/jpeg attachment;filename="a.jpg" downloadFile 1024
5.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>" rel="external nofollow" > 首页 欢迎来到首页,点下面链接去下载一个文件
">下载
上述内容就是使用struts2如何实现文件下载功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
文章题目:使用struts2如何实现文件下载功能
本文URL:http://www.jxjierui.cn/article/jgjisc.html


咨询
建站咨询
