博客
关于我
Struts2文件上传
阅读量:443 次
发布时间:2019-03-06

本文共 3006 字,大约阅读时间需要 10 分钟。

Struts文件上传和下载配置详细说明

一、文件上传配置

1.1 JSP页面配置

在JSP页面中,需配置文件上传表单,确保使用正确的表单类型和属性设置。以下是典型示例:

文件上传示例

1.2 Action类配置

文件上传Action类需继承ActionSupport,确保文件上传拦截器正常工作。以下是示例代码:

public class FileUpLoadAction extends ActionSupport {    private File image2;    private String image2FileName;    private List
listNames; public void setImage2(File file) { this.image2 = file; } public InputStream getInputStream() { return ServletActionContext.getServletContext().getResourceAsStream("/images/" + image2FileName); } public String execute() throws Exception { String realpath = ServletActionContext.getServletContext().getRealPath("/images"); if (image2 != null) { File savefile = new File(realpath + "/" + image2FileName); FileUtils.copyFile(image2, savefile); } listNames = findFileNames(realpath); return "success"; } private List
findFileNames(String path) { List
listNames = new ArrayList<>(); File file = new File(path); File[] files = file.listFiles(); for (File f : files) { if (f.isFile()) { listNames.add(f.getName()); } } return listNames; }}

1.3 Struts配置文件

在struts.xml中,配置文件上传拦截器和动作:

index.jsp
error.jsp
image/bmp,image/png,image/gif
102400
application/octet-stream
inputStream
attachment;fileName=${fileName}
4096

1.4 错误页面配置

错误页面用于显示文件上传失败时的错误信息:

    图片上传失败    

返回图片上传页

二、文件下载配置

2.1 Action类下载实现

下载Action类需提供文件名,返回输入流:

public class DownloadAction {    private String fileName;    public void setFileName(String fileName) {        this.fileName = fileName;    }    public InputStream getInputStream() {        return ServletActionContext.getServletContext().getResourceAsStream("/images/" + fileName);    }    public String execute() {        System.out.println("getFileName()=" + getFileName());        return "success";    }}

2.2 Struts配置文件

在struts.xml中配置下载动作:

application/octet-stream
inputStream
attachment;fileName=${fileName}
4096

三、web.xml配置

确保Struts过滤器正确配置:

struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/.action
struts2
/.jsp
index.jsp

以上配置详细说明了Struts文件上传和下载的实现方法,包括JSP页面、Action类、Struts配置文件和web.xml配置等内容。

转载地址:http://rgxyz.baihongyu.com/

你可能感兴趣的文章
Node-RED中使用html节点爬取HTML网页资料之爬取Node-RED的最新版本
查看>>
Node-RED中使用JSON数据建立web网站
查看>>
Node-RED中使用json节点解析JSON数据
查看>>
Node-RED中使用node-random节点来实现随机数在折线图中显示
查看>>
Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
查看>>
Node-RED中使用node-red-contrib-image-output节点实现图片预览
查看>>
Node-RED中使用node-red-node-ui-iframe节点实现内嵌iframe访问其他网站的效果
查看>>
Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
查看>>
Node-RED中使用range范围节点实现从一个范围对应至另一个范围
查看>>
Node-RED中实现HTML表单提交和获取提交的内容
查看>>
Node-RED中将CSV数据写入txt文件并从文件中读取解析数据
查看>>
Node-RED中建立TCP服务端和客户端
查看>>
Node-RED中建立Websocket客户端连接
查看>>
Node-RED中建立静态网页和动态网页内容
查看>>
Node-RED中解析高德地图天气api的json数据显示天气仪表盘
查看>>
Node-RED中连接Mysql数据库并实现增删改查的操作
查看>>
Node-RED中通过node-red-ui-webcam节点实现访问摄像头并截取照片预览
查看>>
Node-RED中配置周期性执行、指定时间阶段执行、指定时间执行事件
查看>>
Node-RED安装图形化节点dashboard实现订阅mqtt主题并在仪表盘中显示温度
查看>>
Node-RED怎样导出导入流程为json文件
查看>>