SWFUpload官方的样例都是PHP的,在这里提供一个Java版的最简单的使用样例,使用JSP页面完毕全部操作。

实现上传,分为三步:

1、JavaScript设置SWFUpload部分(与官方样例类似):

  1. var upload;
  2. window.onload = function() {
  3. upload = new SWFUpload({
  4. // 处理文件上传的url
  5. upload_url: "${pageContext.request.contextPath}/swfupload/example.jsp?upload=1",
  6. // 上传文件限制设置
  7. file_size_limit : "10240",  // 10MB
  8. file_types : "*.jpg;*.gif;*.png",   //此处也能够改动成你想限制的类型,比方:*.doc;*.wpd;*.pdf
  9. file_types_description : "Image Files",
  10. file_upload_limit : "0",
  11. file_queue_limit : "1",
  12. // 事件处理设置(全部的自己定义处理方法都在handler.js文件中)
  13. file_dialog_start_handler : fileDialogStart,
  14. file_queued_handler : fileQueued,
  15. file_queue_error_handler : fileQueueError,
  16. file_dialog_complete_handler : fileDialogComplete,
  17. upload_start_handler : uploadStart,
  18. upload_progress_handler : uploadProgress,
  19. upload_error_handler : uploadError,
  20. upload_success_handler : uploadSuccess,
  21. upload_complete_handler : uploadComplete,
  22. // 按钮设置
  23. button_image_url : "swfupload/xpbutton.png",    // 按钮图标
  24. button_placeholder_id : "spanButtonPlaceholder",
  25. button_width: 61,
  26. button_height: 22,
  27. // swf设置
  28. flash_url : "swfupload/swfupload.swf",
  29. custom_settings : {
  30. progressTarget : "fsUploadProgress",
  31. cancelButtonId : "btnCancel"
  32. },
  33. // Debug 设置
  34. debug: false
  35. });
  36. }

2、页面显示部分:

  1. <div class="flash" id="fsUploadProgress"></div>
  2. <div style="padding-left: 5px;">
  3. <span id="spanButtonPlaceholder"></span>
  4. <input id="btnCancel" type="button" value="取消" onclick="cancelQueue(upload);"
  5. disabled="disabled" style="margin-left: 2px; height: 22px; font-size: 8pt;" />
  6. </div>

3、Java处理文件上传部分:

  1. String uploadSign = request.getParameter("upload");
  2. String rootPath = request.getParameter("rootPath");
  3. String path = request.getParameter("path");
  4. if(rootPath == null) rootPath = "";
  5. rootPath = rootPath.trim();
  6. if(rootPath.equals("")){
  7. rootPath = application.getRealPath("/swfupload/files");
  8. }
  9. if(path == null) {
  10. path = rootPath;
  11. }else{
  12. path = new String(Base64.decodeBase64(path.getBytes()));
  13. }
  14. //上传操作
  15. if(null != uploadSign && !"".equals(uploadSign)){
  16. FileItemFactory factory = new DiskFileItemFactory();
  17. ServletFileUpload upload = new ServletFileUpload(factory);
  18. //upload.setHeaderEncoding("UTF-8");
  19. try{
  20. List items = upload.parseRequest(request);
  21. if(null != items){
  22. Iterator itr = items.iterator();
  23. while(itr.hasNext()){
  24. FileItem item = (FileItem)itr.next();
  25. if(item.isFormField()){
  26. continue;
  27. }else{
  28. //以当前精确到秒的日期为上传的文件的文件名称
  29. SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddkkmmss");
  30. String type = item.getName().split("\\.")[1];//获取文件类型
  31. File savedFile = new File(path,sdf.format(new Date())+"."+type);
  32. item.write(savedFile);
  33. }
  34. }
  35. }
  36. }catch(Exception e){
  37. e.printStackTrace();
  38. }
  39. }

