struts2实现多文件上传的示例代码

网友投稿 206 2023-06-03


struts2实现多文件上传的示例代码

开发环境JDK1.8 eclipse struts2-2.3.31

1.创建web项目

2.导入struts2核心jar包

3.更改web.xml配置文件(只要配置好struts2的Filter就好)

4.创建src/struts.xml文件

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

WEB-INF/images

/success.jsp

/error.jsp

1025956

5.创建src/com.ifan.action.FileUpload.Java

package com.ifan.action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContmiALLQxext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpload extends ActionSupport{

private File[] image; //上传的文件

private String[] imageFileName; //文件名称

private String[] imageContentType; //文件类型

public String execute(http://) 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;

}

}

6.创建WebContent/index.jsp ,作为上传文件的页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%>

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;

}

}

6.创建WebContent/index.jsp ,作为上传文件的页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%>

7.创建WebContent/success.jsp 作为文件上传成功跳转的页面,创建WebContent/error.jsp 作为文件上传失败的页面 , 创建WebContent/images文件夹,作为上传文件的存储位置


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java 字符串反转实现代码
下一篇:JAVA简单实现MD5注册登录加密实例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~