當前位置:首頁 » 文件管理 » 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-05-18 22:22:24 瀏覽:428
安卓換蘋果sim卡去哪裡換 發布:2024-05-18 22:21:29 瀏覽:636
順豐薪資默認密碼是多少 發布:2024-05-18 22:15:04 瀏覽:790
瀏覽器打不開伺服器通信怎麼辦 發布:2024-05-18 21:32:22 瀏覽:961
創建存儲空間 發布:2024-05-18 21:20:57 瀏覽:122
sql日期和時間 發布:2024-05-18 21:16:19 瀏覽:143
安卓網頁怎麼截取 發布:2024-05-18 20:53:56 瀏覽:972
在配置更新的時候沒電關機怎麼辦 發布:2024-05-18 20:36:10 瀏覽:928
win7訪問win2000 發布:2024-05-18 20:27:41 瀏覽:389
青島人社局密碼多少 發布:2024-05-18 20:19:10 瀏覽:735