Struts(二十六):文件上传

  • 表单的准备

想要使用html表单上传一个或多个文件
1、须把html表单的enctype属性设置为multipart/form-data
2、须把html表单的method属性设置为post
3、须添加<input type="file">字段

  • Struts2的进行单文件上传需要操作步骤:

1、需要引入struts2需要的包,struts2上传是需要使用fileUpload拦截器实现的,而实际上上传文件是使用Commons FileUpload组建,所以需要导入commons-fileupload-1.3.2.jar、commons-io-2.2.jar包。

2、定制表单index.jsp

  <s:form action="testUpload" method="post" enctype="multipart/form-data"
theme="simple">
File:<s:file name="myFile"></s:file>
File Description:<s:textfield name="desc"></s:textfield>
<s:submit value="Submit"></s:submit>
</s:form>

3、基本的文件上传设置:直接在action类中定义如下三个属性,并实现getter和setter

private File myFile;
private String myFileContentType;
private String myFileFileName; public File getMyFile() {
return myFile;
} public void setMyFile(File myFile) {
this.myFile = myFile;
} public String getMyFileContentType() {
return myFileContentType;
} public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
} public String getMyFileFileName() {
return myFileFileName;
} public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}

备注:当查看fileUpload拦截器源码

 /*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/ package org.apache.struts2.interceptor; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.TextProvider;
import com.opensymphony.xwork2.TextProviderFactory;
import com.opensymphony.xwork2.ValidationAware;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.TextParseUtil;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
import org.apache.struts2.util.ContentTypeMatcher; import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set; /**
* <!-- START SNIPPET: description -->
* <p/>
* Interceptor that is based off of {@link MultiPartRequestWrapper}, which is automatically applied for any request that
* includes a file. It adds the following parameters, where [File Name] is the name given to the file uploaded by the
* HTML form:
* <p/>
* <ul>
* <p/>
* <li>[File Name] : File - the actual File</li>
* <p/>
* <li>[File Name]ContentType : String - the content type of the file</li>
* <p/>
* <li>[File Name]FileName : String - the actual name of the file uploaded (not the HTML name)</li>
* <p/>
* </ul>
* <p/>
* <p/> You can get access to these files by merely providing setters in your action that correspond to any of the three
* patterns above, such as setDocument(File document), setDocumentContentType(String contentType), etc.
* <br/>See the example code section.
* <p/>
* <p/> This interceptor will add several field errors, assuming that the action implements {@link ValidationAware}.
* These error messages are based on several i18n values stored in struts-messages.properties, a default i18n file
* processed for all i18n requests. You can override the text of these messages by providing text for the following
* keys:
* <p/>
* <ul>
* <p/>
* <li>struts.messages.error.uploading - a general error that occurs when the file could not be uploaded</li>
* <p/>
* <li>struts.messages.error.file.too.large - occurs when the uploaded file is too large</li>
* <p/>
* <li>struts.messages.error.content.type.not.allowed - occurs when the uploaded file does not match the expected
* content types specified</li>
* <p/>
* <li>struts.messages.error.file.extension.not.allowed - occurs when the uploaded file does not match the expected
* file extensions specified</li>
* <p/>
* </ul>
* <p/>
* <!-- END SNIPPET: description -->
* <p/>
* <p/> <u>Interceptor parameters:</u>
* <p/>
* <!-- START SNIPPET: parameters -->
* <p/>
* <ul>
* <p/>
* <li>maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set
* on the action. Note, this is <b>not</b> related to the various properties found in struts.properties.
* Default to approximately 2MB.</li>
* <p/>
* <li>allowedTypes (optional) - a comma separated list of content types (ie: text/html) that the interceptor will allow
* a file reference to be set on the action. If none is specified allow all types to be uploaded.</li>
* <p/>
* <li>allowedExtensions (optional) - a comma separated list of file extensions (ie: .html) that the interceptor will allow
* a file reference to be set on the action. If none is specified allow all extensions to be uploaded.</li>
* </ul>
* <p/>
* <p/>
* <!-- END SNIPPET: parameters -->
* <p/>
* <p/> <u>Extending the interceptor:</u>
* <p/>
* <p/>
* <p/>
* <!-- START SNIPPET: extending -->
* <p/>
* You can extend this interceptor and override the acceptFile method to provide more control over which files
* are supported and which are not.
* <p/>
* <!-- END SNIPPET: extending -->
* <p/>
* <p/> <u>Example code:</u>
* <p/>
* <pre>
* <!-- START SNIPPET: example-configuration -->
* &lt;action name="doUpload" class="com.example.UploadAction"&gt;
* &lt;interceptor-ref name="fileUpload"/&gt;
* &lt;interceptor-ref name="basicStack"/&gt;
* &lt;result name="success"&gt;good_result.jsp&lt;/result&gt;
* &lt;/action&gt;
* <!-- END SNIPPET: example-configuration -->
* </pre>
* <p/>
* <!-- START SNIPPET: multipart-note -->
* <p/>
* You must set the encoding to <code>multipart/form-data</code> in the form where the user selects the file to upload.
* <p/>
* <!-- END SNIPPET: multipart-note -->
* <p/>
* <pre>
* <!-- START SNIPPET: example-form -->
* &lt;s:form action="doUpload" method="post" enctype="multipart/form-data"&gt;
* &lt;s:file name="upload" label="File"/&gt;
* &lt;s:submit/&gt;
* &lt;/s:form&gt;
* <!-- END SNIPPET: example-form -->
* </pre>
* <p/>
* And then in your action code you'll have access to the File object if you provide setters according to the
* naming convention documented in the start.
* <p/>
* <pre>
* <!-- START SNIPPET: example-action -->
* package com.example;
*
* import java.io.File;
* import com.opensymphony.xwork2.ActionSupport;
*
* public UploadAction extends ActionSupport {
* private File file;
* private String contentType;
* private String filename;
*
* public void setUpload(File file) {
* this.file = file;
* }
*
* public void setUploadContentType(String contentType) {
* this.contentType = contentType;
* }
*
* public void setUploadFileName(String filename) {
* this.filename = filename;
* }
*
* public String execute() {
* //...
* return SUCCESS;
* }
* }
* <!-- END SNIPPET: example-action -->
* </pre>
*/
public class FileUploadInterceptor extends AbstractInterceptor { private static final long serialVersionUID = -4764627478894962478L; protected static final Logger LOG = LoggerFactory.getLogger(FileUploadInterceptor.class); protected Long maximumSize;
protected Set<String> allowedTypesSet = Collections.emptySet();
protected Set<String> allowedExtensionsSet = Collections.emptySet(); private ContentTypeMatcher matcher;
private Container container; @Inject
public void setMatcher(ContentTypeMatcher matcher) {
this.matcher = matcher;
} @Inject
public void setContainer(Container container) {
this.container = container;
} /**
* Sets the allowed extensions
*
* @param allowedExtensions A comma-delimited list of extensions
*/
public void setAllowedExtensions(String allowedExtensions) {
allowedExtensionsSet = TextParseUtil.commaDelimitedStringToSet(allowedExtensions);
} /**
* Sets the allowed mimetypes
*
* @param allowedTypes A comma-delimited list of types
*/
public void setAllowedTypes(String allowedTypes) {
allowedTypesSet = TextParseUtil.commaDelimitedStringToSet(allowedTypes);
} /**
* Sets the maximum size of an uploaded file
*
* @param maximumSize The maximum size in bytes
*/
public void setMaximumSize(Long maximumSize) {
this.maximumSize = maximumSize;
} /* (non-Javadoc)
* @see com.opensymphony.xwork2.interceptor.Interceptor#intercept(com.opensymphony.xwork2.ActionInvocation)
*/ public String intercept(ActionInvocation invocation) throws Exception {
ActionContext ac = invocation.getInvocationContext(); HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST); if (!(request instanceof MultiPartRequestWrapper)) {
if (LOG.isDebugEnabled()) {
ActionProxy proxy = invocation.getProxy();
LOG.debug(getTextMessage("struts.messages.bypass.request", new String[]{proxy.getNamespace(), proxy.getActionName()}));
} return invocation.invoke();
} ValidationAware validation = null; Object action = invocation.getAction(); if (action instanceof ValidationAware) {
validation = (ValidationAware) action;
} MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) request; if (multiWrapper.hasErrors()) {
for (String error : multiWrapper.getErrors()) {
if (validation != null) {
validation.addActionError(error);
}
}
} // bind allowed Files
Enumeration fileParameterNames = multiWrapper.getFileParameterNames();
while (fileParameterNames != null && fileParameterNames.hasMoreElements()) {
// get the value of this input tag
String inputName = (String) fileParameterNames.nextElement(); // get the content type
String[] contentType = multiWrapper.getContentTypes(inputName); if (isNonEmpty(contentType)) {
// get the name of the file from the input tag
String[] fileName = multiWrapper.getFileNames(inputName); if (isNonEmpty(fileName)) {
// get a File object for the uploaded File
File[] files = multiWrapper.getFiles(inputName);
if (files != null && files.length > 0) {
List<File> acceptedFiles = new ArrayList<File>(files.length);
List<String> acceptedContentTypes = new ArrayList<String>(files.length);
List<String> acceptedFileNames = new ArrayList<String>(files.length);
String contentTypeName = inputName + "ContentType";
String fileNameName = inputName + "FileName"; for (int index = 0; index < files.length; index++) {
if (acceptFile(action, files[index], fileName[index], contentType[index], inputName, validation)) {
acceptedFiles.add(files[index]);
acceptedContentTypes.add(contentType[index]);
acceptedFileNames.add(fileName[index]);
}
} if (!acceptedFiles.isEmpty()) {
Map<String, Object> params = ac.getParameters(); params.put(inputName, acceptedFiles.toArray(new File[acceptedFiles.size()]));
params.put(contentTypeName, acceptedContentTypes.toArray(new String[acceptedContentTypes.size()]));
params.put(fileNameName, acceptedFileNames.toArray(new String[acceptedFileNames.size()]));
}
}
} else {
if (LOG.isWarnEnabled()) {
LOG.warn(getTextMessage(action, "struts.messages.invalid.file", new String[]{inputName}));
}
}
} else {
if (LOG.isWarnEnabled()) {
LOG.warn(getTextMessage(action, "struts.messages.invalid.content.type", new String[]{inputName}));
}
}
} // invoke action
return invocation.invoke();
} /**
* Override for added functionality. Checks if the proposed file is acceptable based on contentType and size.
*
* @param action - uploading action for message retrieval.
* @param file - proposed upload file.
* @param contentType - contentType of the file.
* @param inputName - inputName of the file.
* @param validation - Non-null ValidationAware if the action implements ValidationAware, allowing for better
* logging.
* @return true if the proposed file is acceptable by contentType and size.
*/
protected boolean acceptFile(Object action, File file, String filename, String contentType, String inputName, ValidationAware validation) {
boolean fileIsAcceptable = false; // If it's null the upload failed
if (file == null) {
String errMsg = getTextMessage(action, "struts.messages.error.uploading", new String[]{inputName});
if (validation != null) {
validation.addFieldError(inputName, errMsg);
} if (LOG.isWarnEnabled()) {
LOG.warn(errMsg);
}
} else if (maximumSize != null && maximumSize < file.length()) {
String errMsg = getTextMessage(action, "struts.messages.error.file.too.large", new String[]{inputName, filename, file.getName(), "" + file.length(), getMaximumSizeStr(action)});
if (validation != null) {
validation.addFieldError(inputName, errMsg);
} if (LOG.isWarnEnabled()) {
LOG.warn(errMsg);
}
} else if ((!allowedTypesSet.isEmpty()) && (!containsItem(allowedTypesSet, contentType))) {
String errMsg = getTextMessage(action, "struts.messages.error.content.type.not.allowed", new String[]{inputName, filename, file.getName(), contentType});
if (validation != null) {
validation.addFieldError(inputName, errMsg);
} if (LOG.isWarnEnabled()) {
LOG.warn(errMsg);
}
} else if ((!allowedExtensionsSet.isEmpty()) && (!hasAllowedExtension(allowedExtensionsSet, filename))) {
String errMsg = getTextMessage(action, "struts.messages.error.file.extension.not.allowed", new String[]{inputName, filename, file.getName(), contentType});
if (validation != null) {
validation.addFieldError(inputName, errMsg);
} if (LOG.isWarnEnabled()) {
LOG.warn(errMsg);
}
} else {
fileIsAcceptable = true;
} return fileIsAcceptable;
} private String getMaximumSizeStr(Object action) {
return NumberFormat.getNumberInstance(getLocaleProvider(action).getLocale()).format(maximumSize);
} /**
* @param extensionCollection - Collection of extensions (all lowercase).
* @param filename - filename to check.
* @return true if the filename has an allowed extension, false otherwise.
*/
private boolean hasAllowedExtension(Collection<String> extensionCollection, String filename) {
if (filename == null) {
return false;
} String lowercaseFilename = filename.toLowerCase();
for (String extension : extensionCollection) {
if (lowercaseFilename.endsWith(extension)) {
return true;
}
} return false;
} /**
* @param itemCollection - Collection of string items (all lowercase).
* @param item - Item to search for.
* @return true if itemCollection contains the item, false otherwise.
*/
private boolean containsItem(Collection<String> itemCollection, String item) {
for (String pattern : itemCollection)
if (matchesWildcard(pattern, item))
return true;
return false;
} private boolean matchesWildcard(String pattern, String text) {
Object o = matcher.compilePattern(pattern);
return matcher.match(new HashMap<String, String>(), text, o);
} private boolean isNonEmpty(Object[] objArray) {
boolean result = false;
for (int index = 0; index < objArray.length && !result; index++) {
if (objArray[index] != null) {
result = true;
}
}
return result;
} protected String getTextMessage(String messageKey, String[] args) {
return getTextMessage(this, messageKey, args);
} protected String getTextMessage(Object action, String messageKey, String[] args) {
if (action instanceof TextProvider) {
return ((TextProvider) action).getText(messageKey, args);
}
return getTextProvider(action).getText(messageKey, args);
} private TextProvider getTextProvider(Object action) {
TextProviderFactory tpf = new TextProviderFactory();
if (container != null) {
container.inject(tpf);
}
LocaleProvider localeProvider = getLocaleProvider(action);
return tpf.createInstance(action.getClass(), localeProvider);
} private LocaleProvider getLocaleProvider(Object action) {
LocaleProvider localeProvider;
if (action instanceof LocaleProvider) {
localeProvider = (LocaleProvider) action;
} else {
localeProvider = container.getInstance(LocaleProvider.class);
}
return localeProvider;
} }

