zoukankan      html  css  js  c++  java
  • SWFUpload简单使用样例 Java版(JSP)

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

    实现上传,分为三步:

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

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

    2、页面显示部分:

    Html代码  收藏代码
    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处理文件上传部分:

    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.  //上传操作  
    16.  if(null != uploadSign && !"".equals(uploadSign)){  
    17.   FileItemFactory factory = new DiskFileItemFactory();  
    18.   ServletFileUpload upload = new ServletFileUpload(factory);  
    19.   //upload.setHeaderEncoding("UTF-8");  
    20.   try{  
    21.       List items = upload.parseRequest(request);  
    22.       if(null != items){  
    23.           Iterator itr = items.iterator();  
    24.           while(itr.hasNext()){  
    25.               FileItem item = (FileItem)itr.next();  
    26.               if(item.isFormField()){  
    27.                   continue;  
    28.               }else{  
    29.                                         //以当前精确到秒的日期为上传的文件的文件名称  
    30.                   SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddkkmmss");  
    31.                   String type = item.getName().split("\.")[1];//获取文件类型  
    32.                   File savedFile = new File(path,sdf.format(new Date())+"."+type);  
    33.                   item.write(savedFile);  
    34.               }  
    35.           }  
    36.       }  
    37.   }catch(Exception e){  
    38.       e.printStackTrace();  
    39.   }  
    40.  } 

  • 相关阅读:
    LeetCode--Divide Two Integers
    mysql多实例安装与ssl认证
    ajax请求
    mysql5.6升级及mysql无密码登录
    mysql5.7密码设置
    BusyBox 添加 自定义命令小程序 (applet)
    分享9个常用的国外英文论文文献数据库
    arm linux 移植 gdb/gdbserver
    使用 mtd-utils 烧写Arm Linux 系统各个部分
    YUV图解 (YUV444, YUV422, YUV420, YV12, NV12, NV21)
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3947740.html
Copyright © 2011-2022 走看看