Java实现拖拽文件上传dropzone.js的简单使用示例代码

 更新时间:2017年07月15日 09:10:03   作者:crush1988  
本篇文章主要介绍了Java实现拖拽文件上传dropzone.js的简单使用示例代码,具有一定的参考价值,有兴趣的可以了解一下

Java实习生一枚,前端知识薄弱,最近因为工作需要,做了一个拖拽文件上传的功能,发现dropzone.js挺不错的,特地做个笔记。

dropzonejs 的官网是:http://www.dropzonejs.com/, 中文手册是:http://wxb.github.io/dropzonejs.com.zh-CN/

自己写的拖拽文件至一个按钮上传的功能,前端及java代码如下:

 jsp页面:

1. 首先必须引入dropzone的js和css文件

<link rel="stylesheet" href="dropzone/css/dropzone.css" rel="external nofollow" > 
<script src="dropzone/js/dropzone.js"></script> 

 2.自己定义两个div区域

<%--拖拽文件上传 --%> 
            <div id="div1" class="dropz" style="width:0px; height:0px;"> 
             uopload 
            </div> 
            <div id="div2" class="dropz" style=" background: white;border:none;float:left;"> 
              
            </div> 

  这是我的文件上传之后的文件队列区域:

<div id="fileslist" style="padding: 10px;"></div> 

3.对dropzone.css进行修改,将文件内的所有dropzone替换为dropz

 修改文件拖拽区域的显示样式:

.dropz {/*设置拖拽上传文件按钮的格式*/ 
  min-height:0px; 
  min-width: 100px; 
  border: 1px solid #58AF0C; 
  background: white; 
  padding: 15px 20px; 
  background-color: #7AC143; 
  background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0, #7AC143), 
    color-stop(1, #7AC143)); 
  background-position: center top; 
  background-repeat: no-repeat; 
  border-radius: 5px; 
  min-height:0px; 
  min-width: 100px; 
  padding: 15px 20px;    
  color: #FFF; 
  font: bold 12px Arial, Helvetica, sans-serif; 
  text-align: center; 
  text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25); 
 } 
 .dropz.dz-clickable { 
  cursor: pointer; 
  line-height: 0px;/*按钮中的文字垂直居中*/ 
   } 

4.在jsp对div进行dropzone参数的自定义 

<script type="text/javascript"> 
  $("#div1").dropzone({ 
  url:"systemController.action?saveFile",//上传文件的地址, 
  maxFiles:1,//最多上传几个文件 
  maxFilesize: 5,//文件的大小,单位是M 
  addRemoveLinks:true,//是否有删除文件的功能 
  dictRemoveFile:"",//删除文件 
  previewsContainer:"#div2",//文件上传进度显示的区域 
  acceptedFiles: ".jpg,.jpeg,.png,.gif,.xls,.txt,.sql,.rar,.mkv",//支持的格式 
  paramName:'file',//上传的FILE名称,即服务端可以通过此来获取上传的文件,如$_FILES['dropimage'] 
  init: function() {//初始化时的事件 
    //$("#uploadfile").uploadFile({success:function(data){ 
     this.on("addedfile", function(file) { 
 
      // Create the remove button 
      var removeButton = Dropzone.createElement("<img src='plug-in/uploadify/img/uploadify-cancel.png' title='删除'/>"); 
 
      // Capture the Dropzone instance as closure. 
      var _this = this; 
 
      // Listen to the click event 
      removeButton.addEventListener("click", function(e) { 
       // Make sure the button click doesn't submit the form: 
       e.preventDefault(); 
       e.stopPropagation(); 
       alert("Are you sure to delete?"); 
       // Remove the file preview. 
       _this.removeFile(file); 
       // If you want to the delete the file on the server as well, 
       // you can do the AJAX request here. 
      }); 
      // Add the button to the file preview element. 
      file.previewElement.appendChild(removeButton); 
      }); 
      this.on("success", function(file, data) {  
        if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) { 
          var d = $.parseJSON(data); 
          var fileitem = "<span class='uploadFile-queue-item' id='" + d.fileKey + "'><a>" + d.name 
          + "</a><img border='0' style='padding:2px;cursor:pointer;' onclick=delAttachment('" + d.delurl + "','" 
          + d.fileKey + "','" + d.name 
          + "') title='删除' src='plug-in/uploadify/img/uploadify-cancel.png' widht='15' height='15'> </span>"; 
         $("#fileslist").html(fileitem); 
         $("#attachment").val(d.fileKey + "," + d.name + ";"); 
        }  
        this.removeFile(file); 
      }); 
    } 
}); 
</script> 

 java后台处理文件上传的代码: 

