最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)拖拽文件上傳dropzone.js的簡單使用示例代碼

 更新時間:2017年07月15日 09:10:03   作者:crush1988  
本篇文章主要介紹了Java實現(xiàn)拖拽文件上傳dropzone.js的簡單使用示例代碼,具有一定的參考價值,有興趣的可以了解一下

Java實習生一枚,前端知識薄弱,最近因為工作需要,做了一個拖拽文件上傳的功能,發(fā)現(xiàn)dropzone.js挺不錯的,特地做個筆記。

dropzonejs 的官網(wǎng)是: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區(qū)域

<%--拖拽文件上傳 --%> 
            <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> 

  這是我的文件上傳之后的文件隊列區(qū)域:

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

3.對dropzone.css進行修改,將文件內(nèi)的所有dropzone替換為dropz

 修改文件拖拽區(qū)域的顯示樣式:

.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參數(shù)的自定義 

<script type="text/javascript"> 
  $("#div1").dropzone({ 
  url:"systemController.action?saveFile",//上傳文件的地址, 
  maxFiles:1,//最多上傳幾個文件 
  maxFilesize: 5,//文件的大小,單位是M 
  addRemoveLinks:true,//是否有刪除文件的功能 
  dictRemoveFile:"",//刪除文件 
  previewsContainer:"#div2",//文件上傳進度顯示的區(qū)域 
  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 

最終實現(xiàn)的效果如下:

更多使用功能請參考dropzone的官方文檔。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 解決mybatis返回boolean值時數(shù)據(jù)庫返回null的問題

    解決mybatis返回boolean值時數(shù)據(jù)庫返回null的問題

    這篇文章主要介紹了解決mybatis返回boolean值時數(shù)據(jù)庫返回null的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • SpringBoot?+?layui?框架實現(xiàn)一周免登陸功能示例詳解

    SpringBoot?+?layui?框架實現(xiàn)一周免登陸功能示例詳解

    這篇文章主要介紹了SpringBoot+layui框架實現(xiàn)一周免登陸功能,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • IDEA項目啟動時Flyway數(shù)據(jù)庫遷移中的checksum不匹配問題及最新解決方案

    IDEA項目啟動時Flyway數(shù)據(jù)庫遷移中的checksum不匹配問題及最新解決方案

    面對IDEA項目啟動時報出的Flyway遷移校驗和不匹配問題,核心在于保持遷移腳本的一致性、正確管理和理解Flyway的工作機制,本文介紹IDEA項目啟動時Flyway數(shù)據(jù)庫遷移中的checksum不匹配問題及最新解決方案,感興趣的朋友一起看看吧
    2024-01-01
  • 關于Java父類沒有無參構造方法子類處理方法

    關于Java父類沒有無參構造方法子類處理方法

    父類無參構造方法,子類不寫,其實會默認調(diào)用父類的無參構造方法也就是用super(),編譯運行后,會打印出"子類會調(diào)用Father的第一個構造方法,這篇文章給大家介紹關于Java父類沒有無參構造方法子類處理方法,感興趣的朋友一起看看吧
    2024-01-01
  • MyBatis-Plus攔截器實現(xiàn)數(shù)據(jù)權限控制的示例

    MyBatis-Plus攔截器實現(xiàn)數(shù)據(jù)權限控制的示例

    本文主要介紹了MyBatis-Plus攔截器實現(xiàn)數(shù)據(jù)權限控制的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02
  • 使用Iterator刪除List中的多個元素操作

    使用Iterator刪除List中的多個元素操作

    這篇文章主要介紹了使用Iterator刪除List中的多個元素操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 在SpringBoot環(huán)境中使用Mockito進行單元測試的示例詳解

    在SpringBoot環(huán)境中使用Mockito進行單元測試的示例詳解

    Mockito是一個流行的Java?mocking框架,它允許開發(fā)者以簡單直觀的方式創(chuàng)建和使用模擬對象(mocks),Mockito特別適用于在Spring?Boot環(huán)境中進行單元測試,所以本文介紹了在SpringBoot環(huán)境中使用Mockito進行單元測試的示例,需要的朋友可以參考下
    2024-11-11
  • Java設計模式之策略模式深入刨析

    Java設計模式之策略模式深入刨析

    策略模式屬于Java 23種設計模式中行為模式之一,該模式定義了一系列算法,并將每個算法封裝起來,使它們可以相互替換,且算法的變化不會影響使用算法的客戶。本文將通過示例詳細講解這一模式,需要的可以參考一下
    2022-05-05
  • Java多線程之間日志traceId傳遞方式

    Java多線程之間日志traceId傳遞方式

    這篇文章主要介紹了Java多線程之間日志traceId傳遞方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Java?SpringMVC實現(xiàn)自定義攔截器

    Java?SpringMVC實現(xiàn)自定義攔截器

    這篇文章主要為大家詳細介紹了SpringMVC實現(xiàn)自定義攔截器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03

最新評論

樟树市| 贵德县| 靖远县| 杂多县| 巴彦县| 滕州市| 当阳市| 望谟县| 罗甸县| 光山县| 台前县| 沐川县| 土默特左旗| 光山县| 大埔区| 化州市| 庄河市| 龙游县| 南丹县| 叙永县| 前郭尔| 龙海市| 黑龙江省| 全州县| 本溪市| 许昌县| 澄江县| 特克斯县| 巨野县| 门源| 吉木乃县| 五峰| 通许县| 余江县| 永城市| 兴业县| 贵州省| 丹阳市| 固阳县| 九龙城区| 屏东市|