struts2实现多文件上传的示例代码
开发环境JDK1.8eclipsestruts2-2.3.31
1.创建web项目
2.导入struts2核心jar包
3.更改web.xml配置文件(只要配置好struts2的Filter就好)
4.创建src/struts.xml文件
WEB-INF/images /success.jsp /error.jsp image/bmp,image/png,image/gif,image/jpeg 1025956
5.创建src/com.ifan.action.FileUpload.Java
packagecom.ifan.action;
importjava.io.File;
importorg.apache.commons.io.FileUtils;
importorg.apache.struts2.ServletActionContext;
importcom.opensymphony.xwork2.ActionContext;
importcom.opensymphony.xwork2.ActionSupport;
publicclassFileUploadextendsActionSupport{
privateFile[]image;//上传的文件
privateString[]imageFileName;//文件名称
privateString[]imageContentType;//文件类型
publicStringexecute()throwsException{
ServletActionContext.getRequest().setCharacterEncoding("UTF-8");
Stringrealpath=ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realpath);
if(image!=null){
Filesavedir=newFile(realpath);
if(!savedir.getParentFile().exists())
savedir.getParentFile().mkdirs();
for(inti=0;i
6.创建WebContent/index.jsp,作为上传文件的页面
<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%@taglibprefix="s"uri="/struts-tags"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()
+path+"/";
%>
"rel="externalnofollow">
MyJSP'hello.jsp'startingpage
7.创建WebContent/success.jsp作为文件上传成功跳转的页面,创建WebContent/error.jsp作为文件上传失败的页面,创建WebContent/images文件夹,作为上传文件的存储位置
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。