@RequestMapping(params = "saveFile", method = RequestMethod.POST) 
  public void saveFile(HttpServletRequest request, HttpServletResponse response, TSDocument document) throws Exception{ 
    Map<String, Object> attributes = new HashMap<String, Object>(); 
    TSTypegroup tsTypegroup=systemService.getTypeGroup("fieltype","文档分类"); 
    TSType tsType = systemService.getType("files","附件", tsTypegroup); 
    String fileKey = oConvertUtils.getString(request.getParameter("fileKey"));// 文件ID 
    String documentTitle = oConvertUtils.getString(request.getParameter("documentTitle"),"uploadfile");// 文件标题 
    if (StringUtil.isNotEmpty(fileKey)) { 
      document.setId(fileKey); 
      document = systemService.getEntity(TSDocument.class, fileKey); 
      document.setDocumentTitle(documentTitle); 
 
    } 
    document.setBusinessKey(request.getParameter("businessKey")); 
    document.setSubclassname(MyClassLoader.getPackPath(document)); 
    document.setCreatedate(DateUtils.gettimestamp()); 
    document.setTSType(tsType); 
    UploadFile uploadFile = new UploadFile(request, document); 
    uploadFile.setCusPath("files"); 
    uploadFile.setSwfpath("swfpath"); 
    document = systemService.uploadFile(uploadFile); 
    attributes.put("url", document.getRealpath()); 
    attributes.put("fileKey", document.getId()); 
    if (ResourceUtil.getSessionUserName()!=null) { 
      attributes.put("uploadUser", ResourceUtil.getSessionUserName().getUserName()); 
    }else{ 
      attributes.put("uploadUser", "null"); 
    } 
    attributes.put("time", new SimpleDateFormat("yyyy-MM-dd").format(new Date())); 
    attributes.put("name", document.getAttachmenttitle()+"."+document.getExtend()); 
    attributes.put("downloadurl", "commonController.action?viewFile&fileid="+ document.getId()+"&subclassname="); 
    attributes.put("viewhref", "commonController.action?objfileList&fileKey=" + document.getId()); 
    attributes.put("delurl", "commonController.action?delObjFile&fileKey=" + document.getId()); 
    attributes.put("realPath", document.getRealpath()); 
    if(FileUtils.isPicture(document.getExtend())){ 
      attributes.put("imgUrl", document.getRealpath()); 
    } 
    JSONObject js = new JSONObject(attributes); 
    response.getWriter().write(js.toString()); 
    response.getWriter().flush(); 
  } 

注意这里的返回值是直接返回的json对象,如果采用

@RequestMapping(params = "saveFiles", method = RequestMethod.POST) 
  @ResponseBody 

则会报错:

复制代码 代码如下:

[com.framework.core.common.exception.MyExceptionHandler]org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 

最终实现的效果如下:

更多使用功能请参考dropzone的官方文档。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 解决mybatis返回boolean值时数据库返回null的问题

    解决mybatis返回boolean值时数据库返回null的问题

    这篇文章主要介绍了解决mybatis返回boolean值时数据库返回null的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • SpringBoot + layui 框架实现一周免登陆功能示例详解

    SpringBoot + layui 框架实现一周免登陆功能示例详解

    这篇文章主要介绍了SpringBoot+layui框架实现一周免登陆功能,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-08-08
  • IDEA项目启动时Flyway数据库迁移中的checksum不匹配问题及最新解决方案

    IDEA项目启动时Flyway数据库迁移中的checksum不匹配问题及最新解决方案

    面对IDEA项目启动时报出的Flyway迁移校验和不匹配问题,核心在于保持迁移脚本的一致性、正确管理和理解Flyway的工作机制,本文介绍IDEA项目启动时Flyway数据库迁移中的checksum不匹配问题及最新解决方案,感兴趣的朋友一起看看吧
    2024-01-01
  • 关于Java父类没有无参构造方法子类处理方法

    关于Java父类没有无参构造方法子类处理方法

    父类无参构造方法,子类不写,其实会默认调用父类的无参构造方法也就是用super(),编译运行后,会打印出"子类会调用Father的第一个构造方法,这篇文章给大家介绍关于Java父类没有无参构造方法子类处理方法,感兴趣的朋友一起看看吧
    2024-01-01
  • MyBatis-Plus拦截器实现数据权限控制的示例

    MyBatis-Plus拦截器实现数据权限控制的示例

    本文主要介绍了MyBatis-Plus拦截器实现数据权限控制的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • 使用Iterator删除List中的多个元素操作

    使用Iterator删除List中的多个元素操作

    这篇文章主要介绍了使用Iterator删除List中的多个元素操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • 在SpringBoot环境中使用Mockito进行单元测试的示例详解

    在SpringBoot环境中使用Mockito进行单元测试的示例详解

    Mockito是一个流行的Java mocking框架,它允许开发者以简单直观的方式创建和使用模拟对象(mocks),Mockito特别适用于在Spring Boot环境中进行单元测试,所以本文介绍了在SpringBoot环境中使用Mockito进行单元测试的示例,需要的朋友可以参考下
    2024-11-11
  • Java设计模式之策略模式深入刨析

    Java设计模式之策略模式深入刨析

    策略模式属于Java 23种设计模式中行为模式之一,该模式定义了一系列算法,并将每个算法封装起来,使它们可以相互替换,且算法的变化不会影响使用算法的客户。本文将通过示例详细讲解这一模式,需要的可以参考一下
    2022-05-05
  • Java多线程之间日志traceId传递方式

    Java多线程之间日志traceId传递方式

    这篇文章主要介绍了Java多线程之间日志traceId传递方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • Java SpringMVC实现自定义拦截器

    Java SpringMVC实现自定义拦截器

    这篇文章主要为大家详细介绍了SpringMVC实现自定义拦截器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03

最新评论