SWFUpload简单使用例子 Java版(JSP)

来源:互联网 发布:软件开发需求任务 编辑:程序博客网 时间:2024/04/27 19:34

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_width: 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.  }  
原创粉丝点击