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;ifile 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 listimage; // 上传的文件
private listimagefilename; // 文件名称
private listimagecontenttype; // 文件类型
private string savepath;
@override
public string execute() throws exception {
servletactioncontext.getrequest().setcharacterencoding("utf-8");
// 取得需要上传的文件数组
listfiles = 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 listgetimage() {
return image;
}
public void setimage(listimage) {
this.image = image;
}
public listgetimagefilename() {
return imagefilename;
}
public void setimagefilename(listimagefilename) {
this.imagefilename = imagefilename;
}
public listgetimagecontenttype() {
return imagecontenttype;
}
public void setimagecontenttype(listimagecontenttype) {
this.imagecontenttype = imagecontenttype;
}
/**
* 返回上传文件保存的位置
*
* @return
* @throws exception
*/
public string getsavepath() throws exception {
return servletactioncontext.getservletcontext().getrealpath(savepath);
}
public void setsavepath(string savepath) {
this.savepath = savepath;
}
}
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
文件上传
<%@ 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">
文件:
显示结果页面
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级