remote server ppt2003 to images

来源:互联网 发布:淘宝有货提醒 编辑:程序博客网 时间:2024/04/29 10:31
private List<String> getPPTImages(String url) {        List<String> imageFiles = null;        File pptFile = null;        FileOutputStream fos = null;        ReadableByteChannel rbc = null;        HSLFSlideShow ppt = null;        BufferedInputStream is = null;        InputStream iStream = null;        ByteArrayOutputStream br = null;        try {               URL requestUrl = new URL(url);              String tempFilePath = tempFolder + requestUrl.getFile();            pptFile = new File(tempFilePath);            if(!pptFile.getParentFile().exists()){                pptFile.getParentFile().mkdirs();            }            // Code to download            is = new BufferedInputStream(requestUrl.openStream());            br = new ByteArrayOutputStream();            byte[] buf = new byte[1024];            int n = 0;            while (-1 != (n = is.read(buf))) {                br.write(buf, 0, n);            }            byte[] response = br.toByteArray();            fos = new FileOutputStream(pptFile);            fos.write(response);            ppt =  new HSLFSlideShow(new HSLFSlideShowImpl(tempFilePath));            Dimension pgsize = ppt.getPageSize();            String path = url.replaceFirst(hostUrl, "");            String destFormat = FileTypeHelper._s_defaultImgExtension;            int lastDotIndex = path.lastIndexOf(".");            if (ppt.getSlides() != null && !ppt.getSlides().isEmpty()) {                imageFiles = new ArrayList<>();                List<HSLFSlide> slides = ppt.getSlides();                int i = 1;                for (HSLFSlide slide: slides) {                    //防止中文乱码                      for(HSLFShape shape : slide.getShapes()){                          if(shape instanceof HSLFTextShape) {                              HSLFTextShape tsh = (HSLFTextShape)shape;                              for(HSLFTextParagraph p : tsh){                                  for(HSLFTextRun r : p){                                      r.setFontFamily("宋体");                                  }                              }                          }                      }                     BufferedImage img = new BufferedImage(pgsize.width,pgsize.height, BufferedImage.TYPE_INT_RGB);                    Graphics2D graphics = img.createGraphics();                    graphics.setPaint(Color.white);                    graphics.clearRect(0, 0, pgsize.width, pgsize.height);                    graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width,pgsize.height));                    slide.draw(graphics);                    // save the output and upload                    String newPath =path.substring(0, lastDotIndex) + "(" + i + ")" + destFormat;                    File imageFile = new File( tempFolder+ "/" + newPath);                    fos = new FileOutputStream(imageFile);                    ImageIO.write(img, "jpeg", fos);                    GenericResult<String> token = getUptoken(newPath);                    if (token.getHr() == HResult.S_OK.getIndex() && token.getData() != null) {                        Response uploadResponse = uploadManager.put(tempFolder+ "/" + newPath, newPath, token.getData(), null, MimeTypeHelper.getMimeType(newPath), false);                        if (uploadResponse.isOK()) {                            imageFiles.add(hostUrl + newPath);                            imageFile.delete();                        }                    }                    i++;                }            }        } catch (Exception e) {            logger.error("qiniu get ppt pages count error: ", e);        } finally {            try {                if (is != null)                    is.close();                if (br != null)                    br.close();                if (fos != null)                    fos.close();                if (iStream != null)                    iStream.close();                if (rbc != null)                    rbc.close();                if(ppt != null) ppt.close();                if (pptFile != null && pptFile.exists())                    pptFile.delete();            } catch (IOException e) {            }        }        return imageFiles;    }
0 0