与apache poi相关的问题

时间:2015-09-24 11:31:55

标签: java apache-poi

我正面临一个问题,就像我已经使用apache POI生成pptx powerpoint演示文稿所以生成的ppt正在用libra office打开但是当我试图在ms powerpoint中打开它创建一些问题,比如图像没有显示,我插入演示文稿。我在json数组中采用字节编码字符串,我将它传递给我 service.Could有人帮我解决了什么问题?感谢

服务

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();
                    }  

1 个答案:

答案 0 :(得分:4)

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

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

你正在字符串本身的开头减少索引22处的字符串。确保以正确的方式读取字符串。由于特定索引处的子字符串,您正在使用的二进制字符串可能会遗漏decryption。即使你错过了单个字符,你的二进制字符串也没有意义。