时,发现doc文件中备注信息:

  org.apache.struts2.interceptor.FileUploadInterceptor
Interceptor that is based off of MultiPartRequestWrapper, which is automatically applied for any request that includes a file.
It adds the following parameters, where [File Name] is the name given to the file uploaded by the HTML form:
•[File Name] : File - the actual File
•[File Name]ContentType : String - the content type of the file
•[File Name]FileName : String - the actual name of the file uploaded (not the HTML name)

4、使用io进行文件上传,需要在struts.xml定义action,并在对应的action中实现io上传

        <action name="testUpload" class="com.dx.struts2.actions.UploadAction">
<result>/success.jsp</result>
<result name="input">/index.jsp</result>
</action>

UploadAction.java(需要在WebContent下创建文件夹files)

package com.dx.struts2.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import javax.servlet.ServletContext; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L; private File myFile;
private String myFileContentType;
private String myFileFileName; public File getMyFile() {
return myFile;
} public void setMyFile(File myFile) {
this.myFile = myFile;
} public String getMyFileContentType() {
return myFileContentType;
} public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
} public String getMyFileFileName() {
return myFileFileName;
} public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
} @Override
public String execute() throws Exception {
System.out.println(myFile);
System.out.println(myFileContentType);
System.out.println(myFileFileName);
System.out.println("execute..."); ServletContext servletContext = ServletActionContext.getServletContext();
String savePath = servletContext.getRealPath("/files/" + myFileFileName);
System.out.println(savePath); FileOutputStream out = new FileOutputStream(savePath);
FileInputStream in = new FileInputStream(myFile); byte[] bytes = new byte[1024];
int length = 0; while ((length = in.read(bytes)) != -1) {
out.write(bytes, 0, length);
} out.close();
in.close(); return super.execute();
}
}
  • Struts2的进行多文件上传需要操作步骤:

