当前位置:首页 » 文件管理 » struts2上传配置文件

struts2上传配置文件

发布时间: 2022-10-21 04:07:48

① struts2中上传文件时报404错误

如果说配置文件字段名都没有错的话那就是说你的上传的文件超过了4M,而fileupload默认的上传限度为4M!如果没有跟改配置的话那就会抛出异常!而在struts2中则是返回为input!而你又在struts.xml中没有配置<result name = "input">/test.jsp</result>所以为出现404!如果先要根治的话!你需要在配置文件中配置一下<constant name="struts.multipart.maxSize" value="20480000"/>
value的值你可以看着设!最大限度是2G

② struts2框架需要哪些配置文件

1、核心配置文件:一般情况下是写在src下的struts.xml文件,在这个文件中配置action类的跳转信息等,主要标签是package、action和result这三个;

2、过滤器:配置在web.xml中;

3、剩下的就是一些jar包,这些jar包在struts的官网上都有完整的压缩包免费下载的;

更加具体的一些配置,比如说struts.xml中那三个标签的应用、过滤器的配置、约束的导入何制作等这些都比较详细,要是想快速入门可以看一下别人的博客教程文章,我给你推荐一个吧,还是挺详细的,比较适合初学者;

struts2框架搭建

希望对你有所帮助,有帮助的话可以给我个大拇指哦~

java中怎么利用struts2上传多个pdf文件

通过3种方式模拟多个文件上传
第一种方式
package com.ljq.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UploadAction extends ActionSupport{
private File[] image; //上传的文件
private String[] imageFileName; //文件名称
private String[] imageContentType; //文件类型
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if (image != null) {
File savedir=new File(realpath);
if(!savedir.getParentFile().exists())
savedir.getParentFile().mkdirs();
for(int i=0;i<image.length;i++){
File savefile = new File(savedir, imageFileName[i]);
FileUtils.File(image[i], savefile);
}
ActionContext.getContext().put("message", "文件上传成功");
}
return "success";
}
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
}
第二种方式
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用数组上传多个文件
*
* @author ljq
*
*/
@SuppressWarnings("serial")
public class UploadAction2 extends ActionSupport{
private File[] image; //上传的文件
private String[] imageFileName; //文件名称
private String[] imageContentType; //文件类型
private String savePath;
@Override
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
//取得需要上传的文件数组
File[] files = getImage();
if (files !=null && files.length > 0) {
for (int i = 0; i < files.length; i++) {
//建立上传文件的输出流, getImageFileName()[i]
System.out.println(getSavePath() + "\\" + getImageFileName()[i]);
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName()[i]);
//建立上传文件的输入流
FileInputStream fis = new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len=fis.read(buffer))>0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
}
}
return SUCCESS;
}
public File[] getImage() {
return image;
}
public void setImage(File[] image) {
this.image = image;
}
public String[] getImageFileName() {
return imageFileName;
}
public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}
public String[] getImageContentType() {
return imageContentType;
}
public void setImageContentType(String[] imageContentType) {
this.imageContentType = imageContentType;
}
/**
* 返回上传文件保存的位置
*
* @return
* @throws Exception
*/
public String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
第三种方式
package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 使用List上传多个文件
*
* @author ljq
*
*/
@SuppressWarnings("serial")
public class UploadAction3 extends ActionSupport {
private List<File> image; // 上传的文件
private List<String> imageFileName; // 文件名称
private List<String> imageContentType; // 文件类型
private String savePath;
@Override
public String execute() throws Exception {
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
// 取得需要上传的文件数组
List<File> files = getImage();
if (files != null && files.size() > 0) {
for (int i = 0; i < files.size(); i++) {
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getImageFileName().get(i));
FileInputStream fis = new FileInputStream(files.get(i));
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}
}
return SUCCESS;
}
public List<File> getImage() {
return image;
}
public void setImage(List<File> image) {
this.image = image;
}
public List<String> getImageFileName() {
return imageFileName;
}
public void setImageFileName(List<String> imageFileName) {
this.imageFileName = imageFileName;
}
public List<String> getImageContentType() {
return imageContentType;
}
public void setImageContentType(List<String> imageContentType) {
this.imageContentType = imageContentType;
}
/**
* 返回上传文件保存的位置
*
* @return
* @throws Exception
*/
public String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
<constant name="struts.action.extension" value="do" />
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true" />
<!-- 默认的视图主题 -->
<constant name="struts.ui.theme" value="simple" />
<!--<constant name="struts.objectFactory" value="spring" />-->
<!--解决乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.multipart.maxSize" value="10701096"/>
<package name="upload" namespace="/upload" extends="struts-default">
<action name="*_upload" class="com.ljq.action.UploadAction" method="{1}">
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
<package name="upload1" namespace="/upload1" extends="struts-default">
<action name="upload1" class="com.ljq.action.UploadAction2" method="execute">
<!-- 要创建/image文件夹,否则会报找不到文件 -->
<param name="savePath">/image</param>
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
<package name="upload2" namespace="/upload2" extends="struts-default">
<action name="upload2" class="com.ljq.action.UploadAction3" method="execute">
<!-- 要创建/image文件夹,否则会报找不到文件 -->
<param name="savePath">/image</param>
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
上传表单页面upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<!-- ${pageContext.request.contextPath}/upload/execute_upload.do -->
<!-- ${pageContext.request.contextPath}/upload1/upload1.do -->
<!-- ${pageContext.request.contextPath}/upload2/upload2.do -->
<!-- -->
<form action="${pageContext.request.contextPath}/upload2/upload2.do" enctype="multipart/form-data" method="post">
文件1:<input type="file" name="image"><br/>
文件2:<input type="file" name="image"><br/>
文件3:<input type="file" name="image"><br/>
<input type="submit" value="上传" />
</form>
</body>
</html>
显示页面message.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'message.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">
</head>
<body>
上传成功
<br/>
<s:debug></s:debug>
</body>
</html>

