通常在开发的时候,我们会遇到图片上传的功能,特别是有很多是提供url地址的方式。所以需要提供一个将url的图片等存储起来,然后提供一个我们自己的地址给用户使用。

第一步:提供pdfbox的jar包。准备相应的maven

       

<dependency>
             <groupId>org.apache.pdfbox</groupId>
             <artifactId>pdfbox</artifactId>
             <version>2.0.5</version>
         </dependency>
         <dependency>
             <groupId>org.apache.pdfbox</groupId>
             <artifactId>pdfbox-tools</artifactId>
             <version>2.0.5</version>
         </dependency>

第二步:准备相应的类,

import java.awt.image.BufferedImage;
 import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.net.HttpURLConnection;
 import java.net.URL;import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.rendering.ImageType;
 import org.apache.pdfbox.rendering.PDFRenderer;
 import org.apache.pdfbox.tools.imageio.ImageIOUtil;
  
 public class Downimage {
      public final  static String  IMG_TYPE_PDF = ".pdf";       /**
       *將网上的url转换成本地或者
       * @param destUrl   提供的图片URL地址
       * @param orderCode  提供的编号
       * @return
       * @throws Exception
       */
     public static String saveToFile(String destUrl,String orderCode) throws Exception {       //存储在服务器上的地址
         String filePath ="c:/data";      //返回给用户的地址前缀
         String realPath="localhost:8080";
     
         FileOutputStream fos = null;
         BufferedInputStream bis = null;
         HttpURLConnection httpUrl = null;
         URL url = null;
         int BUFFER_SIZE = 1024;
         byte[] buf = new byte[BUFFER_SIZE];
         int size = 0;
         try {
             url = new URL(destUrl);
             httpUrl = (HttpURLConnection) url.openConnection();
             httpUrl.connect();
             bis = new BufferedInputStream(httpUrl.getInputStream());
             String pdfUrl=filePath+"/"+orderCode+IMG_TYPE_PDF;
             System.out.println("pdfUrl="+pdfUrl);
              try {
                 fos = new FileOutputStream(pdfUrl);
             } catch (Exception e) {
                 e.printStackTrace();
                 return null;
             }
         
             while ((size = bis.read(buf)) != -1) {
                 fos.write(buf, 0, size);
             }
             fos.flush();
             String ourUrl=realPath+"/"+orderCode+IMG_TYPE_PDF;
             pdf2img(pdfUrl,filePath+"/"+orderCode,".png");
             ourUrl=realPath+"/"+orderCode+".pdf";
             System.out.println("outUrl="+ourUrl);
             return ourUrl;
         } catch (IOException e) {
             e.printStackTrace();
         } catch (ClassCastException e) {
             e.printStackTrace();
         } finally {
             try {
                 fos.close();
                 bis.close();
                 httpUrl.disconnect();
             } catch (IOException e) {
                 e.printStackTrace();
             } catch (NullPointerException e) {
                 e.printStackTrace();
             }
         }
         return null;
     }
     /**
      * pdf转换成png
      * @param pdfPath
      * @param savePath
      * @param imgType
      * @throws IOException
      */
     public static void pdf2img(String pdfPath, String savePath, String imgType) throws IOException {
         PDDocument document = PDDocument.load(new File(pdfPath));
         PDFRenderer pdfRenderer = new PDFRenderer(document);
         for (int page = 0; page < document.getNumberOfPages(); ++page)
         { 
             BufferedImage bim = pdfRenderer.renderImageWithDPI(page, 300, ImageType.RGB);
             ImageIOUtil.writeImage(bim, savePath + imgType, 300);
         }
         document.close();
     }    public static void main(String[] args) throws Exception {
         Downimage dw = new Downimage();
         String str=dw.saveToFile("https://s1.tuchong.com/content-image/af1cd4d8387103ded6eb0fe78198e5c9.jpg","1254");
         System.out.println("str="+str);
    }
 }