private UploadedFile getUploadedFileInfo(MultipartFile multipartFile) throws IOException {

    UploadedFile fileInfo = new UploadedFile();
    fileInfo.setName(multipartFile.getOriginalFilename());
    fileInfo.setSize(multipartFile.getSize());
    fileInfo.setType(multipartFile.getContentType());
    fileInfo.setLocation(Constants.UPLOADTEMP);

    return fileInfo;
  }
Exemple #2
0
  public static FileUploadResult uploadFile(File file, String fileName, long userid)
      throws IOException {
    FileUploadResult result = new FileUploadResult();
    UploadedFile uploadedFile = new UploadedFile();

    S3File s3File = new S3File();
    s3File.file = file;
    s3File.name = fileName;
    s3File.userId = userid;
    s3File.save();
    uploadedFile.url = s3File.getUrl();
    uploadedFile.size = file.length();

    uploadedFile.name = fileName;
    result.files.add(uploadedFile);

    return result;
  }
  @RequestMapping(value = "/get/{fileId}", method = RequestMethod.GET)
  public void getFile(HttpServletResponse response, @PathVariable Long fileId) {

    UploadedFile dataFile = uploadService.getFile(fileId);

    File file = new File(dataFile.getLocation(), dataFile.getName());

    try {
      response.setContentType(dataFile.getType());
      response.setHeader(
          "Content-disposition", "attachment; filename=\"" + dataFile.getName() + "\"");

      FileCopyUtils.copy(FileUtils.readFileToByteArray(file), response.getOutputStream());

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Exemple #4
0
  public static FileUploadResult uploadImageFile(
      File file, String fileName, String contentType, long userid, boolean needthumbnail)
      throws IOException {
    FileUploadResult result = new FileUploadResult();
    UploadedFile uploadedFile = new UploadedFile();

    File targetFolder = new File(RootFolder + userid);

    BufferedImage im = ImageIO.read(file);

    File targetFile = Util.scaleImage(im, fileName, targetFolder, contentType, 1000, 10000);
    if (targetFile != null) {
      S3File s3File = new S3File();
      s3File.file = targetFile;
      s3File.name = fileName;
      s3File.userId = userid;
      s3File.save();
      uploadedFile.url = s3File.getUrl();
      uploadedFile.size = targetFile.length();
    }

    if (needthumbnail) {
      targetFile = Util.scaleImage(im, fileName, targetFolder, contentType, 250, 150);
      if (targetFile != null) {
        S3File s3File = new S3File();
        s3File.file = targetFile;
        s3File.name = fileName;
        s3File.userId = userid;
        s3File.save();
        uploadedFile.thumbnail_url = s3File.getUrl();
      }
    }

    uploadedFile.name = fileName;
    uploadedFile.delete_url = "delete";
    uploadedFile.delete_type = "DELETE";
    result.files.add(uploadedFile);

    return result;
  }
  /**
   * Save image to avatar dir
   *
   * @param avatar
   * @param uploadedFile
   * @return
   */
  private File saveImage(Avatar avatar, UploadedFile uploadedFile) {
    String configKey = getAvatarPathConfigKey(avatar);

    if (configKey != null && uploadedFile != null) {
      UploadUtils upload = new UploadUtils(uploadedFile);

      String imageName =
          String.format(
              "%s.%s",
              MD5.hash(uploadedFile.getFileName() + System.currentTimeMillis()),
              upload.getExtension());

      String filePath =
          String.format(
              "%s/%s/%s",
              this.config.getApplicationPath(), this.config.getValue(configKey), imageName);

      upload.saveUploadedFile(filePath);

      return new File(filePath);
    }

    return null;
  }