SWFUpload简单使用样例 Java版(JSP)的更多相关文章

  1. Introspector(内省)简单演示样例 与 简单应用

    简单演示样例: package com.asdfLeftHand.test; import java.beans.BeanDescriptor; import java.beans.BeanInfo; ...

  2. JBoss 系列九十六:JBoss MSC - 简介及一个简单演示样例

    什么是 JBoss MSC JBoss MSC 即 JBoss Modular Service Container,是第三代 JBoss 产品 JBoss 7和WildFfly的内核,JBoss MS ...

  3. Thrift的安装和简单演示样例

    本文仅仅是简单的解说Thrift开源框架的安装和简单使用演示样例.对于具体的解说,后面在进行阐述. Thrift简述                                           ...

  4. [hadoop系列]Pig的安装和简单演示样例

    inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish ).(来源:http://blog.csdn.net/inkfish) Pig是Yaho ...

  5. Android中关于JNI 的学习(零)简单的样例,简单地入门

    Android中JNI的作用,就是让Java可以去调用由C/C++实现的代码,为了实现这个功能.须要用到Anrdoid提供的NDK工具包,在这里不讲怎样配置了,好麻烦,配置了好久. . . 本质上,J ...

  6. Android通过startService播放背景音乐简单演示样例

    关于startService的基本使用概述及其生命周期可參见博客<Android中startService的使用及Service生命周期>. 本文通过播放背景音乐的简单演示样例,演示sta ...

  7. 一则简单演示样例看Oracle的“无私”健壮性

    Oracle的强大之处就在于他能总帮助让你选择正确的运行计划,即使你给了它错误的指示. 实验: 1. 创建測试表: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZ ...

  8. scrapy爬虫学习系列二:scrapy简单爬虫样例学习

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  9. 使用CEF(二)— 基于VS2019编写一个简单CEF样例

    使用CEF(二)- 基于VS2019编写一个简单CEF样例 在这一节中,本人将会在Windows下使用VS2019创建一个空白的C++Windows Desktop Application项目,逐步进 ...

随机推荐

  1. Visual Studio2013创建、公布监控Windows Azure网站

    原文 Visual Studio2013创建.公布监控Windows Azure网站 随着Visual Studio 2013的发布,现在我们可以在Visual Studio内部实现Windows A ...

  2. javascript 中 undefined 和 null 区别

    1.相同点 如果我们直接用 undefined == null  比较他们是相等的返回的将是 true. 2.区别 当我们用undefined === null 比较的时候最后返回的将是 false. ...

  3. Boost::thread库的使用

    阅读对象 本文假设读者有几下Skills [1]在C++中至少使用过一种多线程开发库,有Mutex和Lock的概念. [2]熟悉C++开发,在开发工具中,能够编译.设置boost::thread库. ...

  4. hbase memstorelab

    关于MemStore的补充 在通过HStore.add向store中加入�一个kv时,首先把数据写入到memstore中.这一点没有什么说明: publiclongadd(finalKeyValue ...

  5. 使用JDBC调用数据库的存储过程

    本篇讲述如何使用JDBC来调用MySQL数据库中的存储过程.建议在学习如何使用JDBC调用存储过程前,请先了解如何在数据库中使用存储过程. 存储过程是指在数据库系统中,一组为了完成特定功能的SQL语句 ...

  6. delphi中一切皆指针

    unit Unit1; interface uses  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form ...

  7. eclipse发布项目报错:Multiple Contexts hava a path of “/xxx“

    你的位置:首页 > Java编程 > eclipse发布项目报错:Multiple Contexts hava a path of “/xxx“ eclipse发布项目报错:Multipl ...

  8. 去掉Enter字符(\r)的几个方法

    数据:test.txt: f1:f2:f3:# Shell: #!/bin/bash while read line do echo $line result1=$(echo $line|awk -F ...

  9. javascript实现图片无缝滚动(scrollLeft的使用方法介绍)

    <!DOCTYPE html > <html> <head> <meta http-equiv="Content-Type" conten ...

  10. R语言与数据分析之六:时间序列简介

    今年在某服装企业蹲点了4个多月,之间非常长一段时间在探索其现货和期货预測.时间序列也是做销售预測的首选,今天和小伙伴分享下时间序列的基本性质和怎样用R来挖据时间序列的相关属性. 首先读入一个时间序列: ...