④ Struts2 配置文件标签问题

在struts的配置文件中的形式为:

<constant name="struts.i18n.encoding" value="UTF-8" />

struts.action.extension
The URL extension to use to determine if the request is meant for a Struts action
用URL扩展名来确定是否这个请求是被用作Struts action,其实也就是设置 action的后缀,例如login.do的'do'字。

struts.configuration
The org.apache.struts2.config.Configuration implementation class
org.apache.struts2.config.Configuration接口名

struts.configuration.files
A list of configuration files automatically loaded by Struts
struts自动加载的一个配置文件列表

struts.configuration.xml.reload
Whether to reload the XML configuration or not
是否加载xml配置(true,false)

struts.continuations.package
The package containing actions that use Rife continuations
含有actions的完整连续的package名称

struts.custom.i18n.resources
Location of additional localization properties files to load
加载附加的国际化属性文件(不包含.properties后缀)

struts.custom.properties
Location of additional configuration properties files to load
加载附加的配置文件的位置

struts.devMode
Whether Struts is in development mode or not
是否为struts开发模式

struts.dispatcher.parametersWorkaround
Whether to use a Servlet request parameter workaround necessary for some versions of WebLogic
(某些版本的weblogic专用)是否使用一个servlet请求参数工作区(PARAMETERSWORKAROUND)

struts.enable.DynamicMethodInvocation
Allows one to disable dynamic method invocation from the URL
允许动态方法调用

struts.freemarker.manager.classname
The org.apache.struts2.views.freemarker.FreemarkerManager implementation class
org.apache.struts2.views.freemarker.FreemarkerManager接口名

struts.i18n.encoding
The encoding to use for localization messages
国际化信息内码

struts.i18n.reload
Whether the localization messages should automatically be reloaded
是否国际化信息自动加载

struts.locale
The default locale for the Struts application
默认的国际化地区信息

struts.mapper.class
The org.apache.struts2.dispatcher.mapper.ActionMapper implementation class
org.apache.struts2.dispatcher.mapper.ActionMapper接口

struts.multipart.maxSize
The maximize size of a multipart request (file upload)
multipart请求信息的最大尺寸(文件上传用)

struts.multipart.parser
The org.apache.struts2.dispatcher.multipart.MultiPartRequest parser implementation for a multipart request (file upload)
专为multipart请求信息使用的 org.apache.struts2.dispatcher.multipart.MultiPartRequest解析器接口(文件上传用)

struts.multipart.saveDir
The directory to use for storing uploaded files
设置存储上传文件的目录夹

struts.objectFactory
The com.opensymphony.xwork2.ObjectFactory implementation class
com.opensymphony.xwork2.ObjectFactory接口(spring)

struts.objectFactory.spring.autoWire
Whether Spring should autoWire or not
是否自动绑定Spring

struts.objectFactory.spring.useClassCache
Whether Spring should use its class cache or not
是否spring应该使用自身的cache