1、form表单修改:(需要注意的是页面其他属性File Description name需要有下标,否则无法回填)

    <s:debug></s:debug>
<s:form action="testUpload" method="post" enctype="multipart/form-data"
theme="simple">
File:<s:file name="myFile"></s:file>
File Description:<s:textfield name="desc[0]"></s:textfield>
<br><br>
File:<s:file name="myFile"></s:file>
File Description:<s:textfield name="desc[1]"></s:textfield>
<s:submit value="Submit"></s:submit>
</s:form>

2、上传文件属性文件修改为List

package com.dx.struts2.actions;

import java.io.File;
import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L; private List<File> myFile;
private List<String> myFileContentType;
private List<String> myFileFileName; private List<String> desc; public List<File> getMyFile() {
return myFile;
}
public void setMyFile(List<File> myFile) {
this.myFile = myFile;
}
public List<String> getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(List<String> myFileContentType) {
this.myFileContentType = myFileContentType;
}
public List<String> getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(List<String> myFileFileName) {
this.myFileFileName = myFileFileName;
}
public List<String> getDesc() {
return desc;
}
public void setDesc(List<String> desc) {
this.desc = desc;
} @Override
public String execute() throws Exception {
System.out.println(myFile);
System.out.println(myFileContentType);
System.out.println(myFileFileName);
System.out.println(desc); System.out.println("execute..."); return super.execute();
}
}

