最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
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;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;
}
}
struts.xml配置文件
上传表单页面upload.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
文件上传
显示页面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">
文件:
显示结果页面
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21