一、简述Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUpload" class="org.apache.struts2.in
一、简述
Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
二、指定用户上传文件的大小,有两种方式
1)默认是在default.properties 文件的 struts.multipart.maxSize=2097152 键值指定为2097152 也就是2M,通过计算 2097152/(1024*1024) = 2 M
那我们可以改变其默认值,只要在src目录下,新建一个 struts.properties 文件,指定上传大小 如下:
一次上传只可以上传10M,不管一次上传多少个文件,按总和计算
2)在struts.xml文件中指定,如图:
其实name就对应struts.properties的键,value对应 值
注意:如果即在struts.properties设定文件上传大小,又在struts.xml 设定文件上传大小,则struts.properties的优先级高于struts.xml,一般在一处指定上传大小即可,推荐 struts.properties
三、Struts2之单文件上传
fileupload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > <title>My JSP 'fileupload.jsp' starting page</title> <meta Http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keyWords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/CSS" href="styles.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" > --> </head> <body> <!-- enctype 默认是 application/x-www-fORM-urlencoded --> <form action="FileUpload2" enctype="multipart/form-data" method="post" > 用户名:<input type="text" name="usename"> <br/> 上传文件:<input type="file" name="file1"><br/> <input type="submit" value="提交"/> </form> </body> </html>
--结束END--
本文标题: Struts2实现单文件或多文件上传功能
本文链接: https://www.lsjlt.com/news/227237.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0