3、提交页面后,后台打印信息:

[D:\Work\Java\apache-tomcat-7.0.-windows-x64\apache-tomcat-7.0.\work\Catalina\localhost\Struts_07FileUpload\upload_c2e431f2_8b1a_4aa7_a1d6_ae448d1dddb1_00000004.tmp, 
D:\Work\Java\apache-tomcat-7.0.-windows-x64\apache-tomcat-7.0.\work\Catalina\localhost\Struts_07FileUpload\upload_c2e431f2_8b1a_4aa7_a1d6_ae448d1dddb1_00000006.tmp]
[image/jpeg, image/jpeg]
[.jpg, .jpg]
[a, b]
execute...
  • 如何对上传文件的错误信息进行国际化

当我们查看fileUpload拦截器源代码时,会发现其doc文件中写着以下信息:

  This interceptor will add several field errors, assuming that the action implements ValidationAware. 
  These error messages are based on several i18n values stored in struts-messages.properties, a default i18n file processed for all i18n requests.
  You can override the text of these messages by providing text for the following keys: •struts.messages.error.uploading - a general error that occurs when the file could not be uploaded
•struts.messages.error.file.too.large - occurs when the uploaded file is too large
•struts.messages.error.content.type.not.allowed - occurs when the uploaded file does not match the expected content types specified
•struts.messages.error.file.extension.not.allowed - occurs when the uploaded file does not match the expected file extensions specified

