Esempio n. 1
0
  /**
   * 返回FileRepositoryDTO类
   *
   * @return
   * @throws IOException s
   * @see FileRepositoryDTO
   */
  public FileRepositoryDTO getFileContentWithFileRepository(
      String filePath, FileRepository fileRepository) throws IOException {

    StringBuilder builder = new StringBuilder();
    filePath = Config.UPLOAD_FILE_PATH + "/" + filePath + "/" + fileRepository.getId() + ".xzsoft";
    FileReader fr = new FileReader(filePath);
    BufferedReader br = new BufferedReader(fr);
    String readLineStr = null;
    while ((readLineStr = br.readLine()) != null) {
      builder.append(readLineStr);
    }

    String fileContent = builder.toString();

    FileRepositoryDTO dto = new FileRepositoryDTO();
    BeanMapper.copy(fileRepository, dto);
    dto.setContent(Base64.decode(fileContent));

    return dto;
  }
Esempio n. 2
0
  /**
   * 文件的上传服务
   *
   * @param request
   * @return
   * @throws FileUploadException
   * @throws IOException
   */
  public FileRepository uploadFile(HttpServletRequest request)
      throws FileUploadException, IOException {

    boolean isMultipartContent = ServletFileUpload.isMultipartContent(request);
    FileRepository fileRepository = null;
    if (isMultipartContent) {
      FileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);
      List<FileItem> items = upload.parseRequest(request);
      for (Iterator<FileItem> iterator = items.iterator(); iterator.hasNext(); ) {
        FileItem item = iterator.next();
        InputStream inputStream = item.getInputStream();
        // 获得文件名
        String fileName = item.getName();
        String fileExtension = FileUtil.getFileExtension(fileName);
        // 获得文件的Extension类型
        MimeTypeExtension mimeTypeExtension =
            mimeTypeExtensionService.findByMimeTypeExtensionName(fileExtension);
        fileRepository = new FileRepository();
        FixEntityUtil.fixEntity(fileRepository);
        String fileRepoId = fileRepository.getId();
        fileRepository.setMimeTypeExtensionName(fileExtension);
        if (mimeTypeExtension.getMimeType() != null) {
          fileRepository.setMimeTypeName(mimeTypeExtension.getMimeType().getMimeTypeName());
        }
        fileRepository.setFileName(fileName);
        String datePath = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
        String saveFilePath = Config.UPLOAD_FILE_PATH + "/" + datePath + "/";
        File file = new File(saveFilePath);
        if (!file.exists()) {
          file.mkdirs();
        }

        file = new File(saveFilePath + fileRepoId + ".xzsoft");
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        int byteRead = 0;
        byte[] buffer = new byte[8192];
        while ((byteRead = inputStream.read(buffer, 0, 8192)) != -1) {
          out.write(buffer, 0, byteRead);
        }

        inputStream.close();
        // 将上传的文件变Base64加密过的字符串
        String content = Base64.encode(out.toByteArray());
        out.close();

        // 将加密过后的数据存储到硬盘中
        FileWriter fileWriter = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fileWriter);
        bw.write(content);
        bw.flush();
        fileWriter.flush();
        bw.close();
        fileWriter.close();
      }

    } else {
      throw new NotMultipartRequestException("上传的文件里面没有Multipart内容");
    }

    return fileRepository;
  }