struts.objectTypeDeterminer
The com.opensymphony.xwork2.util.ObjectTypeDeterminer implementation class
com.opensymphony.xwork2.util.ObjectTypeDeterminer接口

struts.serve.static.browserCache
If static content served by the Struts filter should set browser caching header properties or not
是否struts过滤器中提供的静态内容应该被浏览器缓存在头部属性中

struts.serve.static
Whether the Struts filter should serve static content or not
是否struts过滤器应该提供静态内容

struts.tag.altSyntax
Whether to use the alterative syntax for the tags or not
是否可以用替代的语法替代tags

struts.ui.templateDir
The directory containing UI templates
UI templates的目录夹

struts.ui.theme
The default UI template theme
默认的UI template主题

struts.url.http.port
The HTTP port used by Struts URLs
设置http端口

struts.url.https.port
The HTTPS port used by Struts URLs
设置https端口

struts.url.includeParams
The default includeParams method to generate Struts URLs
在url中产生 默认的includeParams

struts.velocity.configfile
The Velocity configuration file path
velocity配置文件路径

struts.velocity.contexts
List of Velocity context names
velocity的context列表

struts.velocity.manager.classname
org.apache.struts2.views.velocity.VelocityManager implementation class
org.apache.struts2.views.velocity.VelocityManager接口名

struts.velocity.toolboxlocation
The location of the Velocity toolbox
velocity工具盒的位置
struts.xslt.nocache
Whether or not XSLT templates should not be cached
是否XSLT模版应该被缓存

【原创】struts2的struts.properties配置文件详解

struts.action.extension
The URL extension to use to determine if the request is meant for a Struts action
用URL扩展名来确定是否这个请求是被用作Struts action,其实也就是设置 action的后缀,例如login.do的'do'字。

struts.configuration
The org.apache.struts2.config.Configuration implementation class
org.apache.struts2.config.Configuration接口名

struts.configuration.files
A list of configuration files automatically loaded by Struts
struts自动加载的一个配置文件列表

struts.configuration.xml.reload
Whether to reload the XML configuration or not
是否加载xml配置(true,false)

struts.continuations.package
The package containing actions that use Rife continuations
含有actions的完整连续的package名称

struts.custom.i18n.resources
Location of additional localization properties files to load
加载附加的国际化属性文件(不包含.properties后缀)

struts.custom.properties
Location of additional configuration properties files to load
加载附加的配置文件的位置

struts.devMode
Whether Struts is in development mode or not
是否为struts开发模式

struts.dispatcher.parametersWorkaround
Whether to use a Servlet request parameter workaround necessary for some versions of WebLogic
(某些版本的weblogic专用)是否使用一个servlet请求参数工作区(PARAMETERSWORKAROUND)

struts.enable.DynamicMethodInvocation
Allows one to disable dynamic method invocation from the URL
允许动态方法调用

struts.freemarker.manager.classname
The org.apache.struts2.views.freemarker.FreemarkerManager implementation class
org.apache.struts2.views.freemarker.FreemarkerManager接口名

struts.i18n.encoding
The encoding to use for localization messages
国际化信息内码

struts.i18n.reload
Whether the localization messages should automatically be reloaded
是否国际化信息自动加载

struts.locale
The default locale for the Struts application
默认的国际化地区信息

struts.mapper.class
The org.apache.struts2.dispatcher.mapper.ActionMapper implementation class
org.apache.struts2.dispatcher.mapper.ActionMapper接口

struts.multipart.maxSize
The maximize size of a multipart request (file upload)
multipart请求信息的最大尺寸(文件上传用)

struts.multipart.parser
The org.apache.struts2.dispatcher.multipart.MultiPartRequest parser implementation for a multipart request (file upload)
专为multipart请求信息使用的 org.apache.struts2.dispatcher.multipart.MultiPartRequest解析器接口(文件上传用)

struts.multipart.saveDir
The directory to use for storing uploaded files
设置存储上传文件的目录夹

struts.objectFactory
The com.opensymphony.xwork2.ObjectFactory implementation class
com.opensymphony.xwork2.ObjectFactory接口(spring)

struts.objectFactory.spring.autoWire
Whether Spring should autoWire or not
是否自动绑定Spring

struts.objectFactory.spring.useClassCache
Whether Spring should use its class cache or not
是否spring应该使用自身的cache

struts.objectTypeDeterminer
The com.opensymphony.xwork2.util.ObjectTypeDeterminer implementation class
com.opensymphony.xwork2.util.ObjectTypeDeterminer接口

