当前位置:编程学习 > JSP >>

Struts2上传多文件实现程序

Struts2上传多文件也很简单。在Action中把文件对应的属性用数组或者集合接收就可以了。

 代码如下 复制代码

File[] file; 

String[] fileFileName;

String[] fileContentType;

1、FileUploadAction的代码

package cn.luxh.struts2.action;

 

import java.io.File;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

 

import org.apache.commons.io.FileUtils;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.apache.struts2.ServletActionContext;

 

import com.opensymphony.xwork2.ActionSupport;

 

 

/**

* 上传文件

* @author Luxh

*/

public class FileUploadAction extends ActionSupport {

 

private static final long serialVersionUID = 2642160699551232611L;

 

private static Log LOG = LogFactory.getLog(FileUploadAction.class);

 

protected File[] file;

protected String[] fileFileName;

protected String[] fileContentType;

 

 

/**

* 上传文件

*/

public String upload() {

try {

File[] files = uploadFile("/upload");

//获得files后

//do some work

//...

}catch(Exception e) {

LOG.error("上传文件出错!");

throw new RuntimeException("上传文件出错");

}

return SUCCESS;

}

 

/**

* 处理上传的文件

* @param saveDir

* @return 

* @throws IOException 

*/

public File[] uploadFile(String saveDir) throws IOException {

if(saveDir==null ||"".equals(saveDir.trim())) {

saveDir = "/upload";

}

File[] files = null;

if(file != null && file.length > 0) {

String saveDirPath = ServletActionContext.getServletContext().getRealPath(saveDir);

File dirFile = new File(saveDirPath);

if(!dirFile.exists()) {

dirFile.mkdir();

}

files = new File[file.length];

for(int i=0;i<file.length;i++) {

if(file[i] != null) {

String newFileName = generateFileName(fileFileName[i]);

File destFile = new File(saveDirPath,newFileName);

FileUtils.copyFile(file[i], destFile);

}

}

}

return files;

 

/**

* 生成文件名

* @param fileName

* @return

*/

private String generateFileName(String fileName) {

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

String formatDate = sdf.format(new Date());

int position = fileName.lastIndexOf("."); 

String extension = "";

if(position!=-1) {

extension = fileName.substring(position); 

}

return formatDate + extension; 

}

 

public File[] getFile() {

return file;

}

public void setFile(File[] file) {

this.file = file;

}

public String[] getFileFileName() {

return fileFileName;

}

public void setFileFileName(String[] fileFileName) {

this.fileFileName = fileFileName;

}

public String[] getFileContentType() {

return fileContentType;

}

public void setFileContentType(String[] fileContentType) {

this.fileContentType = fileContentType;

}

 

}


2、上传页面upload.jsp

 

<form action="${pageContext.request.contextPath}/fileUpload/upload" method="post" enctype="multipart/form-data">

<table>

<tr>

<td>

请选择文件:

</td>

</tr>

<tr>

<td>

<input type="file" name="file">

</td>

</tr>

<tr>

<td>

<input type="file" name="file">

</td>

</tr>

<tr>

<td>

<input type="file" name="file">

</td>

</tr>

</table>

<tr>

<td>

<input type="submit" value="Submit" id="submit">

</td>

</tr>

</form>


3、配置文件struts.xml

 代码如下 复制代码

<action name="upload" class="cn.luxh.struts2.action.FileUploadAction" method="upload">

<result>/WEB-INF/pages/common/success.jsp</result>

</action>

补充:Jsp教程,Java技巧及代码
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,