这两天看了一下struts2.0的上传,实现一个简单的例子很简单,我是从strtus2.0自带的一个例子看的,也没有经过修改就可以实现上传了,但下载还是出现了一些问题。最开始接触的时候出现了一个问题,也就是包的问题或者说是配置的问题,在web.xml里面仅仅是配置了struts的映射,导致每次启动的时候说找不着一个东西,然后又发现包存在问题,因为使用的是myEclipse,在里面选的加入自定义的一个userLibery库,貌似这样是不行,以前也碰到这种情况,那个时候还不清楚,像这一次,我把那几个需要的包直接添加进去之后就可以运行了。其实也有可能触发此问题的是和其它的一些包起冲突,没有去证明这个推断的正确性。
下面就说一下怎么实现这上传的功能吧。
首先是把包给加入进去,参考了一下网上的例子,因此就只加入了一下几个包
xwork-2.04.jar
commons-loggin-1.0.4.jar
ognl-2.6.11.jar
struts2-core-1.011.jar
commons-fileupload-1.1.1.jar
commons-io-1.1.jar
web.xml的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
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">
<!-- START SNIPPET: xworkSample -->
<struts>
<include file="struts-fileupload.xml" />
</struts>
struts-fileupload.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>
<package name="fileupload" extends="struts-default" namespace="/fileupload">
<action name="upload" class="com.struts.fileupload.FileUploadAction" method="input">
<result>upload.jsp</result>
</action>
<action name="doUpload" class="com.struts.fileupload.FileUploadAction" method="upload">
<result name="input">upload.jsp</result>
<result>upload-success.jsp</result>
</action>
<action name="multipleUploadUsingList">
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型,多个用","分隔 -->
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg
,image/x-png, image/pjpeg
</param>
<!-- 配置允许上传的文件大小,单位字节 -->
<param name="maximumSize">1024000000</param>
</interceptor-ref>
<param name="savePath">/temp</param>
<result>multipleUploadUsingList.jsp</result>
</action>
<action name="doMultipleUploadUsingList" class="com.struts.fileupload.MultipleFileUploadUsingListAction" method="upload">
<result>multipleUploadUsingList-success.jsp</result>
</action>
<action name="multipleUploadUsingArray">
<result>multipleUploadUsingArray.jsp</result>
</action>
<action name="doMultipleUploadUsingArray" class="com.struts.fileupload.MultipleFileUploadUsingArrayAction" method="upload">
<result>multipleUploadUsingArray-success.jsp</result>
</action>
</package>
</struts>
fileUploadAction.java如下:
package com.struts.fileupload;
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
/**
* Show case File Upload example's action. <code>FileUploadAction</code>
*
*/
public class FileUploadAction extends ActionSupport {
private static final long serialVersionUID = 5156288255337069381L;
private String contentType;
private File upload;
private String fileName;
private String caption;
// since we are using <s:file name="upload" .../> the file name will be
// obtained through getter/setter of <file-tag-name>FileName
public String getUploadFileName() {
return fileName;
}
public void setUploadFileName(String fileName) {
this.fileName = fileName;
}
// since we are using <s:file name="upload" ... /> the content type will be
// obtained through getter/setter of <file-tag-name>ContentType
public String getUploadContentType() {
return contentType;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}
// since we are using <s:file name="upload" ... /> the File itself will be
// obtained through getter/setter of <file-tag-name>
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public String input() throws Exception {
return SUCCESS;
}
public String upload() throws Exception {
return SUCCESS;
}
}
upload.jsp如下:
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Showcase</title>
</head>
<body>
<h1>Fileupload sample</h1>
<s:actionerror />
<s:fielderror />
<s:form action="doUpload" method="POST" enctype="multipart/form-data">
<s:fielderror></s:fielderror>
<s:file name="upload" label="File"/>
<s:textfield name="caption" label="Caption"/>
<s:submit />
</s:form>
</body>
</html>
upload-success.jsp如下:
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Showcase</title>
</head>
<body>
<h1>Fileupload sample</h1>
<ul>
<li>ContentType: <s:property value="uploadContentType" /></li>
<li>FileName: <s:property value="uploadFileName" /></li>
<li>File: <s:property value="upload" /></li>
<li>Caption:<s:property value="caption" /></li>
</ul>
</body>
</html>
这样单一的上传文件就好了。
下面是多文件上传的代码,MultipleFileUploadUsingListAction.java代码如下:
package com.struts.fileupload;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
/**
* Showcase action - multiple file upload using List
* @version $Date: 2006-11-23 12:31:52 -0500 (Thu, 23 Nov 2006) $ $Id: MultipleFileUploadUsingListAction.java 478625 2006-11-23 17:31:52Z wsmoak $
*/
public class MultipleFileUploadUsingListAction extends ActionSupport {
private List<File> uploads = new ArrayList<File>();
private List<String> uploadFileNames = new ArrayList<String>();
private List<String> uploadContentTypes = new ArrayList<String>();
public List<File> getUpload() {
return this.uploads;
}
public void setUpload(List<File> uploads) {
this.uploads = uploads;
}
public List<String> getUploadFileName() {
return this.uploadFileNames;
}
public void setUploadFileName(List<String> uploadFileNames) {
this.uploadFileNames = uploadFileNames;
}
public List<String> getUploadContentType() {
return this.uploadContentTypes;
}
public void setUploadContentType(List<String> contentTypes) {
this.uploadContentTypes = contentTypes;
}
public String upload() throws Exception {
System.out.println("\n\n upload1");
System.out.println("files:");
for (File u: uploads) {
System.out.println("*** "+u+"\t"+u.length());
}
System.out.println("filenames:");
for (String n: uploadFileNames) {
System.out.println("*** "+n);
}
System.out.println("content types:");
for (String c: uploadContentTypes) {
System.out.println("*** "+c);
}
System.out.println("\n\n");
return SUCCESS;
}
}
multipleFileUploadUsingList.jsp如下:
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Showcase - fileupload - multiple fileupload using List</title>
</head>
<body>
<s:form action="doMultipleUploadUsingList" method="POST" enctype="multipart/form-data">
<s:file label="File (1)" name="upload" />
<s:file label="File (2)" name="upload" />
<s:file label="FIle (3)" name="upload" />
<s:submit />
</s:form>
</body>
</html>
multipleFileUploadUsingList-success.jsp如下:
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<table border="1">
<s:iterator value="upload" status="stat">
<tr>
<td>File <s:property value="%{#stat.index}" /></td>
<td><s:property value="%{upload[#stat.index]}" /></td>
</tr>
</s:iterator>
</table>
<table border="1">
<s:iterator value="uploadFileName" status="stat">
<tr>
<td>File Name <s:property value="%{#stat.index}" /></td>
<td><s:property value="%{uploadFileName[#stat.index]}" /></td>
</tr>
</s:iterator>
</table>
<table border="1">
<s:iterator value="uploadContentType" status="stat">
<tr>
<td>Content Type <s:property value="%{#stat.index}" /></td>
<td><s:property value="%{uploadContentType[#stat.index]}" /></td>
</tr>
</s:iterator>
</table>
</body>
</html>
这样就可以了。
现在还存在着一些问题,不知道怎么去限制文件上传类型,确定上传目录,限制上传的大小这个还不知道怎么设置,在相应的action里面做如下配置就是不行,也不知道为啥。
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型,多个用","分隔 -->
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg
,image/x-png, image/pjpeg
</param>
<!-- 配置允许上传的文件大小,单位字节 -->
<param name="maximumSize">1024000000</param>
</interceptor-ref>
不知道是不是和tomcat6有关。