2017-12-08 16 views
-1

Amazon S3サーバからgif形式とjpeg形式で画像をダウンロードしようとしています。 gif形式で画像をダウンロードしようとすると、画像がZip形式でダウンロードされますが、最初のフレームのみが表示されます(gifには3つの画像が含まれています) 。私は非常に多くの例を見てきましたが、適切な解決策を見つけることができません。読み込みと書き込みgif画像

注:
私が直接画像URLに当たったとき、それはgifまたはgif formateの画像を表示します。ここで

は私のコードです:事前に感謝

@RequestMapping(value={"/downloadPhotographs"}, method=RequestMethod.GET) 
    public void downloadPhotograph(HttpServletRequest request, HttpServletResponse response, @RequestParam String campaignId, @RequestParam String campaignName) { 

     String url = "https://s3.amazonaws.com/myimages/"; //not real url 

     String imageOldId = ""; 
     String img_ext = request.getSession().getAttribute("imgExt").toString(); 
     campaignName = campaignName.replace(" ", "") + "_photographs"; 
     try { 

      File zipFile = new File("zipdemo.zip"); 

      // create byte buffer 
      byte[] buffer = new byte[3074]; 

      /* 
      * To create a zip file, use 
      * ZipOutputStream(OutputStream out) constructor of ZipOutputStream class. 
      */ 

      // create object of FileOutputStream 
      FileOutputStream fout = new FileOutputStream(zipFile); 

      // create object of ZipOutputStream from FileOutputStream 
      ZipOutputStream zout = new ZipOutputStream(fout); 

      ArrayList<String> imageIdList = downloadUserDataJavaBean.getPhotographsLink(campaignId); 

      Iterator<String> itr = imageIdList.iterator(); 

      while (itr.hasNext()) { 

       imageOldId = itr.next(); 

       System.out.println("imgId " + imageOldId); 

       BufferedImage bufferImage = ImageIO.read(new URL(url + imageOldId + "." + img_ext)); 
       ByteArrayOutputStream os = new ByteArrayOutputStream(); 

       ImageIO.write(bufferImage, img_ext, os); 

       buffer = os.toByteArray(); 

       InputStream is = new ByteArrayInputStream(buffer); 

       // create object of FileInputStream for source file 
       // below me 
       // FileInputStream fin = new FileInputStream(sourceFiles[i]); 

       /* 
       * To begin writing ZipEntry in the zip file, use 
       * void putNextEntry(ZipEntry entry) method of ZipOutputStream 
       * class. 
       * This method begins writing a new Zip entry to the zip file 
       * and positions the stream to the start of the entry data. 
       */ 

       zout.putNextEntry(new ZipEntry(imageOldId + "." + img_ext)); 

       /* 
       * After creating entry in the zip file, actually write the 
       * file. 
       */ 
       int length; 

       while ((length = is.read(buffer)) > 0) { 
        zout.write(buffer, 0, length); 
       } 

       /* 
       * After writing the file to ZipOutputStream, use 
       * void closeEntry() method of ZipOutputStream class to close 
       * the current entry and position the stream to write the next 
       * entry. 
       */ 

       zout.closeEntry(); 

       // close the InputStream 
       is.close(); 

      } 

      // close the ZipOutputStream 
      zout.close(); 

      System.out.println("Zip file has been created!"); 

      String contentType = "application/excel"; 
      response.setContentType(contentType); 

      byte[] b = new byte[(int) zipFile.length()]; 

      FileInputStream fis = null; 

      try { 

       fis = new FileInputStream(zipFile); 
       fis.read(b); 
       fis.close(); 

      } catch (Exception e) { 

       System.out.println("Problem in file input stream" + e.toString()); 

      } 

      response.setHeader("Content-Disposition", "attachment; filename=" + campaignName + ".zip"); 

      response.setContentLength(b.length); 
      response.getOutputStream().write(b); 
      response.getOutputStream().flush(); 
      response.getOutputStream().close(); 
     } catch (IOException ioe) { 
      System.out.println("IOException :" + ioe); 
     } 
    } 

答えて

0

このコードは正常に動作しています。
注:BufferedImagesはアニメーション化されません。アニメーションの1つのフレームのみを表現できます。 ImageIO.read(......)は、GIFアニメーションの最初のフレームだけを読み込みます。

@RequestMapping(value={"/downloadPhotographs"}, method=RequestMethod.GET) 
     public void downloadPhotograph(HttpServletRequest request, HttpServletResponse response, @RequestParam String camp_id) { 

      String url = "https://s3.amazonaws.com/myimages/"; //not real url 
      try { 
       File zipFile = new File("zipdemo.zip"); 

       /* 
       * To create a zip file, use 
       * ZipOutputStream(OutputStream out) constructor of ZipOutputStream class. 
       */ 

       // create object of FileOutputStream 
       FileOutputStream fout = new FileOutputStream(zipFile); 

       // create object of ZipOutputStream from FileOutputStream 
       ZipOutputStream zout = new ZipOutputStream(fout); 

       ArrayList<String> imageIdList = downloadUserDataJavaBean.getPhotographsLink(camp_id); 

       Iterator<String> itr = imageIdList.iterator(); 

       while (itr.hasNext()) { 

        id = itr.next(); 

        System.out.println("imgId " + id); 

        byte[] b = new byte[1]; 
        URL url_img = new URL(url + id + "." + img_ext); 
        URLConnection urlConnection = url_img.openConnection(); 
        urlConnection.connect(); 
        DataInputStream di = new DataInputStream(urlConnection.getInputStream()); 

        zout.putNextEntry(new ZipEntry(id + "." + img_ext)); 

        while (-1 != di.read(b, 0, 1)) 
         zout.write(b, 0, 1); 

        zout.closeEntry(); 

        di.close(); 

       } 

       // close the ZipOutputStream 
       zout.close(); 

       System.out.println("Zip file has been created!"); 

       String contentType = "application/excel"; 
       response.setContentType(contentType); 

       byte[] b = new byte[(int) zipFile.length()]; 

       FileInputStream fis = null; 

       try { 
        fis = new FileInputStream(zipFile); 
        fis.read(b); 
        fis.close(); 

       } catch (Exception e) { 
        System.out.println("Problem in file input stream" + e.toString()); 
       } 

       response.setHeader("Content-Disposition", "attachment; filename="test.zip"); 

       response.setContentLength(b.length); 
       response.getOutputStream().write(b); 
       response.getOutputStream().flush(); 
       response.getOutputStream().close(); 
      } catch (IOException ioe) { 
       System.out.println("IOException :" + ioe); 
      } 
     } 
関連する問題