文档库 最新最全的文档下载
当前位置:文档库 › struts2文件上传

struts2文件上传

1.需要的jar包、 commons-io-1.3.2.jar
HelloWorldAction.java
public class HelloWorldAction{
private String[] image; //获取上传的文件
private String[] imageFileName; //得到上传文件的名称

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 addUI() {
return "success";
}


public String execute() throws Exception{
// images/
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]); //文件保存目录
//得到它的目录
if(!savefile.getParentFile().exists) savefile.getParentFile().mkdirs();
FileUtils.copyFile(image[i],savefile);
}
ActionContext.getContext().put("message","上传成功!");
}
return "success";
}
}


struts.xml 配置

"-//Apache Software Foundation//DTD Configuration 2.0//EN"
"https://www.wendangku.net/doc/6616829099.html,/dtds/struts-2.0.dtd">







method="{1}">
/WEB-INF/page/message.jsp






message.jsp


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>






My JSP 'message.jsp' starting page











${message }




employee.jsp

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





My JSP 'employeeAdd.j<br></p><!--/p1--><!--p2--><p>sp' starting page











action="${pageContext.request.contextPath}/control/employee/list_execute.action"
method="post">

文件1:



文件2:



文件3:








web.xml 配置


xmlns="https://www.wendangku.net/doc/6616829099.html,/xml/ns/javaee"
xmlns:xsi="https://www.wendangku.net/doc/6616829099.html,/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.wendangku.net/doc/6616829099.html,/xml/ns/javaee
https://www.wendangku.net/doc/6616829099.html,/xml/ns/javaee/web-app_2_5.xsd">


struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter



struts2
/*




index.jsp






相关文档
相关文档 最新文档