【问题标题】:issue related to apache poi与 apache poi 相关的问题
【发布时间】:2015-09-24 11:31:54
【问题描述】:

我面临一个问题,比如我使用 apache POI 来生成 pptx PowerPoint 演示文稿,因此生成的 ppt 可以通过 Libra Office 打开,但是当我尝试在 ms PowerPoint 中打开时,它会产生一些问题,例如无法显示图像,这我插入了演示文稿。我在 json 数组中获取字节编码字符串,并将它传递给我的 服务。有人可以帮我解决什么问题吗?谢谢

服务

public  Response  generatePptDocument(JSONObject json) throws JSONException{
                    JSONArray  jsonArray=json.getJSONArray("image");
                    String [] stringArray=new String[jsonArray.length()];
                    XMLSlideShow ppt = new XMLSlideShow();
                    Response result=null;
                    String uniquename=null;
                    try{
                        for(int i=0;i<jsonArray.length();i++){
                            XSLFSlide slide = ppt.createSlide();
                            stringArray[i]=(String) jsonArray.get(i);
                            byte[] picture=Base64.decodeBase64(stringArray[i].substring(22));
                            int idx = ppt.addPicture(picture, XSLFPictureData.PICTURE_TYPE_PNG);
                            ppt.setPageSize(new java.awt.Dimension(1600, 600));
                            //creating a slide with given picture on it
                            XSLFPictureShape pic = slide.createPicture(idx);
                            String outputDirectory = propertyUtil.getProperty("output.save.uri");
                            SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("ddMMyyyyHHmmss");
                            String date = DATE_FORMAT.format(new Date());
                             uniquename = 1 + "-" + date + "." + "pptx";
                            String streamPath = outputDirectory+uniquename;
                          //creating a file object 
                            File file=new File(streamPath);
                            FileOutputStream out = new FileOutputStream(file);
                          //saving the changes to a file
                            ppt.write(out);
                            StringResponse  response=new StringResponse();
                            response.setUniqueName(uniquename);
                            result=Response.ok().entity(response).build();
                          }
                    }catch(JSONException e){
                        log.info("Error in json Object");
                        result=Response.status(304).entity("Error in json Object").build();
                    }
                    catch(IOException e){
                        log.info("Error while creating PPT Document");
                        result=Response.status(304).entity("Error while generation PPT").build();
                    }  

【问题讨论】:

    标签: java apache-poi


    【解决方案1】:

    您似乎将JSONObject 中的参数作为二进制或输入流。确保您没有使用 if 条件获得 null JSONObject。而且,你写了:

    byte[] picture=Base64.decodeBase64(stringArray[i].substring(22));
    

    您正在削减字符串本身开头索引 22 处的字符串。确保您以正确的方式读取字符串。因为由于特定索引处的子字符串,您正在使用的二进制字符串可能会在 decryption 时丢失某些内容。即使您错过了单个字符,您的二进制字符串也没有任何意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多