1、下载uploadify-v3.1.zip
官网URL:http://www.uploadify.com/
2、jquery-1.7.1.js
官网URL:http://jquery.com/download/
3、struts1.2
4、index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<%@ 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> <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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <link rel="stylesheet" type="text/css" href="<%=basePath%>js/uploadify/uploadify.css" /> <script type="text/javascript" src="<%=basePath%>js/jquery/jquery-1.7.1.js"></script> <script type="text/javascript" src="<%=basePath%>js/uploadify/jquery.uploadify-3.1.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $(function() { $("#file_upload").uploadify({ swf : '<%=basePath%>js/uploadify/uploadify.swf', uploader : 'uploadAction.do', cancelimg : '<%=basePath%>js/uploadify/uploadify-cancel.png', height : 30, width : 100, buttonText : '浏览', auto : false }); }); function close(){ window.close(); } }); </script> </head> <body> <input type="file" name="file_upload" id="file_upload" /> <a href="javascript:$('#file_upload').uploadify('upload','*')" >保存</a> </body> </html> |
5、UploadActon.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.mxzhe.struts.action; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Iterator; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.DefaultFileItemFactory; import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction; public class UploadAction extends DispatchAction { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String filePath = "D:\\uploadif"; try { DefaultFileItemFactory dff = new DefaultFileItemFactory(); DiskFileUpload upload = new DiskFileUpload(dff); upload.setHeaderEncoding("UTF-8"); List<FileItem> fileList = null; fileList = upload.parseRequest(request); Iterator<FileItem> it = fileList.iterator(); while (it.hasNext()) { FileItem item = it.next(); if (!item.isFormField()) { // 创建目录 File directory = new File(filePath); if (!directory.exists()) { directory.mkdirs(); } System.out.println(filePath+File.separator+item.getFieldName()); OutputStream os = new FileOutputStream(filePath+File.separator+item.getName()); int bytesResd = 0; byte[] buffer = new byte[1024]; InputStream is = item.getInputStream(); while ((bytesResd = is.read(buffer, 0, 1024)) != -1) { os.write(buffer, 0, bytesResd); os.flush(); } os.close(); } } } catch (FileUploadException ex) { return null; } return null; } } |
6、struts-config.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?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 /> <global-exceptions /> <global-forwards /> <action-mappings> <action type="com.mxzhe.struts.action.UploadAction" path="/uploadAction" scope="request" validate="false" /> </action-mappings> <message-resources parameter="com.mxzhe.struts.ApplicationResources" /> </struts-config> |
7、web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name /> <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> |