package com.xishui.struts.commons; import java.io.*; public class UploadUtil { private static final String DATABASE_DEST = "database"; private static final String FILE_DEST = "file"; /** * 定义上传文件的大小 */ private static final int MAX_SIZE = 10240 * 10240; /** * 定义上传文件的类型 */ private static final String[] TYPES = { ".3pg", ".avi", ".flv", ".mpg" }; public static void saveFile(String fileName, byte[] fileData, int size, String dest) throws FileNotFoundException, IOException { if (!checkSize(size)) { throw new IOException(size + " 太大了 !"); } if (!checkType(fileName)) { throw new IOException("未定义的文件类型 !"); } if (dest.equals(DATABASE_DEST)) { saveToDb(fileName, fileData); } if (dest.equals(FILE_DEST)) { saveToFile(fileName, fileData); } } private static void saveToDb(String fileName, byte[] fileData) { } private static void saveToFile(String fileName, byte[] fileData) throws FileNotFoundException, IOException { OutputStream opt = new FileOutputStream(fileName); opt.write(fileData); opt.close(); } public static void delFile(String fileName, String dest) throws NullPointerException, SecurityException { if (dest.equals(DATABASE_DEST)) { delFromDb(fileName); } if (dest.equals(FILE_DEST)) { delFromFile(fileName); } } private static void delFromDb(String fileName) { } private static void delFromFile(String fileName) throws NullPointerException, SecurityException { File file = new File(fileName); if (file.exists()) file.delete(); } private static boolean checkSize(int size) { if (size > MAX_SIZE) return false; return true; } private static boolean checkType(String fileName) { for (int i = 0; i < TYPES.length; i++) { if (fileName.toLowerCase().endsWith(TYPES[i])) { return true; } } return false; } } ------ package com.xishui.struts.upload; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public abstract class BaseAction extends Action { protected static final String SAVE = "save"; protected static final String DELETE = "delete"; protected static final String DATABASE_DEST = "database"; protected static final String FILE_DEST = "file"; public abstract ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception; public String getPath(String filePath){ String path = getServlet().getServletContext().getRealPath(filePath) + "//"; return path; } } ------ /** * org.apache.struts.upload.FormFile是Struts处理文件上传的核心组件,提供了获得 * 上传文件元数据的方法。 */ package com.xishui.struts.upload; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; import com.xishui.struts.commons.UploadUtil; public class UploadAction extends BaseAction { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { UploadForm uploadForm = (UploadForm) form; FormFile file = uploadForm.getTheFile(); String contentType = file.getContentType(); String fileName = file.getFileName(); int fileSize = file.getFileSize(); byte[] fileDate = file.getFileData(); String command = request.getParameter("command"); if (command.equals(SAVE)) { UploadUtil.saveFile(getPath("fileUpload") + fileName , fileDate, fileSize, FILE_DEST); } if (command.equals(DELETE)) { UploadUtil.delFile(fileName, FILE_DEST); } file.destroy(); return mapping.findForward("success"); } } ------- package com.xishui.struts.upload; import org.apache.struts.action.ActionForm; import org.apache.struts.upload.FormFile; public class UploadForm extends ActionForm { private FormFile theFile; public FormFile getTheFile() { return theFile; } public void setTheFile(FormFile theFile) { this.theFile = theFile; } } ------ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans> <form-bean name="uploadForm" type="com.xishui.struts.upload.UploadForm" /> </form-beans> <global-exceptions /> <global-forwards /> <action-mappings> <action attribute="uploadForm" input="/upload/upload.jsp" name="uploadForm" path="/upload" scope="request" type="com.xishui.struts.upload.UploadAction"> <forward name="success" path="/upload/success.jsp" /> </action> </action-mappings> <message-resources parameter="com.xishui.struts.ApplicationResources" /> </struts-config> ------ <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> ------ <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page language="java"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <html> <head> <title>文件上传</title> </head> <body> <html:form action="/upload" method="post" enctype="multipart/form-data"> 文件路径 : <html:file property="theFile"/> <html:errors property="theFile"/> <html:submit/> <html:cancel/> <html:hidden property="command" value="save"/> </html:form> </body> </html> ------ <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <mce:script language="javascript"><!-- setTimeout("location.href='Upload.jsp';", 1000); // --></mce:script> </head> <body> This is my JSP page. <br> </body> </html> ------<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page language="java"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html:html locale="true"> <head> <html:base /> <title>success.jsp</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <center> <h3> 文件上传成功 </h3> <hr> </body> </html:html> 发布的wepapp-->项目下建立一个目录fileUpload,放上传后的文件