struts.serve.static.browserCache
If static content served by the Struts filter should set browser caching header properties or not
是否struts过滤器中提供的静态内容应该被浏览器缓存在头部属性中

struts.serve.static
Whether the Struts filter should serve static content or not
是否struts过滤器应该提供静态内容

struts.tag.altSyntax
Whether to use the alterative syntax for the tags or not
是否可以用替代的语法替代tags

struts.ui.templateDir
The directory containing UI templates
UI templates的目录夹

struts.ui.theme
The default UI template theme
默认的UI template主题

struts.url.http.port
The HTTP port used by Struts URLs
设置http端口

struts.url.https.port
The HTTPS port used by Struts URLs
设置https端口

struts.url.includeParams
The default includeParams method to generate Struts URLs
在url中产生 默认的includeParams

struts.velocity.configfile
The Velocity configuration file path
velocity配置文件路径

struts.velocity.contexts
List of Velocity context names
velocity的context列表

struts.velocity.manager.classname
org.apache.struts2.views.velocity.VelocityManager implementation class
org.apache.struts2.views.velocity.VelocityManager接口名

struts.velocity.toolboxlocation
The location of the Velocity toolbox
velocity工具盒的位置
struts.xslt.nocache
Whether or not XSLT templates should not be cached
是否XSLT模版应该被缓存

⑤ struts2中关于文件上传的配置

MIME类型。text/plain表示文本类型

⑥ struts2上传文件,我在struts.xml配置上传文件Form表单就不能提交数据

<input type="button" value=" 添加 " onclick="add()"/>

这个的js脚本是什么

⑦ struts2如何同时上传文件以及获得表单数据

事实上这根本不需要什么其他配置操作,因为这是Struts2,而不是原生Servlet,在Struts2中,拦截器会将request中的表单数据(或者文件格式的数据)都和action类中的属性名称一一对应的注入值(包括文件数据)。所以你需要做的,其实只是在jsp页面(或html)中加入一个file类型的input标签,名称记住(比如为photo),然后在action类中加一个File类型(java.io.File)字段,此字段必须和刚刚的input标签name属性一致,即photo(private File photo;)。最后,需要注意的是,当你妄图从网页上传一个文件类型的表单时,必须将包围它的form类将enctype="multipart/form-data" method="post"加上,即method必须为post,且enctype,也就是表单数据类型,必须为二进制的。

⑧ struts2 文件上传怎么指定保存文件的路径和大小

在action配置文件struts.xml里设置(如下):
<package name="upload" extends="struts-default">
<action name="upload" class="">
<!--配置fieldUpload拦截器--->
<interceptor-ref name="fileUpoad">
<param name="allowedTypes">image/bmp,image/png,image/jpg,image/gif</param>
<param name="maximumSize">200000</param>
</interceptor-ref>
<!---必须显示配置引用struts默认的拦截器栈:defaultStack----->
<interceptor name="defaultStack"></interceptor>
<!---设置上传路径----->
<param name="savePath">/upload</param>
<result name="success">/upload_succ.jsp</result>
<result name="input">/upload.jsp</result>
</action>
</package>
希望能帮到你哈....

⑨ struts2 如何动态限制文件上传大小

1 配置好struts的上传文件限制大小是不能修改的,这个是所有上传文件都必须接受的物理限制,当然,根据需要你可以把这个值设大点 2 用户可以配置的值必须在程序中控制,也就是说有你要保存一个配置项(例如:可以保存在数据库中),用户可以去修改这个,修改时更新数据库就行了。 3 用户上传文件时,需要判断文件大小是否超过数据库中这个配置项,超过了的话就不会进行后面的上传行为了,对用户返回错误信息。

热点内容
编译器按变量 发布:2024-10-07 15:07:03 浏览:770
怎么忘记电脑wifi密码怎么办 发布:2024-10-07 15:02:18 浏览:423
安卓开发java开发 发布:2024-10-07 15:01:29 浏览:90
工业级安卓主板价格怎么样 发布:2024-10-07 14:07:57 浏览:625
编程先乘除 发布:2024-10-07 13:58:45 浏览:268
编译内核时发生循环编译 发布:2024-10-07 13:58:43 浏览:494
当下笔记本电脑什么配置好 发布:2024-10-07 12:57:33 浏览:468
安卓倒车轨迹怎么调 发布:2024-10-07 12:54:47 浏览:913
问道刷道队伍怎么配置 发布:2024-10-07 12:09:17 浏览:325
androidservice使用 发布:2024-10-07 12:00:01 浏览:264