struts2中的多文件上传的处理操作方法。
在sturts2中的多个文件的上传处理,真是做到足够简单了,想以前阿堂都是在Servlet中用smartUpload.jar来进行手工处理二进制流,来多个文件的上传处理的。。struts2是在common-FileUpload和COS的文件上传解析器的基础上,再次进行了封装..,不能不说这是struts2的高明之处。。不过,struts2黙认使用的jakarta的Common-FileUpload的文件上传框架,如果需要使用struts2的文件上传功能,则需要在web应用中增加两个JAR文件,即commons-io.jar和commons-fileupload.jar文件。(如果需要使用其它的文件上传处理的框架,只需要在struts.xml文件中,配置struts.multipart.parser常量的值就可以了)
对于struts2中的多个文件的上传处理,可以有两种方式来处理,既可以用Array数组,也可以用List方式,基本上差不多,只是使用List<File>代替了File[]的操作.对于struts2而言,使用泛型限制的List与数组的作用几乎相同。因此strtus2可以将多个文件域的内容解析成三个List对象,一个用于封装多个文件,一个用于封装文件类型,一个用于封装多个文件内容
一.用Array数组方式来实现多个文件的上传
1.上传页面文件代码upload.jsp
<%@ page language=”java” contentType=”text/html; charset=GBK”%>
<%@taglib prefix=”s” uri=”/struts-tags”%>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=GBK” />
<title>使用数组上传多个文件</title>
</head>
<body>
<s:fielderror/>
<form action=”upload.action” method=”post” enctype=”multipart/form-data”>
文件标题:<input type=”text” name=”title” /><br>
选择第一个文件:<input type=”file” name=”upload” /><br>
选择第二个文件:<input type=”file” name=”upload” /><br>
选择第三个文件:<input type=”file” name=”upload” /><br>
<input value=”上传” type=”submit” />
</form>
</body>
</html>
2.显示结果页面代码succ.jsp
<%@ page language=”java” contentType=”text/html; charset=GBK”%>
<%@taglib prefix=”s” uri=”/struts-tags”%>
<html>
<head>
<title>上传成功</title>
</head>
<body>
上传成功!<br>
文件标题:<s:property value=”title”/><br>
第一个文件为:<img src=”<s:property value=”‘upload/’ + uploadFileName[0]“/>”/><br>
第二个文件为:<img src=”<s:property value=”‘upload/’ + uploadFileName[1]“/>”/><br>
第三个文件为:<img src=”<s:property value=”‘upload/’ + uploadFileName[2]“/>”/><br>
</body>
</html>
3.处理的UploadAction.java
package com.ish.struts.action;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
private String title;
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
{
this.savePath = value;
}
private String getSavePath() throws Exception
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) {
this.title = title;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getTitle() {
return (this.title);
}
public File[] getUpload() {
return (this.upload);
}
public String[] getUploadContentType() {
return (this.uploadContentType);
}
public String[] getUploadFileName() {
return (this.uploadFileName);
}
@Override
public String execute() throws Exception
{
File[] files = getUpload();
for (int i = 0 ; i < files.length ; i++)
{
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + “\\” + getUploadFileName()[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);
}
}
return SUCCESS;
}
}
4.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>
<constant name=”struts.custom.i18n.resources” value=”globalMessages”/>
<constant name=”struts.i18n.encoding” value=”GBK”/>
<constant name=”struts.multipart.saveDir” value=”/temp” />
<package name=”upload” extends=”struts-default”>
<action name=”upload” class=”lee.UploadAction”>
<interceptor-ref name=”fileUpload”>
<param name=”allowedTypes”>image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param>
</interceptor-ref>
<interceptor-ref name=”defaultStack”/>
<param name=”savePath”>/upload</param>
<result name=”input”>/upload.jsp</result>
<result>/succ.jsp</result>
</action>
</package>
</struts>
二。List处理多个文件上传时的Action,其余的和Array的内容基本一致,就省略了
package com.ish.strtus.action;
import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.util.*;
import java.io.*;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport
{
private String title;
private List<File> upload;
private List<String> uploadContentType;
private List<String> uploadFileName;
//接受依赖注入的属性
private String savePath;
//接受依赖注入的方法
public void setSavePath(String value)
{
this.savePath = value;
}
private String getSavePath() throws Exception
{
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setTitle(String title) {
this.title = title;
}
public void setUpload(List<File> upload) {
this.upload = upload;
}
public void setUploadContentType(List<String> uploadContentType) {
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(List<String> uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getTitle() {
return (this.title);
}
public List<File> getUpload() {
return (this.upload);
}
public List<String> getUploadContentType() {
return (this.uploadContentType);
}
public List<String> getUploadFileName() {
return (this.uploadFileName);
}
@Override
public String execute() throws Exception
{
List<File> files = getUpload();
for (int i = 0 ; i < files.size() ; i++)
{
//以服务器的文件保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath() + “\\” + getUploadFileName().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);
}
}
return SUCCESS;
}
}
http://blog.sina.com.cn/s/blog_4c925dca0100gfjc.html