pdf转图片,上传阿里云返回url

/***
     * PDF文件转PNG图片,全部页数
     * @param PdfFilePath pdf完整路径
     * @param formateName 文件后缀
     * @param fileNamespace 文件命名空间
     * @param dpi dpi越大转换后越清晰,相对转换速度越慢
     * @return
     */
    public static List<ResultDO<String>> pdf2ImageAndUpdate(String PdfFilePath, int dpi, String formateName, String fileNamespace) {
        File file = new File(PdfFilePath);
        PDDocument pdDocument = null;
        try {
            pdDocument = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(pdDocument);
            /* dpi越大转换后越清晰,相对转换速度越慢 */
            int pages = pdDocument.getNumberOfPages();
            UploadConstant.FileNamespace fileNamespaceEnum = UploadConstant.FileNamespace.valueOf(fileNamespace);

            ArrayList<ResultDO<String>> list = new ArrayList<>();
            for (int i = 0; i < pages; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, dpi);
                //这边需要每次都新建,不然数据只是第一张图片的流数据,
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                //上传文件名
                String fileName = UUID.randomUUID() + "." + formateName;
                File dstFile = new File("d:\\" + UUID.randomUUID() + "." + formateName);//输出磁盘目录
                ImageIO.write(image, formateName, dstFile);//输出磁盘
                ImageIO.write(image, formateName, byteArrayOutputStream);
                InputStream is = new ByteArrayInputStream(os.toByteArray());
                ResultDO<String> result = ZaUploadManager.uploadFile(UploadConstant.Systype.CLS, fileNamespaceEnum,
                        byteArrayOutputStream.toByteArray(), UUID.randomUUID() + fileName);
                LOGGER.info(result.getMsg()+result.getModule());
                list.add(result);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (Objects.isNull(pdDocument)) {
                    pdDocument.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

 

public static ResultDO<String> uploadFile(Systype sysType, FileNamespace fileNamespace, byte[] contents, String fileName,String pathFlag)  {
      // 参数校验
      if (sysType == null || StringUtils.isEmpty(sysType.name())) {
          return new ResultDO<String>("不允许的系统类型");
      }
      if (fileNamespace == null || StringUtils.isEmpty(fileNamespace.name())) {
          return new ResultDO<String>("不允许的文件空间");
      }
      if (contents == null || contents.length == 0) {
          return new ResultDO<String>("上传内容不能为空");
      }
      if (StringUtils.isEmpty(fileName)) {
          return new ResultDO<String>("文件名不能为空");
      }
      String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1);
      if (StringUtils.isEmpty(fileExtension)) {
          return new ResultDO<String>("无法识别的文件后缀名 " + fileExtension);
      }
      // 文件类型校验
      String contentType = getContentType(fileExtension);

      if (StringUtils.isEmpty(contentType)) {
          return new ResultDO<String>("不支持的上传类型");
      }
      if (USEOSS) {
       String ossPath="zahy/" +sysType.getKey()+"/"+fileNamespace.getKey() +"/"+ Calendar.getInstance().get(Calendar.YEAR) +
             "/" + (Calendar.getInstance().get(Calendar.MONTH)+1) +
             "/" + Calendar.getInstance().get(Calendar.DAY_OF_MONTH) +
             "/" + fileName;
       try {
          boolean putRet = OssUtil.putObject(ossPath, contentType, contents);
          if (putRet) {
             return new ResultDO<String>(true, "", getCdnDomain(contentType) + ossPath);
          }
       } catch (Exception e) {
          logger.error("",e);
       }
}
      else {
       String filePath="zahy/" +sysType.getKey()+"/"+fileNamespace.getKey() +"/"+ Calendar.getInstance().get(Calendar.YEAR) +
             "/" + (Calendar.getInstance().get(Calendar.MONTH)+1) +
             "/" + Calendar.getInstance().get(Calendar.DAY_OF_MONTH) +
             "/" ;
       File target=new File(UPLOADFILE+filePath,fileName);
       if(!target.exists()){
          target.getParentFile().mkdirs();
       try {
          target.createNewFile();
      FileUtils.inputstreamtofile(new ByteArrayInputStream(contents), target);
   } catch (Exception e) {
      logger.error("",e);
   }
       //如果是controller调用会存在.9,bss存在.10
       if (!Objects.isNull(pathFlag)&& Objects.equals(pathFlag,"bss")){
              return new ResultDO<String>(true, "", BSSIPCONFIG+filePath+fileName);
          }else{
              return new ResultDO<String>(true, "", IPCONFIG+filePath+fileName);
          }
}
   }
      return new ResultDO<String>("上传失败");
  }

 

 

//上传到自己服务器
public static void inputstreamtofile(InputStream ins,File file){
   OutputStream os = null;
   try {
      os = new FileOutputStream(file);
      int bytesRead = 0;
      byte[] buffer = new byte[8192];
      while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
         os.write(buffer, 0, bytesRead);
      }
      os.close();
      ins.close();
   } catch (FileNotFoundException e) {
      e.printStackTrace();
   } catch (IOException e) {
      e.printStackTrace();
   }

}

 

 

/**
 * 上传单个文件到oss
 * 
 * @param key
 *            文件路径
 * @param contentType
 *            内容类型
 * @param contents
 *            内容
 * @return
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws IOException
 * @throws MalformedURLException
 */
public static boolean putObject(String key, String contentType, byte[] contents) throws InvalidKeyException, NoSuchAlgorithmException,
      MalformedURLException, IOException {

   String date = TimeUtil.getRfc822DateFormat(new Date());

   HttpURLConnection httpURLConnection = WebUtil.getHttpURLConnection(HOST + key);
   httpURLConnection.setRequestProperty("Date", date);
   httpURLConnection.setRequestProperty("Authorization", computeSignature("PUT", contentType, date, null, BASE_RESOURCE_PATH + key));
   httpURLConnection.setRequestProperty("Cache-Control", "public");

   httpURLConnection.setRequestMethod("PUT");
   httpURLConnection.setDoOutput(true);
   httpURLConnection.setRequestProperty("Content-Length", contents.length + "");
   if (contentType != null && contentType.length() > 0) {
      httpURLConnection.setRequestProperty("Content-Type", contentType);
   }

   OutputStream os = null;
   try {
      os = httpURLConnection.getOutputStream();
      os.write(contents);
   } catch (Exception e) {
   } finally {
      if (os != null) {
         try {
            os.flush();
            os.close();
         } catch (Exception e2) {
         }
      }
   }

   httpURLConnection.connect();

   InputStream inputStream = httpURLConnection.getInputStream();
   if (inputStream != null) {
      try {
         inputStream.close();
      } catch (Exception e) {
      }
   }

   if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
      return true;
   }

   return false;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值