SWFUPLOAD的使用(java版)

2023-12-07 06:48
文章标签 java 使用 swfupload

本文主要是介绍SWFUPLOAD的使用(java版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

很多开发的时候会遇到附加上传功能,最近项目中使用的是uploadswf插件,SWF主要是多文件无刷新上传,利用flash完成多文件上传操作下面把java开发中使用简单的介绍一下:

 

Jsp页面代码:

先在头部引入要使用到的样式文件和js文件

<linkhref="${ctx}/resource/uploadswf/default.css"rel="stylesheet"type="text/css"/>

    <scripttype="text/javascript"src="${ctx}/resource/uploadswf/swfupload.js"></script>

    <scripttype="text/javascript"src="${ctx}/resource/uploadswf/swfupload.queue.js"></script>

    <scripttype="text/javascript"src="${ctx}/resource/uploadswf/fileprogress.js"></script>

    <scripttype="text/javascript"src="${ctx}/resource/uploadswf/handlers.js"></script>

 

<!—使用的js方法 --> 

    <scripttype="text/javascript">

       var upload1;

     window.onload = function() {

           upload1 = new SWFUpload({

              // Backend Settings

              upload_url: "${ctx}/suqiu/topical-upload.action",//上传文件的action

              file_post_name: "file",

              post_params: {"contentId" :$("#msgContentId").val()},

                 //要使用到的参数,contentId在后台设置set get方法,不传就为空

              // File Upload Settings

              file_size_limit : "10240"// 100MB大小自己设置

              file_types : "*.*",

              file_types_description :"All Files",//文件类型自己控制

              file_upload_limit :"10",

              file_queue_limit :"10",

 

              // Event HandlerSettings (all my handlers are in the Handler.js file)

              file_dialog_start_handler : fileDialogStart,

              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,

 

              // ButtonSettings

              button_image_url :"${ctx}/resource/uploadswf/XPButtonUploadText_61x22.png",

              button_placeholder_id :"spanButtonPlaceholder1",

              button_width: 61,

              button_height: 22,

              button_text:"添加附件",

              button_text_left_padding:5,

              // Flash Settings

              flash_url : "${ctx}/resource/uploadswf/swfupload.swf",

             custom_settings : {

                  progressTarget :"fsUploadProgress1",

                  cancelButtonId :"btnCancel1"

              },

             

              // Debug Settings

              debug: false

           });

           //$("#SWFUpload_0").hide();

       }

      

       function deleteAttach(attachId,value){

           if(confirm("确定要删除该附件吗?")){

              $.post("${ctx}/suqiu/topicattach-delete.action?key="

                     + attachId,function(data) {

                         if (data =="no") {

                               alert("删除失败");

                            }else{

                                alert("删除成功");

                                window.location.reload();

                         }

                }

             );

           }

       }

//说明:插入(上传成功)、删除等方法在handlers.js里面定义,页面里不重写的话使用默认的,重写可以自己定义和传不同的参数

<!—页面代码 --> 

<tr>

              <tdclass="text">&nbsp;附件:</td>

              <tdcolspan='2'>

              <divstyle="padding-left:5px;">

              <spanid="spanButtonPlaceholder1"></span>

              <inputid="btnCancel1"type="button"value="取消上传"onclick="cancelQueue(upload1);"disabled="disabled"style="margin-left:2px;height:22px;font-size: 8pt;"/>

              <br/>

                <div class="fieldset flash"id="fsUploadProgress1">

                  <s:iterator value="topicalContent. Attachs"status="u">

                   <divclass="progressWrapper" id='FWSWFUpload<s:propertyvalue="#u.index"/>' style="opacity:1;">

                     <div class="progressContainerblue">

                       <aclass="progressCancel" href="#"style="visibility:visible;"> </a>

                         <div class="progressName"><inputtype="button" class="ico_attbig"><s:propertyvalue="attachName" /></div>

                         <div class="progressBarStatus">

                         <a οnclick="deleteAttach('<s:propertyvalue="attachId"/>',this);" href="#this">删除</a></div>

                         <div class="progressBarComplete"style=""></div>

                      </div>

                  </div>

                 </s:iterator>

              </div>

              </div>

              </td>

           </tr> 

    以上循环遍历出来了已经存在的附件,然后可以继续上传和删除。

 

后台action代码:

import java.io.File;

import java.text.SimpleDateFormat;

import java.util.Date;

import net.sf.json.JSONObject;

@Action(value="topicalcontent-upload")

    public String saveAttach(){

        TopicContentAttach noticeAttachs=newTopicContentAttach();

        TopicalContent obj=null;

        if(msgContentId!=null&&StringUtils.isNotBlank(msgContentId)){

        obj = topicalContentService.getTopicalContent(msgContentId);

        }else{

        obj= new TopicalContent();

        }

       noticeAttachs.setTopicalcontent(obj);

       String nameflll=upload();

noticeAttachs.setAttachUrl(FilePathSptUtil.UPLOAD_CMS+FilePathSptUtil.URL_SPT+FilePathSptUtil.CMS_ZHUANTI+

               FilePathSptUtil.URL_SPT+DateUtil.formatDateToString(new Date(), "yyyy-MM")+FilePathSptUtil.URL_SPT+nameflll);

       noticeAttachs.setAttachName(fileFileName);

       noticeAttachs.setUploadOriginName(fileFileName);

       String fileExtend = fileFileName.substring(fileFileName.lastIndexOf(".")+1,fileFileName.length());

       noticeAttachs.setAttachType(fileExtend);

       topicContentAttachService.save(noticeAttachs);

      return renderText( "{    attachId:\"" +noticeAttachs.getAttachId()+ "\" ,  attachName:\"" +noticeAttachs.getAttachName()+ "\" ,   attachUrl:\"" +noticeAttachs.getAttachUrl()+ "\" }" );

    }

     private Stringupload(){

       SimpleDateFormat formater =new SimpleDateFormat("yyyyMMddhhmmssSSSS");

       String filename=formater.format(new Date());

       String fileExtend = fileFileName.substring(fileFileName.lastIndexOf(".")+1,fileFileName.length());

       String root= FilePathSptUtil.UPLOAD_ROOT_PATH+FilePathSptUtil.UPLOAD_CMS+SPT+FilePathSptUtil.CMS_ZHUANTI+SPT+

                DateUtil.formatDateToString(new Date(),"yyyy-MM");

       String fullName=filename+"."+fileExtend;

       String dstPath = root+SPT+fullName;   //全路径

 

        File dirFile = new File(root);

       boolean isDir = dirFile.isDirectory();

       if(!isDir){//目录不存在则先建目录

           try{

              dirFile.mkdirs();

           }catch (Exception e) {

              File delFile = new File(root+SPT +fullName );

              deleteExitsFile(delFile);

           }

       }

       FileUtil.copyFile(file.getPath(), dstPath);//上传文件

       return fullName;

    }

    private voiddeleteExitsFile(File file) {

       if(file.exists() && file.isFile()){

           file.delete();

       }

    }

 

其中一些常量变量定义好:

    private StringfileFileName;

    private Filefile;

    private StringcontentId;

    privatestaticfinal StringPATH =FilePathSptUtil.UPLOAD_ROOT_PATH;

    privatestaticfinal StringSPT=File.separator;

 

// UPLOAD_ROOT_PATHFilePathSptUtil类中定义,从配置文件读取的路径

    UPLOAD_ROOT_PATH =prop.getProperty("upload.root.path","/");

public StringgetContentId() {

       return contentId;

    }

publicvoidsetContentId(String contentId) {

       this.contentId = contentId;

    }

public File getFile() {

       returnfile;

    }

publicvoid setFile(File file){

       this.file = file;

    }

    public String getFileFileName() {

       returnfileFileName;

    }

publicvoidsetFileFileName(String fileFileName) {

       this.fileFileName = fileFileName;

    }

其中在struts2中在非IE内核的浏览器里会提示上传错误报302错误,接下来解决。

Upload_url:”${ctx}/suqiu/topicalcontent-upload.action;jsessionid=<%=request.getSession().getId()%>”;

 

附件下载: http://download.csdn.net/detail/shiyuezhong/4552201

这篇关于SWFUPLOAD的使用(java版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/464892

相关文章

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Java NoClassDefFoundError运行时错误分析解决

《JavaNoClassDefFoundError运行时错误分析解决》在Java开发中,NoClassDefFoundError是一种常见的运行时错误,它通常表明Java虚拟机在尝试加载一个类时未能... 目录前言一、问题分析二、报错原因三、解决思路检查类路径配置检查依赖库检查类文件调试类加载器问题四、常见

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的