后台代码用servlet,这样不依赖框架什么情况,都可以用。
web.xml添加
<!-- swfupload 上传 begin --> <servlet> <servlet-name>SWFUploader</servlet-name> <servlet-class>sunfish.upload.SWFUploadServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SWFUploader</servlet-name> <url-pattern>/upload/SWFUploader</url-pattern> </servlet-mapping> <!-- swfupload 上传 end -->
java代码如下:
package sunfish.upload; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.lang.StringUtils; public class SWFUploadServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String relativePath = request.getParameter("relativePath"); if (StringUtils.isEmpty(relativePath)) relativePath = "upload"; // 设定上传文件路径 String currentPath = relativePath + "/"; // 获得web应用的上传路径 String currentDirPath = getServletContext().getRealPath(currentPath); System.out.println("currentDirPath=" + currentDirPath); // 判断文件夹是否存在,不存在则创建 File dirTest = new File(currentDirPath); if (!dirTest.exists()) { dirTest.mkdirs(); } boolean status = false; String msg = ""; // 使用Apache Common组件中的fileupload进行文件上传 FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); String newFileName = null; try { String fileName = null; FileItem uplFile = null; List<FileItem> itemList = upload.parseRequest(request); // Iterator iter = items.iterator(); System.out .println("enctype=multipart/form-data 参数解析 begin----------"); for (FileItem item : itemList) { // FileItem item = (FileItem) iter.next(); if (item.isFormField()) { // 这里Filename和下面的Filedata都是swflupoad设置的默认设置,够无聊的话你可以去修改 // 另外不同的浏览器传递的Filename可能不同,或全路径,或只是文件名称 if (item.getFieldName().equals("Filename")) { fileName = item.getString(); } } else { if (item.getFieldName().equals("Filedata")) { uplFile = (FileItem) item; } } System.out.println(item.getFieldName() + "=" + item); }// end of for System.out .println("enctype=multipart/form-data 参数解析 end----------"); // CEKditor中file域的name值是upload // 获取文件名(无扩展名) newFileName = createNewFileName(fileName); File pathToSave = new File(currentDirPath, newFileName); // 如果文件名相同,则重写且名字 int counter = 1; while (pathToSave.exists()) { if (counter == 10) { throw new IOException("名称重复:" + counter); } newFileName = createNewFileName(fileName); pathToSave = new File(currentDirPath, newFileName); counter++; } uplFile.write(pathToSave); status = true; } catch (Exception ex) { ex.printStackTrace(); throw new IOException(ex.getMessage()); } // 以Jsong格式为输出信息 // response.setContentType("text/html; charset=UTF-8"); response.setContentType("application/json;charset=UTF-8"); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); Map<String, String> data = new HashMap<String, String>(); data.put("newFileName", newFileName); data.put("relativePath", relativePath); String result = JSONObject.fromObject(data).toString(); System.out.println("json:\n" + result); response.getWriter().write(result); response.getWriter().flush(); response.getWriter().close(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } private static String createNewFileName(String oldFileName) { String ext = getExtension(oldFileName);// 获取文件扩展名 String newName = UUID.randomUUID().toString() + "." + ext; return newName; } /** * 获取扩展名的方法 */ private static String getExtension(String fileName) { return fileName.substring(fileName.lastIndexOf(".") + 1); } /** * Servlet初始化方法 */ public void init() throws ServletException { } }
对于上面返回的json数据,你可以在如下handlers.js中的uploadSuccess(file, serverData) 方法里面获取:
function uploadSuccess(file, serverData) { try { var progress = new FileProgress(file, this.customSettings.progressTarget); progress.setComplete(); progress.setStatus("上传成功"); serverData=eval('('+serverData+')'); //string->json var relativePath=serverData.relativePath; var newFileName=serverData.newFileName; alert("serverData.relativePath="+serverData.relativePath); alert("serverData="+serverData); //alert(serverData+"上传成功"+file.name); progress.toggleCancel(true);//不隐藏删除按钮 } catch (ex) { this.debug(ex); } }
前台页面,只需要修改一些简单的地方就可以了,这里主要修改了upload_url,主要代码如下:
window.onload = function() { var settings = { flash_url : "<%=path%>/js/swfupload/swfupload.swf", upload_url: "<%=path%>/upload/SWFUploader?relativePath=upload/attachement", post_params: {"SESSID" : "<%=session.getId()%>"}, // 附加参数,版本2新功能 file_size_limit : "100 MB", //file_types : "*.*", //file_types : "*.txt;*.docx;*.doc,*.jpeg;*.png;*.jpg;*.gif", file_types : "*.txt;*.docx;*.doc,*.jpeg;*.png;*.jpg;*.gif;*.doc;*.ppt;*.xls;*.pps;*.docx;*.pptx;*.xlsx;*.rar;*.zip;*.swf;*.zip;*.zip;*.zip;*.zip;", file_types_description : "All Files", file_upload_limit : 2, file_queue_limit : 0, custom_settings : { progressTarget : "fsUploadProgress", cancelButtonId : "btnCancel" }, debug: false, // Button settings button_image_url: "<%=path%>/images/TestImageNoText_65x29.png", button_width: "65", button_height: "29", button_placeholder_id: "spanButtonPlaceHolder", button_text: '<span class="theFont">Hello</span>', button_text_style: ".theFont { font-size: 16; }", button_text_left_padding: 12, button_text_top_padding: 3, // The event handler functions are defined in handlers.js file_queued_handler : fileQueued, file_queue_error_handler : fileQueueError, file_dialog_complete_handler : fileDialogComplete, upload_start_handler : uploadStart, upload_progress_handler : uploadProgress, upload_error_handler : uploadError, upload_success_handler : uploadSuccess, upload_complete_handler : uploadComplete, queue_complete_handler : queueComplete // Queue plugin event }; swfu = new SWFUpload(settings); }; </script>
完整的前台页面代码,可以到这里去下载:http://demo.swfupload.org/v220/simpledemo/index.php
参考:
http://demo.swfupload.org/v220/index.htm
http://demo.swfupload.org/Documentation/
附件中有代码:部署到tomcat中后,访问http://localhost:8080/swfupload_demo/
附件
相关推荐
hadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-bin-32.rarhadoop-common-2.2.0-...
支持图片转pdf、excel转pdf等功能 导入maven命令 ...mvn install:install-file -Dfile=文件路径\spire.xls.free-2.2.0.jar -DgroupId=e-iceblue -DartifactId=spire.xls.free -Dversion=2.2.0 -Dpackaging=jar
amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gzamoeba-mysql-binary-2.2.0.tar.gz amoeba-mysql-binary-2.2.0.tar.gz ...
python库。 资源全名:Pygments-2.2.0-py2.py3-none-any.whl
com.springsource.net.sf.cglib-2.2.0.jar + com.springsource.org.aopalliance-1.0.0.jar + com.springsource.org.aspectj.weaver-1.6.4.RELEASE.jar + spring-aspects-4.3.11.RELEASE.jar
aop的jar包: com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
numpy-1.10.4-cp34-none-win_amd64.whl,python3.4,pyparsing-2.2.0-py2.py3-none-any.whl,python_dateutil-2.7.2-py2.py3-none-any.whl,scipy-0.16.0-cp34-none-win_amd64.whl共同使用
《Keil.STM32F1xx_DFP.2.2.0》是Keil公司为STM32F1系列微控制器提供的一款开发工具包,主要用于STM32F1系列芯片的软件开发。DFP全称为Device Family Pack,是Keil uVision集成开发环境中的一个重要组成部分,它包含...
hook-tar/apache-atlas-2.2.0-falcon-hook.tar.gz hook-tar/apache-atlas-2.2.0-hbase-hook.tar.gz hook-tar/apache-atlas-2.2.0-hive-hook.tar.gz hook-tar/apache-atlas-2.2.0-impala-hook.tar.gz hook-tar/apache...
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.net.sf.cglib-2.2.0.jar
Keil.STM32F1xx_DFP.2.2.0.pack是专门为这些芯片设计的设备支持包(Device Family Pack),它为Keil μVision5 IDE提供了必要的开发工具和驱动程序,使得开发者能够在Keil5环境下方便地进行STM32F10x系列的软件开发...
赠送源代码:spring-boot-autoconfigure-2.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-boot-autoconfigure-2.2.0.RELEASE.pom; 包含翻译后的API文档:spring-boot-autoconfigure-2.2.0.RELEASE-...
Keil.STM32L0xx_DFP.2.2.0.pack
【标题】"Keil.STM32L4xx_DFP.2.2.0-官网下载太慢.rar.zip" 指的是一个压缩文件,包含了STM32L4系列微控制器的开发包(Device Family Pack,简称DFP)。这个开发包是针对Keil MDK集成开发环境的,版本为2.2.0。由于...
5. **集成到Java项目**: 开发者可以将"spire.pdf.free-2.2.0.jar"添加到他们的Java项目类路径中,以便在代码中直接引用和使用其提供的类和方法。这通常可以通过Maven或Gradle等构建工具来实现,或者手动将jar文件放...
《Keil.STM32L4xx_DFP.2.2.0.pack:STM32L4系列微控制器的开发工具包详解》 在嵌入式系统开发领域,Keil是广泛使用的集成开发环境(IDE),它为各种微控制器提供了强大的开发支持。本文将详细介绍Keil.STM32L4xx_...
在官网下载太慢了,Keil.STM32L4xx_DFP.2.2.0.pack,来保存一波,STM32L4xx_DFP.2.2.0.pack 芯片支持包下载。STM32L4xx_DFP.2.2.0.pack
依赖 <groupId>com.xuxueli</groupId> <artifactId>xxl-job-core <version>2.2.0-SNAPSHOT </dependency>