1. 页面使用freemerk技术设计
?test.ftl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>文件上传操作</title> <script> function addFiles(){ var filesDiv = document.getElementById("files"); var fileInput = document.createElement("input"); fileInput.type="file"; fileInput.name="files"; var delButton = document.createElement("input"); delButton.type="button"; delButton.value="删除"; delButton.onclick=function del(){ this.parentNode.parentNode.removeChild(this.parentNode); } var div = document.createElement("div"); div.appendChild(fileInput); div.appendChild(delButton); filesDiv.appendChild(div); } function upLoad(){ document.form.action = "upLoad.action"; document.form.submit(); } function down(){ document.form.action = "downloadList.action"; document.form.submit(); } </script> </head> <body> <form id="form" name="form" enctype="multipart/form-data" method="post"> <table> <tr><td><input type="button" value="添加文件" onclick="addFiles();"/></td></tr> <tr><td><input type="file" name="files"/></td><td></td></tr> <tr><td><div id="files"></div></td><td></td></tr> <tr><td><input type="button" onclick="upLoad();" value="文件上传"/></td></tr> </table> </form> </body> </html>
?2.web.xml核心配置 注意使用最新的filter
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- <display-name>excel</display-name> <welcome-file-list> <welcome-file>user.jsp</welcome-file> </welcome-file-list> --> <!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> --> <!-- <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> --> <filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
?3.action
package com.daizhaoyun.action; import java.io.File; /*import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream;*/ import java.io.IOException; /*import java.io.InputStream; import java.io.OutputStream;*/ import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; @SuppressWarnings("serial") public class FileUpAction extends ActionSupport { private List<File> files; private List<String> filesFileName; private List<String> filesContentType; public List<File> getFiles() { return files; } public void setFiles(List<File> files) { this.files = files; } public List<String> getFilesFileName() { return filesFileName; } public void setFilesFileName(List<String> filesFileName) { this.filesFileName = filesFileName; } public List<String> getFilesContentType() { return filesContentType; } public void setFilesContentType(List<String> filesContentType) { this.filesContentType = filesContentType; } public String inter() { return SUCCESS; } /** * 批量上传文件 */ public String upLoad() { /*if (files != null) { int i = 0; for (; i < files.size(); i++) { try { InputStream is = new FileInputStream(files.get(i)); String path = ServletActionContext.getServletContext().getRealPath("/upload/"); //下一级目录 OutputStream os = new FileOutputStream(path+File.separator+filesFileName.get(i)); byte buffer[] = new byte[8192]; int count = 0; while ((count = is.read(buffer)) > 0) { os.write(buffer, 0, count); } os.close(); is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }*/ String path = ServletActionContext.getServletContext().getRealPath("/upload/"); if(files!=null){ File savedir = new File(path); if(!savedir.exists()){ savedir.mkdirs(); } for(int i=0;i<files.size();i++){ File saveFile = new File(savedir,filesFileName.get(i)); try { FileUtils.copyFile(files.get(i), saveFile); } catch (IOException e) { e.printStackTrace(); } } } return SUCCESS; } }?
4.struts核心配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/> --> <constant name="struts.multipart.saveDir" value="/tmp"></constant> <constant name="struts.i18n.encoding" value="utf-8"></constant> <constant name="struts.multipart.maxSize" value="209715200" /> <package name="file" extends="struts-default" namespace="/"> <action name="inter" class="com.daizhaoyun.action.FileUpAction" method="inter"> <result name="success" type="freemarker"> /WEB-INF/test.ftl </result> </action> <action name="upLoad" class="com.daizhaoyun.action.FileUpAction" method="upLoad"> <result name="success" type="freemarker"> /WEB-INF/test.ftl </result> </action> </package> </struts>
?5.访问路径 以inter.action进行访问