Struts2实现多文件上传
2012-02-06 16:31
267 查看
和单文件上传一样,多文件上传只不过是更改一下表单和Action代码而已,前面的东西不再累述,配置什么的都是一
样的,关键就是在Action中,针对的File必须写成数组形式或者说是List形式也是可以的。下面直接看一下代码
可以看到,除了下面的对文件的遍历和建立目录是不一样的,其他的收拾没问题的,然后再看一下表单的实现,
需要几个文件弄一个就行了,其实很简单。
样的,关键就是在Action中,针对的File必须写成数组形式或者说是List形式也是可以的。下面直接看一下代码
package com.bird.action; import java.io.File; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; public class FileUpload { private File[] image;//获取上传文件 private String[] imageFileName;//获取上传文件名称 private String[] imageContentType;//获取上传文件类型 public File[] getImage() { return image; } public void setImage(File[] image) { this.image = image; } public String[] getImageFileName() { return imageFileName; } public void setImageFileName(String[] imageFileName) { this.imageFileName = imageFileName; } public String[] getImageContentType() { return imageContentType; } public void setImageContentType(String[] imageContentType) { this.imageContentType = imageContentType; } public String execute() throws Exception{ String path = ServletActionContext.getServletContext().getRealPath("/images"); System.out.println(path); if(image != null){ File savedir = new File(path); if(!savedir.exists()) savedir.mkdirs(); for(int i = 0; i < image.length; i++){ File saveFile = new File(savedir,imageFileName[i]); FileUtils.copyFile(image[i], saveFile); } } return "success"; } }
可以看到,除了下面的对文件的遍历和建立目录是不一样的,其他的收拾没问题的,然后再看一下表单的实现,
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'fileUpload.jsp' starting page</title> </head> <body> <form action="${pageContext.request.contextPath }/test/fileupload.action" enctype="multipart/form-data" method="post"> 选择文件1:<input type="file" name="image"> 选择文件2:<input type="file" name="image"> 选择文件3:<input type="file" name="image"> <input type="submit" value="上传"> </form> </body> </html>
需要几个文件弄一个就行了,其实很简单。
相关文章推荐
- struts2实现文件上传(一)
- Struts2实现文件批量上传
- Struts2 之 实现文件上传(多文件)和下载
- Struts2实现文件上传报错(四)
- Struts2实现文件上传(二)
- Struts2实现单文件的上传功能实例详解及源代码
- Struts2:实现多个文件同时上传代码
- Struts2实现文件上传
- struts2实现上传下载(单文件上传与多文件上传的比较)
- struts2实现文件上传
- 周报8.15-8.21 struts2实现文件上传和下载(下载)
- Struts2实现文件上传(默认拦截器篇)
- struts2文件上传的实现
- Struts2轻松实现多文件上传(自定义多线程加速程序效率)
- Struts2的一个和多个文件上传的实现
- struts2实现单文件上传
- struts2中的文件上传实现
- ajaxFileUpload+struts2实现多文件上传(动态添加文件上传框)
- struts2实现多文件的上传
- Struts2实现单文件的上传功能实例详解及源代码