乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java 文件上傳

       小仙女本仙人 2021-11-13

      一、前端頁(yè)面

      1、html

       (1)設(shè)置input 的type類(lèi)型為file,代表 用于文件上傳。

       (2)accept屬性,它規(guī)定能夠通過(guò)文件上傳進(jìn)行提交的文件類(lèi)型。accept值是 MIME 類(lèi)型列表,多個(gè)類(lèi)型之間用逗號(hào)隔開(kāi)

      (3)multiple 屬性是 HTML5 中的新屬性。屬性規(guī)定輸入字段可選擇多個(gè)值。多文件上傳

      <div >
      <input id="addFile" class="form-control" class="filepath" type="file" multiple="multiple" accept="application/msword,application/vnd.ms-works,text/plain,application/pdf,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document"><br>
      </div>
      2、js
      add: function () {
          var file = document.getElementById("addFile").files[0];
          if (file == null) {
              toastr.error('請(qǐng)上傳文件');
              return false;
          }
          // 創(chuàng)建form對(duì)象
          var param = new FormData();
          // 通過(guò)append向form對(duì)象添加數(shù)據(jù)
          param.append('file', file);
          param.append('token', $('#token').val());
          // 上傳需要將對(duì)應(yīng)的文件類(lèi)型上傳的數(shù)據(jù)庫(kù)
          param.append('fileType', fileType);
          $.ajax({
              cache: false,
              type: "POST",
              url: backbasePath + '/apia/v1/file/uploadFile',
              data: param,
              async: true,
              contentType: false,
              processData: false,
              success: function (data) {
                  data = eval("(" + data + ")");
                  if ('000000' == data.code) {
                      toastr.success(data.msg);
                      //上傳成功之后清楚掉之前選擇的文件
                      $("#addFile").val("");
                      // 上傳成功之后進(jìn)行table的重新加載
                      $('#filesList').DataTable().ajax.reload();
                  } else if ('900000' == data.code) {
                      toastr.error('上傳失??!');
                  } else {
                      toastr.error(data.msg);
                  }
                  $("#upload").modal('hide');
              },
              error: function () {
                  toastr.error('上傳失??!');
                  $("#upload").modal('hide');
              }
          });
      },
      二、后端代碼
          // 上傳文件
          @RequestMapping("/uploadFile")
          public Object upload(HttpServletRequest request, @RequestParam(required = false) MultipartFile file) {
              String result = null;if (null != file && !file.isEmpty()) {
                  try {
                      // 檢查文件大小
                      long fileSize = file.getSize();
                      if (fileSize > 1 * 1024 * 1024) {
                          return RequestResponseTool.getJsonMessage(RespCode.repeat, "上傳失??!上傳的文件大小超出了1M限制!");
                      }
                      // 檢查文件MIME類(lèi)型
                      String contentType = file.getContentType();
                      List<String> types = new ArrayList<String>();
                      //擴(kuò)展名 doc dot
                      types.add("application/msword");
                      //擴(kuò)展名 docx
                      types.add("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                      //擴(kuò)展名 pdf
                      types.add("application/pdf");
                      //擴(kuò)展名 txt
                      types.add("text/plain");
                      //擴(kuò)展名 wps
                      types.add("application/vnd.ms-works");
                      //擴(kuò)展名 xla xlc xlm xls xlt xlw
                      types.add("application/vnd.ms-excel");
                      if (!types.contains(contentType)) {
                          return RequestResponseTool.getJsonMessage(RespCode.repeat, "上傳失??!不允許上傳此類(lèi)型的文件!");
                      }
                      // 獲取原始文件名
                      String originalFilename = file.getOriginalFilename();
                      String path = filePath;
                      path = path + "/upload/";//定義位絕對(duì)路徑
                      File parent = new File(new File(path).getAbsolutePath());
                      System.out.println("\tparent=" + parent);
                      if (!parent.exists()) {
                          parent.mkdirs();
                      }
                      Date date = new Date();
                      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                      Map<String, Object> m = new HashMap<String, Object>();
                      // 根據(jù)文件名稱(chēng)進(jìn)行查詢(xún),如果存在則更新否則新增
                      Map<String, Object>  fileMap = knowledgeService.getFileByName(originalFilename);
                      // 如果能查詢(xún)出對(duì)應(yīng)的數(shù)據(jù),則進(jìn)行更新操作
                      if(fileMap !=null && fileMap.size() >0){
                          String id =fileMap.get("id").toString();
                          String oldfileName =fileMap.get("file_name").toString();
                          // 找到之前的文件,如果存在則刪除
                          File oldFile = new File( path+"/"+oldfileName);
                          if (oldFile.exists()) {
                              oldFile.delete();
                          }
                          // 保存當(dāng)前的文件
                          file.transferTo(new File(parent, oldfileName));
                          // 更新文件表中的大小
                          m.put("id", id);
                          m.put("file_size", fileSize);
                          result = knowledgeService.update(m);
                      }
                      // 如果查不到數(shù)據(jù),則進(jìn)行新增操作
                      else {
                          // 新文件名稱(chēng)
                          String filename = UUID.randomUUID().toString();
                          String suffix = "";
                          int beginIndex = originalFilename.lastIndexOf(".");
                          if (beginIndex != -1) {
                              suffix = originalFilename.substring(beginIndex);
                          }
                          // 執(zhí)行保存文件
                          file.transferTo(new File(parent, filename + suffix));
                          // 存放的文件路徑
                          String file_path = "/upload/" + filename + suffix;
                          //id
                          String knowledgeId = IDCode.knowledgeId + IDTool.getWebUserId() + "";
                          //文件表Id
                          String file_id = IDCode.fileId + IDTool.getWebUserId() + "";
                          //文件邏輯名稱(chēng)(和路徑中的名稱(chēng)保持一致)
                          String file_name = filename + suffix;
                          //  知識(shí)資源表中的主鍵
                          m.put("id", knowledgeId);// 文件id
                          m.put("file_id", file_id);// 創(chuàng)建時(shí)間
                          m.put("create_time", dateFormat.format(date));
                          m.put("file_name", file_name);
                          m.put("file_real_name", originalFilename);
                          m.put("file_path", file_path);
                          m.put("file_size", fileSize);
                  // 執(zhí)行新增操作 result
      = knowledgeService.add(m); } return result; } catch (Exception ex) { ex.printStackTrace(); } } return result; }

       



        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多