struts2上传文件及多文件上传struts2上传文件及多文件上传
1. struts2中的文件上传
第一步:在WEB=INF/lib下加入commons-fileupload-1.2.1.jar , commons-io-1.3.2.jar。
第二步:把form表单的enctype属性设置为";multipart/form-data";,如
Java代码
;
文件:;
;
;
//${pageContext.request.contextPath}:获取服务器根路径
第三步:在action中添加一下属性,
Java代码
public...
struts2上传文件及多文件上传
1. struts2中的文件上传
第一步:在WEB=INF/lib下加入commons-fileupload-1.2.1.jar , commons-io-1.3.2.jar。
第二步:把form
单的enctype属性设置为";multipart/form-data";,如
Java代码
<;form
action=";${pageContext.request.contextPath}/control/employee/list_execute.action
"; enctype=";multipart/form-data"; method=";post";>;
文件:<;input type=";file"; name=";image";>;
<;input type=";submit"; value=";上传";/>;
<;/form>;
//${pageContext.request.contextPath}:获取服务器根路径
第三步:在action中添加一下属性,
Java代码
public class HelloWorldAction {
private File image; //与jsp表单中的名称对应
private String imageFileName; //FileName为固定格式
private String imageContentType ;//ContentType为固定格式
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;
}
public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String execute() throws Exception{
System.out.println(";imageFileName = ";+imageFileName);
System.out.println(";imageContentType = ";+imageContentType);
//获取服务器的根路径realpath
String realpath =
ServletActionContext.getServletContext().getRealPath(";/images";);
System.out.println(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";, ";上传成
功";);
}else{
ActionContext.getContext().put(";message";, ";上传失
败";);
}
return ";success";;
}
}
此外,可以在struts.xml中配置上传文件的大小
<;constant name=";struts.multipart.maxSize"; value=";10701096";/>; //最大
上传配置成10M
默认的上传大小为2M
思维拓展:如果要上传的文件非常大,如上传的是电影,好几百M ,用web上传一般是不可
能难上传成功的,这时候要安装一个插件,类似于应用程序
socket ,通过网络通讯上传。
2 . 多文件上传
在上面的基础上略加改动
1.jsp表单
Java代码
<;form
action=";${pageContext.request.contextPath}/control/employee/list_execute.action
"; enctype=";multipart/form-data"; method=";post";>;
文件1:<;input type=";file"; name=";image";>;<;br/>;
文件2:<;input type=";file"; name=";image";>;<;br/>;
文件3:<;input type=";file"; name=";image";>;<;br/>;
<;input type=";submit"; value=";上传";/>;
<;/form>;
2. action中用数组接收
Java代码
public class HelloWorldAction {
private File[] image;
private String[] imageFileName;
private String[] imageContentType ;
//省略了set和get
public String execute() throws Exception{
String realpath =
ServletActionContext.getServletContext().getRealPath(";/images";);
System.out.println(realpath);
if(image!=null){
File savedir = new File(realpath);
if(!savedir.exists())
{
savedir.mkdirs();
}
System.out.println(";image.length = ";+image.length);
for(int i = 0 ; i<;image.length ; i++){
System.out.println(";imageContentType[";+i+";] = ";+imageContentType[i]);
File savefile = new File(savedir, imageFileName[i]);
FileUtils.copyFile(image[i], savefile);
}
ActionContext.getContext().put(";message";, ";上传成
功";);
}
return ";success";;
}
}
本文档为【struts2上传文件及多文件上传】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。