一聚教程网:一个值得你收藏的教程网站

热门教程

struts2之单个与多个文件上传代码

时间:2022-06-29 02:40:19 编辑:袖梨 来源:一聚教程网

通过3种方式模拟多个文件上传,效果如下所示
         
目录结构
 
新建action
第一种方式
package com.ljq.action;

import java.io.file;

import org.apache.commons.io.fileutils;
import org.apache.struts2.servletactioncontext;

import com.opensymphony.xwork2.actioncontext;
import com.opensymphony.xwork2.actionsupport;

@suppresswarnings("serial")
public class uploadaction extends actionsupport{

private file[] image; //上传的文件
private string[] imagefilename; //文件名称
private string[] imagecontenttype; //文件类型

public string execute() throws exception {
servletactioncontext.getrequest().setcharacterencoding("utf-8");
string realpath = servletactioncontext.getservletcontext().getrealpath("/images");
system.out.println(realpath);
if (image != null) {
file savedir=new file(realpath);
if(!savedir.getparentfile().exists())
savedir.getparentfile().mkdirs();
for(int i=0;i file savefile = new file(savedir, imagefilename[i]);
fileutils.copyfile(image[i], savefile);
}
actioncontext.getcontext().put("message", "文件上传成功");
}
return "success";
}

public file[] getimage() {
return image;
}

public void setimage(file[] image) {
this.image = image;
}

public string[] getimagecontenttype() {
return imagecontenttype;
}

public void setimagecontenttype(string[] imagecontenttype) {
this.imagecontenttype = imagecontenttype;
}

public string[] getimagefilename() {
return imagefilename;
}

public void setimagefilename(string[] imagefilename) {
this.imagefilename = imagefilename;
}




}
                   
第二种方式
package com.ljq.action;

import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;

import org.apache.struts2.servletactioncontext;

import com.opensymphony.xwork2.actionsupport;
/**
* 使用数组上传多个文件
*
* @author ljq
*
*/
@suppresswarnings("serial")
public class uploadaction2 extends actionsupport{
private file[] image; //上传的文件
private string[] imagefilename; //文件名称
private string[] imagecontenttype; //文件类型
private string savepath;

@override
public string execute() throws exception {
servletactioncontext.getrequest().setcharacterencoding("utf-8");
//取得需要上传的文件数组
file[] files = getimage();
if (files !=null && files.length > 0) {
for (int i = 0; i < files.length; i++) {
//建立上传文件的输出流, getimagefilename()[i]
system.out.println(getsavepath() + "" + getimagefilename()[i]);
fileoutputstream fos = new fileoutputstream(getsavepath() + "" + getimagefilename()[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);
}
fos.close();
fis.close();
}
}
return success;
}

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

/**
* 返回上传文件保存的位置
*
* @return
* @throws exception
*/
public string getsavepath() throws exception {
return servletactioncontext.getservletcontext().getrealpath(savepath);
}

public void setsavepath(string savepath) {
this.savepath = savepath;
}


}
            
第三种方式
package com.ljq.action;

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;

/**
* 使用list上传多个文件
*
* @author ljq
*
*/
@suppresswarnings("serial")
public class uploadaction3 extends actionsupport {
private list image; // 上传的文件
private list imagefilename; // 文件名称
private list imagecontenttype; // 文件类型
private string savepath;

@override
public string execute() throws exception {
servletactioncontext.getrequest().setcharacterencoding("utf-8");
// 取得需要上传的文件数组
list files = getimage();
if (files != null && files.size() > 0) {
for (int i = 0; i < files.size(); i++) {
fileoutputstream fos = new fileoutputstream(getsavepath() + "" + getimagefilename().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);
}
fis.close();
fos.close();
}
}
return success;
}

public list getimage() {
return image;
}

public void setimage(list image) {
this.image = image;
}

public list getimagefilename() {
return imagefilename;
}

public void setimagefilename(list imagefilename) {
this.imagefilename = imagefilename;
}

public list getimagecontenttype() {
return imagecontenttype;
}

public void setimagecontenttype(list imagecontenttype) {
this.imagecontenttype = imagecontenttype;
}

