コード例 #1
0
  @Transactional("blTransactionManagerAssetStorageInfo")
  @Override
  public void createStaticAssetStorageFromFile(MultipartFile file, StaticAsset staticAsset)
      throws IOException {
    if (StorageType.DATABASE.equals(staticAsset.getStorageType())) {
      StaticAssetStorage storage = staticAssetStorageDao.create();
      storage.setStaticAssetId(staticAsset.getId());
      Blob uploadBlob = staticAssetStorageDao.createBlob(file);
      storage.setFileData(uploadBlob);
      staticAssetStorageDao.save(storage);
    } else if (StorageType.FILESYSTEM.equals(staticAsset.getStorageType())) {
      FileWorkArea tempWorkArea = broadleafFileService.initializeWorkArea();
      String destFileName =
          tempWorkArea.getFilePathLocation() + removeLeadingSlash(staticAsset.getFullUrl());

      InputStream input = file.getInputStream();
      byte[] buffer = new byte[fileBufferSize];

      File destFile = new File(destFileName);
      if (!destFile.getParentFile().exists()) {
        if (!destFile.getParentFile().mkdirs()) {
          throw new RuntimeException(
              "Unable to create parent directories for file: " + destFileName);
        }
      }

      OutputStream output = new FileOutputStream(destFile);
      boolean deleteFile = false;
      try {
        int bytesRead;
        int totalBytesRead = 0;
        while ((bytesRead = input.read(buffer)) != -1) {
          totalBytesRead += bytesRead;
          if (totalBytesRead > maxUploadableFileSize) {
            deleteFile = true;
            throw new IOException("Maximum Upload File Size Exceeded");
          }
          output.write(buffer, 0, bytesRead);
        }

        broadleafFileService.addOrUpdateResource(tempWorkArea, destFile, true);
      } finally {
        output.close();
        broadleafFileService.closeWorkArea(tempWorkArea);
      }
    }
  }
コード例 #2
0
  protected void createLocalFileFromInputStream(InputStream is, File baseLocalFile)
      throws IOException {
    BufferedOutputStream bos = null;
    FileWorkArea workArea = null;
    try {
      if (!baseLocalFile.getParentFile().exists()) {
        if (!baseLocalFile.getParentFile().mkdirs()) {
          throw new RuntimeException(
              "Unable to create middle directories for file: " + baseLocalFile.getAbsolutePath());
        }
      }

      workArea = broadleafFileService.initializeWorkArea();
      File tmpFile =
          new File(appendTrailingSlash(workArea.getFilePathLocation()) + baseLocalFile.getName());

      bos = new BufferedOutputStream(new FileOutputStream(tmpFile));

      boolean eof = false;
      int temp;
      while (!eof) {
        temp = is.read();
        if (temp < 0) {
          eof = true;
        } else {
          bos.write(temp);
        }
      }

      FileUtils.moveFile(tmpFile, baseLocalFile);
    } finally {
      try {
        if (bos != null) {
          bos.flush();
          bos.close();
        }
        if (workArea != null) {
          broadleafFileService.closeWorkArea(workArea);
        }
      } catch (Throwable e) {
        // do nothing
      }
    }
  }