iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Struts2实现单文件或多文件上传功能
  • 168
分享到

Struts2实现单文件或多文件上传功能

struts2文件上传st 2023-05-31 17:05:33 168人浏览 八月长安
摘要

一、简述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 文件,指定上传大小 如下:

Struts2实现单文件或多文件上传功能

一次上传只可以上传10M,不管一次上传多少个文件,按总和计算

2)在struts.xml文件中指定,如图:

Struts2实现单文件或多文件上传功能

其实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文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作