一:所要的包
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
freemarker-2.3.8.jar
jakarta.jar
ognl-2.6.11.jar
struts2-core-2.0.8.jar
xwork-2.0.4.jar
二:上传文件jsp页面 FileUploadMany.jsp
<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>
三:struts.xml配置文件
<action name="upload" class="com.yysoft.uploadfile.FileUploadActionMany">
<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">/images</param>
<result name="input">/FileUploadMany.jsp</result>
<result>/uploadsuccessMany.jsp</result>
</action>
四:FileUploadActionMany.java action处理类 --一种方法,通过String数组来现实
package com.yysoft.uploadfile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadActionMany extends ActionSupport {
private String title;
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
private static final int BUFFER_SIZE = 16 * 1024 ;
// 接受依赖注入的属性
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);
}
public String execute() throws Exception{
File[] files = getUpload();
FileOutputStream fos=null;
FileInputStream fis=null;
System.out.println("files:"+files);
for (int i = 0 ; i < files.length ; i++){
// 以服务器的文件保存地址和原文件名建立上传文件输出流
System.out.println("getSavePath():"+getSavePath());
System.out.println("getUploadFileName().get(i):"+getUploadFileName()[i]);
System.out.println("files.get(i):"+files[i]);
System.out.println("uploadContentType[i]"+uploadContentType[i]);
fos= new FileOutputStream(getSavePath() + "//" + getUploadFileName()[i]);
fis= new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0){
fos.write(buffer , 0 , len);
}
fos.close();
fos=null;
fis.close();
fis=null;
}
return SUCCESS;
}
}
五LFileUploadActionMany1.java 另一种方法,action处理类,通过List集合来操作
package com.yysoft.uploadfile;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadActionMany1 extends ActionSupport {
private static final int BUFFER_SIZE = 16 * 1024 ;
private List <File> uploads = new ArrayList <File> ();
private List <String> uploadFileNames = new ArrayList <String>();
private List <String> uploadContentTypes = new ArrayList <String>();
public List<File> getUploads() {
return uploads;
}
public void setUploads(List<File> uploads) {
this.uploads = uploads;
}
public List<String> getUploadsFileName(){
return this.uploadFileNames;
}
public void setUploadsFileName(List<String> uploadFileName){
this.uploadFileNames = uploadFileName;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf( "." );
return fileName.substring(pos);
}
@Override
public String execute(){
System.out.println("uploads"+uploads.size());
System.out.println("uploadFileNames"+uploadFileNames.size());
for(int i=0;i<uploads.size();i++){
System.out.println("上传来了....................");
System.out.println("myFile======="+uploads.get(i));
System.out.println("fileName========="+uploadFileNames.get(i));
String imageFileName = new Date().getTime()+ i + getExtention(uploadFileNames.get(i));
System.out.println("imageFileName:"+imageFileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/images" ) + "/" + imageFileName);
System.out.println(uploads.get(i));
System.out.println(imageFile);
copy(uploads.get(i), imageFile);
}
return SUCCESS;
}
}
commons-fileupload-1.2.1.jar
commons-io-1.4.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
freemarker-2.3.8.jar
jakarta.jar
ognl-2.6.11.jar
struts2-core-2.0.8.jar
xwork-2.0.4.jar
二:上传文件jsp页面 FileUploadMany.jsp
<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>
三:struts.xml配置文件
<action name="upload" class="com.yysoft.uploadfile.FileUploadActionMany">
<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">/images</param>
<result name="input">/FileUploadMany.jsp</result>
<result>/uploadsuccessMany.jsp</result>
</action>
四:FileUploadActionMany.java action处理类 --一种方法,通过String数组来现实
package com.yysoft.uploadfile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadActionMany extends ActionSupport {
private String title;
private File[] upload;
private String[] uploadContentType;
private String[] uploadFileName;
private static final int BUFFER_SIZE = 16 * 1024 ;
// 接受依赖注入的属性
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);
}
public String execute() throws Exception{
File[] files = getUpload();
FileOutputStream fos=null;
FileInputStream fis=null;
System.out.println("files:"+files);
for (int i = 0 ; i < files.length ; i++){
// 以服务器的文件保存地址和原文件名建立上传文件输出流
System.out.println("getSavePath():"+getSavePath());
System.out.println("getUploadFileName().get(i):"+getUploadFileName()[i]);
System.out.println("files.get(i):"+files[i]);
System.out.println("uploadContentType[i]"+uploadContentType[i]);
fos= new FileOutputStream(getSavePath() + "//" + getUploadFileName()[i]);
fis= new FileInputStream(files[i]);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0){
fos.write(buffer , 0 , len);
}
fos.close();
fos=null;
fis.close();
fis=null;
}
return SUCCESS;
}
}
五LFileUploadActionMany1.java 另一种方法,action处理类,通过List集合来操作
package com.yysoft.uploadfile;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadActionMany1 extends ActionSupport {
private static final int BUFFER_SIZE = 16 * 1024 ;
private List <File> uploads = new ArrayList <File> ();
private List <String> uploadFileNames = new ArrayList <String>();
private List <String> uploadContentTypes = new ArrayList <String>();
public List<File> getUploads() {
return uploads;
}
public void setUploads(List<File> uploads) {
this.uploads = uploads;
}
public List<String> getUploadsFileName(){
return this.uploadFileNames;
}
public void setUploadsFileName(List<String> uploadFileName){
this.uploadFileNames = uploadFileName;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf( "." );
return fileName.substring(pos);
}
@Override
public String execute(){
System.out.println("uploads"+uploads.size());
System.out.println("uploadFileNames"+uploadFileNames.size());
for(int i=0;i<uploads.size();i++){
System.out.println("上传来了....................");
System.out.println("myFile======="+uploads.get(i));
System.out.println("fileName========="+uploadFileNames.get(i));
String imageFileName = new Date().getTime()+ i + getExtention(uploadFileNames.get(i));
System.out.println("imageFileName:"+imageFileName);
File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/images" ) + "/" + imageFileName);
System.out.println(uploads.get(i));
System.out.println(imageFile);
copy(uploads.get(i), imageFile);
}
return SUCCESS;
}
}
发表评论
-
jquery动态调用struts中的action
2011-08-25 14:02 847------------------------------- ... -
Queue队列实例
2011-08-25 13:57 876import java.util.Queue; impor ... -
struts2.0多文件上传(我自己用,好用,中文也可以上传)
2011-08-16 20:33 825这个页面是点一个添加就出一个浏览上传的 <%@ pag ... -
Struts2.0 文件上传与下载全解析
2011-08-16 20:31 735struts的上传封装的已经非常完美了,首先我们来看一下页面 ... -
jquery数据列表(需要json.jar)
2011-08-15 23:17 1150public Map getDsrPwdList(String ... -
copy文件
2011-08-15 22:23 687import java.io.BufferedInputStr ...
相关推荐
01 为Struts 2.0做好准备 02 常用的Struts 2.0的标志(Tag) 03 Struts 2.0的Action讲解...09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新表单标志的使用 13 Struts 2与AJAX
struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0struts2.0
Struts2.0 实现文件上传进度 原理和实现 主要是实现Struts2.0的监听器
Struts 2.0系列(MAX),pdf格式,全方位介绍struts2: 常用的Struts 2.0的标志(Tag)介绍 ...在Struts 2中实现文件上传 在Struts 2中实现CRUD Struts 2中的OGNL Strus 2的新表单标志的使用 Struts 2与AJAX
struts2.0上传文件 struts2.0上传文件 struts2.0上传文件 struts2.0上传文件
Struts2.0实现文件上传例子
sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0sstruts2.0 struts2.0
Struts2.0视频教程,struts2.0中文教程,Struts2.0视频教程,struts2.0中文教程,
使用struts2.0进行多文件同时上传
struts2.0jar包 struts2.0包 struts2.0源文件
struts2.0的数据校验框架struts2.0的数据校验框架struts2.0的数据校验框架struts2.0的数据校验框架
struts 2.0 详细配置 struts 2.0 详细配置 struts 2.0 详细配置
struts2.0源代码(有关于struts2.0实现上传与下载和如何操作数据库的源代码),非常有用
清晰的介绍了Struts 2.0框架的工作流程,Action线程安全,程序入口,配置文件。
JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0JavaEE源代码 Struts2.0...
struts2.0实现的数据分页实现及实现分页步骤,
此为Struts2.0最新Jar包,方便各位用于Struts2.0的开发.
功能强大的Struts2.0+Extjs实现的文件上传,包含文件上传进度,文件的下载,压缩,解压多文件删除等
非常好的 struts2.0 jar
为Struts 2.0做好准备 Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的应用。作为最成功的Web框架,Struts自然拥有众多的优点: MVC 2模型的使用 功能齐全的标志库(Tag Library) 开放...