/**
* 返回上传文件保存的位置
*
* @return
* @throws exception
*/
public string getsavepath() throws exception {
return servletactioncontext.getservletcontext().getrealpath(savepath);
}

public void setsavepath(string savepath) {
this.savepath = savepath;
}

}
              
struts.xml配置文件

"-//apache software foundation//dtd struts configuration 2.0//en"
"http://struts.apache.org/dtds/struts-2.0.dtd">



















/web-inf/page/message.jsp教程





/image
/web-inf/page/message.jsp





/image
/web-inf/page/message.jsp


           
上传表单页面upload.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>




文件上传












文件1:

文件2:

文件3:




显示页面message.jsp

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>




my jsp 'message.jsp' starting page







上传成功




 

单文件上传

过2种方式模拟单个文件上传,效果如下所示

开发步骤如下:
1、新建一个web工程,导入struts2上传文件所需jar,如下图
目录结构
            
2、新建action 
第一种方式
package com.ljq.action;

import java.io.file;

import org.apache.commons.io.fileutils;
import org.apache.struts2.servletactioncontext;

import com.opensymphony.xwork2.actioncontext;
import com.opensymphony.xwork2.actionsupport;

@suppresswarnings("serial")
public class uploadaction extends actionsupport{

private file image; //上传的文件
private string imagefilename; //文件名称
private string imagecontenttype; //文件类型

public string execute() throws exception {
string realpath = servletactioncontext.getservletcontext().getrealpath("/images");
//d:apache-tomcat-6.0.18webapps教程struts2_uploadimages
system.out.println("realpath: "+realpath);
if (image != null) {
file savefile = new file(new file(realpath), imagefilename);
if (!savefile.getparentfile().exists())
savefile.getparentfile().mkdirs();
fileutils.copyfile(image, savefile);
actioncontext.getcontext().put("message", "文件上传成功");
}
return "success";
}

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


}
            
第二种方式
package com.ljq.action;

import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;

import org.apache.struts2.servletactioncontext;

import com.opensymphony.xwork2.actionsupport;

@suppresswarnings("serial")
public class uploadaction2 extends actionsupport {

// 封装上传文件域的属性
private file image;
// 封装上传文件类型的属性
private string imagecontenttype;
// 封装上传文件名的属性
private string imagefilename;
// 接受依赖注入的属性
private string savepath;

@override
public string execute() {
fileoutputstream fos = null;
fileinputstream fis = null;
try {
// 建立文件输出流
system.out.println(getsavepath());
fos = new fileoutputstream(getsavepath() + "" + getimagefilename());
// 建立文件上传流
fis = new fileinputstream(getimage());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} catch (exception e) {
system.out.println("文件上传失败");
e.printstacktrace();
} finally {
close(fos, fis);
}
return success;
}

/**
* 返回上传文件的保存位置
*
* @return
*/
public string getsavepath() throws exception{
return servletactioncontext.getservletcontext().getrealpath(savepath);
}

public void setsavepath(string savepath) {
this.savepath = savepath;
}

public file getimage() {
return image;
}

public void setimage(file image) {
this.image = image;
}

public string getimagecontenttype() {
return imagecontenttype;
}

public void setimagecontenttype(string imagecontenttype) {
this.imagecontenttype = imagecontenttype;
}

public string getimagefilename() {
return imagefilename;
}

public void setimagefilename(string imagefilename) {
this.imagefilename = imagefilename;
}

private void close(fileoutputstream fos, fileinputstream fis) {
if (fis != null) {
try {
fis.close();
} catch (ioexception e) {
system.out.println("fileinputstream关闭失败");
e.printstacktrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (ioexception e) {
system.out.println("fileoutputstream关闭失败");
e.printstacktrace();
}
}
}

}
              
struts.xml配置文件

"-//apache software foundation//dtd struts configuration 2.0//en"
"http://struts.apache.org/dtds/struts-2.0.dtd">























/web-inf/page/message.jsp






/images
/web-inf/page/message.jsp
/upload/upload.jsp


image/bmp,image/png,image/gif,image/jpeg

1025956





           
上传表单页面
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>




文件上传









enctype="multipart/form-data" method="post">
文件:






           
显示结果页面
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>




上传成功







上传成功!




">


热门栏目