struts2使用数组同时上传多个文件

一月 15th, 2008 Traffic(931)

大家可能都知道struts2了,他的正式版发布的时间不常,所以本人也是刚刚学习了一下,如有不对的地方请指点,我在看struts2书时发现在以下这些代码,我觉得不错,所以拿出来和大家一起分享.

首先是上传的页面"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>使用List上传多个文件</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>

下面让我们来看一下对应的Action

package lee;

import com.opensymphony.xwork2.Action;

import org.apache.struts2.ServletActionContext;

import java.util.*;

import java.io.*;

import com.opensymphony.xwork2.ActionSupport;

/**

* #author yeeku.H.lee kongyeeku#163.com

* #version 1.0

* <br>Copyright (C), 2005-2008, yeeku.H.Lee

* <br>This program is protected by copyright laws.

* <br>Program Name:

* <br>Date:

*/

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;

}

}

大家可以看见用STRUTS2实现多文件上传是如此的方便.

下面让我们来看一下他的配置文件

<?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"/>

<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>

值得一提的是文件上传的路径可以写在配置文件中,这样在以后如果像更换路径,只要改一下配置文件即可.就不必去改程序了,必境写好的程序在改来改去还是不好的.

以上是一个非常完整的代码段,大家可以复制下来,改动一个,即可使用.

Posted by: 唐朝

Comments(0)

暂无留言。
User Name:
(必添)
User Email:
(必添)
User URL:
Comment:
(必添)

他的介绍

唐朝
唐朝中国沈阳Contact

工作职能

java/jsp

简介

...
View Detail

他的回复

(2)
View All ReTopic

他的模板

(0)
Browse All Templates

友情链接

(1)
View All Links