jQuery 异步上传插件 Uploadify 使用 struts

Uploadify是JQuery的一个上传插件,实现的效果非常不错,带进度显示。而且是Ajax的,省去了自己写Ajax上传功能的麻烦。不过官方提供的实例时php版本的,本文将详细介绍Uploadify在J2EE中的使用。


1.引入头文件(注意一定要把jQuery放在前面):

注意:uploader是上传的Action路径名;formData是传递给Action的参数列表。

[html] view plain copy
  1. <scriptsrc="lib/jquery-1.8.3.min.js"></script>
  2. <scriptsrc="lib/uploadify/jquery.uploadify.min.js"type="text/javascript"></script>
  3. <scripttype="text/javascript">
  4. $(function(){
  5. $('#projectfile').uploadify({
  6. 'swf':'lib/uploadify/uploadify.swf',
  7. 'uploader':'ProjectfileAdd.action',
  8. 'formData':{'projectid':'${project.id}'},
  9. 'fileObjName':'file',
  10. 'fileSizeLimit':'0',
  11. 'onQueueComplete':function(queueData){
  12. alert(queueData.uploadsSuccessful+'filesweresuccessfullyuploaded.');
  13. }
  14. });
  15. });
  16. </script>

2.需要上传的地方添加一个<form>,里面有一个<input>(注意其id属性):

[html] view plain copy
  1. <form>
  2. <inputid="projectfile"name="file"type="file"></input>
  3. </form>

3.看一下<input>效果如下,还挺美观的


4.后台的Action就是一个普通的上传文件的Action,示例如下:

[java] view plain copy
  1. packagedc.action.projectfile;
  2. importjava.io.BufferedInputStream;
  3. importjava.io.BufferedOutputStream;
  4. importjava.io.File;
  5. importjava.io.FileInputStream;
  6. importjava.io.FileOutputStream;
  7. importjava.io.IOException;
  8. importjava.io.InputStream;
  9. importjava.io.OutputStream;
  10. importjava.sql.Timestamp;
  11. importjava.util.Date;
  12. importjava.util.Map;
  13. importorg.apache.struts2.ServletActionContext;
  14. importcom.opensymphony.xwork2.ActionContext;
  15. importcom.opensymphony.xwork2.ActionSupport;
  16. importdc.bean.Project;
  17. importdc.bean.Projectfile;
  18. importdc.bean.User;
  19. importdc.service.ProjectService;
  20. importdc.service.ProjectfileService;
  21. publicclassProjectfileAddextendsActionSupport{
  22. privatestaticfinalintFILE_SIZE=16*1024;
  23. privateintprojectid;
  24. privateProjectServiceprojectService;
  25. privateProjectfileServiceprojectfileService;
  26. privateFilefile;
  27. privateStringfileFileName;
  28. privateStringfileContentType;
  29. publicintgetProjectid(){
  30. returnprojectid;
  31. }
  32. publicvoidsetProjectid(intprojectid){
  33. this.projectid=projectid;
  34. }
  35. publicProjectServicegetProjectService(){
  36. returnprojectService;
  37. }
  38. publicvoidsetProjectService(ProjectServiceprojectService){
  39. this.projectService=projectService;
  40. }
  41. publicProjectfileServicegetProjectfileService(){
  42. returnprojectfileService;
  43. }
  44. publicvoidsetProjectfileService(ProjectfileServiceprojectfileService){
  45. this.projectfileService=projectfileService;
  46. }
  47. publicFilegetFile(){
  48. returnfile;
  49. }
  50. publicvoidsetFile(Filefile){
  51. this.file=file;
  52. }
  53. publicStringgetFileFileName(){
  54. returnfileFileName;
  55. }
  56. publicvoidsetFileFileName(StringfileFileName){
  57. this.fileFileName=fileFileName;
  58. }
  59. publicStringgetFileContentType(){
  60. returnfileContentType;
  61. }
  62. publicvoidsetFileContentType(StringfileContentType){
  63. this.fileContentType=fileContentType;
  64. }
  65. publicstaticintgetFileSize(){
  66. returnFILE_SIZE;
  67. }
  68. //写入文件时候用到的函数
  69. publicvoidupLoadFile(Filesource,Filetarget){
  70. InputStreamin=null;
  71. OutputStreamout=null;
  72. try{
  73. in=newBufferedInputStream(newFileInputStream(source),FILE_SIZE);
  74. out=newBufferedOutputStream(newFileOutputStream(target),FILE_SIZE);
  75. //in-->image-->out
  76. byte[]image=newbyte[FILE_SIZE];
  77. while(in.read(image)>0){
  78. out.write(image);
  79. }
  80. }catch(IOExceptionex){
  81. ex.printStackTrace();
  82. }finally{
  83. try{
  84. in.close();
  85. out.close();
  86. }catch(IOExceptionex){
  87. ex.printStackTrace();
  88. }
  89. }
  90. }
  91. publicStringexecute(){
  92. try{
  93. Projectfileprojectfile=newProjectfile();
  94. projectfile.setName(fileFileName);
  95. Projectproject=projectService.ReadByID(projectid);
  96. projectfile.setProject(project);
  97. projectfile.setModifytime(newTimestamp(newDate().getTime()));
  98. Stringfilepath="file/"+project.getId()+"/"+fileFileName;
  99. projectfile.setFilepath(filepath);
  100. projectfile.setFilesize(file.length());
  101. //不是连接到外网的文件
  102. projectfile.setFilelink(0);
  103. projectfile.setContenttype(fileContentType);
  104. //--记录编辑人------------------
  105. ActionContextcontext=ActionContext.getContext();
  106. MapsessionMap=context.getSession();
  107. Usercurrentuser=(User)sessionMap.get("user");
  108. projectfile.setUser(currentuser);
  109. //------------------------------
  110. //projectfile.setFilesize(1);
  111. projectfileService.save(projectfile);
  112. //文件上传,存至硬盘
  113. //ServletActionContext.getServletContext().getRealPath获得webRoot绝对路径
  114. StringrealfilePath=ServletActionContext.getServletContext().getRealPath("file")
  115. +"/"+project.getId()+"/"+this.fileFileName;
  116. FiletargetFile=newFile(realfilePath);
  117. targetFile.getParentFile().mkdirs();
  118. upLoadFile(file,targetFile);
  119. }catch(Exceptionex){
  120. ex.printStackTrace();
  121. returnERROR;
  122. }
  123. returnSUCCESS;
  124. }
  125. }

Uploadify官方网站: http://www.uploadify.com/

Uploadify下载:http://download.csdn.net/detail/leixiaohua1020/6376463

你可能感兴趣的:(uploadify)