網路上很多的例子雖然說是3.1版本的,但是調用方法都是老版本的,跑不起來,經過查看doc,下面的例子可以在tomcat中正常運行。
index.jsp
<%@ page language="java" contentType="text/html; charset=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>檔案上傳</title> <link href="css/uploadify.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="scripts/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="scripts/jquery.uploadify-3.1.min.js"></script><script type="text/javascript">$(document).ready(function() { $("#uploadify").uploadify({ 'auto' : false, 'swf' : '<%=path%>/scripts/uploadify.swf', 'uploader' : '<%=path%>/scripts/uploadify',//幕後處理的請求 'queueID' : 'fileQueue',//與下面的id對應 'queueSizeLimit' :1, 'fileTypeDesc' : 'rar檔案或zip檔案', 'fileTypeExts' : '*.rar;*.zip', //控制可上傳檔案的副檔名,啟用本項時需同時聲明fileDesc 'multi' : true, 'buttonText' : '上傳' });});</script> </head><body> <div id="fileQueue"></div> <input type="file" name="uploadify" id="uploadify" /> <p> <a href="javascript:$('#uploadify').uploadify('upload')">開始上傳</a> <a href="javascript:$('#uploadify').uplaodify('cancel','*')">取消上傳</a> </p> </body></html>
servlet: Uploadify.java
package com.rh.core.upload;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Iterator;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.disk.DiskFileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;import org.apache.commons.fileupload.util.Streams;public class Uploadify extends HttpServlet{private static final long serialVersionUID = 1L; /** * 實現多檔案的同時上傳 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設定接收的編碼格式 request.setCharacterEncoding("UTF-8"); Date date = new Date();//擷取目前時間 SimpleDateFormat sdfFileName = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat sdfFolder = new SimpleDateFormat("yyMM"); String newfileName = sdfFileName.format(date);//檔案名稱 String fileRealPath = "";//檔案存放真真實位址 String fileRealResistPath = "";//檔案存放真實相對路徑 //名稱 介面編碼 必須 和request 儲存一致..否則亂碼 String name = request.getParameter("name"); String firstFileName=""; // 獲得容器中上傳檔案夾所在的實體路徑 String savePath = this.getServletConfig().getServletContext().getRealPath("/") + "uploads\\" + newfileName +"\\"; System.out.println("路徑" + savePath+"; name:"+name); File file = new File(savePath); if (!file.isDirectory()) { file.mkdirs(); } try { DiskFileItemFactory fac = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(fac); upload.setHeaderEncoding("UTF-8"); // 擷取多個上傳檔案 List fileList = fileList = upload.parseRequest(request); // 遍曆上傳檔案寫入磁碟 Iterator it = fileList.iterator(); while (it.hasNext()) { Object obit = it.next(); if(obit instanceof DiskFileItem){ System.out.println("xxxxxxxxxxxxx"); DiskFileItem item = (DiskFileItem) obit; // 如果item是檔案上傳表單域 // 獲得檔案名稱及路徑 String fileName = item.getName(); if (fileName != null) { firstFileName=item.getName().substring(item.getName().lastIndexOf("\\")+1); String formatName = firstFileName.substring(firstFileName.lastIndexOf("."));//擷取檔案尾碼名 fileRealPath = savePath + newfileName+ formatName;//檔案存放真真實位址 BufferedInputStream in = new BufferedInputStream(item.getInputStream());// 獲得檔案輸入資料流 BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(fileRealPath)));// 獲得檔案輸出資料流 Streams.copy(in, outStream, true);// 開始把檔案寫到你指定的上傳檔案夾 //上傳成功,則插入資料庫 if (new File(fileRealPath).exists()) { //虛擬路徑賦值 fileRealResistPath=sdfFolder.format(date)+"/"+fileRealPath.substring(fileRealPath.lastIndexOf("\\")+1); //儲存到資料庫 System.out.println("儲存到資料庫:"); System.out.println("name:"+name); System.out.println("虛擬路徑:"+fileRealResistPath); } } } } } catch (org.apache.commons.fileupload.FileUploadException ex) { ex.printStackTrace(); System.out.println("沒有上傳檔案"); return; } response.getWriter().write("1"); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet> <servlet-name>Uploadify</servlet-name> <servlet-class>com.rh.core.upload.Uploadify</servlet-class> </servlet> <servlet-mapping> <servlet-name>Uploadify</servlet-name> <url-pattern>/scripts/uploadify</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
項目:(可能需要修改css檔案中圖片的位置)