因此,可以在src下创建i18n.properties文件,并在文件内容定义:

struts.messages.error.uploading=""
struts.messages.error.file.too.large=""
struts.messages.error.content.type.not.allowed=""
struts.messages.error.file.extension.not.allowed=""

在struts.xml中定义国际化设置

<constant name="struts.custom.i18n.resources" value="i18n"></constant>

还可以参考struts-core.jar下的org.apache.struts2包下的struts-messages.properties文件中的message信息:

struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=File {0} is too large to be uploaded. Maximum allowed size is {4} bytes!
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}
  • 如何对上传文件限定,大小、文件内容类型、扩展名以及总文件大小显示修改

当我们查看fileUpload拦截器源代码时,会发现其doc文件中写着以下信息:

  Interceptor parameters:
•maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set on the action. Note, this is not related to the various properties found in struts.properties. Default to approximately 2MB.
•allowedTypes (optional) - a comma separated list of content types (ie: text/html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all types to be uploaded.
•allowedExtensions (optional) - a comma separated list of file extensions (ie: .html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all extensions to be uploaded. Extending the interceptor:
You can extend this interceptor and override the acceptFile method to provide more control over which files are supported and which are not. Example code:
<action name="doUpload" class="com.example.UploadAction">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.jsp</result>
</action>

我们可以通过重写fileUpload拦截器的maximumSize 、allowedTypes 、allowedExtensions 三个字段来设置参数,具体操作修改struts.xml

     <interceptors>
<interceptor-stack name="myInterceptorStack">
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">2000</param>
<param name="fileUpload.allowedTypes">text/html,text/xml</param>
<param name="fileUpload.allowedExtensions">html,dtd,xml</param>
</interceptor-ref>
</interceptor-stack>
</interceptors> <default-interceptor-ref name="myInterceptorStack"></default-interceptor-ref>

重写总文件大小限定,总文件大小设置在struts-core.jar下的org.apache.struts2包下的default.properties文件中:

struts.multipart.maxSize=2097152

可以通过在struts.xml中定义常量来覆盖:

<constant name="struts.multipart.maxSize" value="20971520"></constant>
上一篇:要开始学习C#


下一篇:java:ssh连接服务器,实现本地文件上传和下载