应用服务引擎转换API(JAVA)(Appengine conversion Api (java))

2019-10-29 19:39发布

我想为PDF转换为图像文件的AppEngine内。 理想我想上传PDF格式作为BLOB和同时存储PDF格式和PDF格式的图像。 该转换也可以在不同的时间(任务队列)来完成。

我还没有找到任何工作样品或这样做的好的文档。
官方的文档是在这里 。 这是我对我的上传Servlet实现。

@SuppressWarnings("serial")
public class UploadBlobServlet extends HttpServlet {

  private static final Logger log = Logger.getLogger(UploadBlobServlet.class.getName());

public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
    BlobKey blobKey = blobs.get("data");
    log.log(Level.WARNING,"blobKey: "+blobKey.getKeyString());



        if (blobKey != null) {
        resp.getWriter().println(blobKey.getKeyString());

        BlobstoreInputStream in=new BlobstoreInputStream(blobKey);
        byte[] b = IOUtils.toByteArray(is);
      //  try{
            in.read(b);
            Asset asset = new Asset(
                    "application/pdf", b, "testfile.pdf");
                Document document = new Document(asset);
                Conversion conversion = new Conversion(document, "image/png");

                ConversionService service =
                    ConversionServiceFactory.getConversionService();
                ConversionResult result = service.convert(conversion);

                if (result.success()) {
                  // Note: in most cases, we will return data all in one asset,
                  // except that we return multiple assets for multi-page images.
                FileService fileService=FileServiceFactory.getFileService(); 
                for (Asset ass : result.getOutputDoc().getAssets()) {
                    AppEngineFile file=fileService.createNewBlobFile("image/png", "testfile.png");
                    FileWriteChannel writeChannel=fileService.openWriteChannel(file, false);
                    writeChannel.write(ByteBuffer.wrap(b));
                    writeChannel.closeFinally();
                  }
                } else {
                    log.log(Level.WARNING,"error");

                }

更新:有加入字节[] = IOUtils.toByteArray(是); 但还是收到一个NPE ...

我也很好奇,如果任何人有经验的转换质量。

Answer 1:

要转换的文件,你首先要创建一个资产。 资产通过使字节给构造创建,如示例中所示。 在你的情况,你将需要使用类BlobstoreInputStream阅读您的PDF的字节。

BlobKey blobKey = new BlobKey("your-pdf-blobkey");
InputStream is = new BlobstoreInputStream(blobkey);

然后,你需要从这个输入流中读取所有字节。

转换后,您可以访问与变换图像的字节asset.getData()然后按照这个文档的图像写入Blob存储区。



Answer 2:

这里是工作的代码接收上传PDF格式,并使用这个转换API将其转换成PNG。 上传完成后用多部分后到上传网址必须通过获得:

     String url=blobstoreService.createUploadUrl("/upload");

只要将此代码放在一个servlet并将其映射在web.xml中“上传”。

转换是质量好,但是我没有通知就在文本有点模糊。 在我的情况下,PNG是更大的约25%。

public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
    BlobKey blobKey = blobs.get("data");



        if (blobKey != null) {
        resp.getWriter().println(blobKey.getKeyString());
        BlobstoreInputStream in=new BlobstoreInputStream(blobKey);

        byte[] b = IOUtils.toByteArray(in);
        if(b!=null){
            log.log(Level.WARNING,"blobsize: "+b.length);
        }else{
            log.log(Level.WARNING,"b is null");

        }
            in.read(b);
            Asset asset = new Asset(
                    "application/pdf", b, "testfile.pdf");
                Document document = new Document(asset);
                Conversion conversion = new Conversion(document, "image/png");

                ConversionService service =
                    ConversionServiceFactory.getConversionService();
                ConversionResult result = service.convert(conversion);

                if (result.success()) {
                  // Note: in most cases, we will return data all in one asset,
                  // except that we return multiple assets for multi-page images.
                FileService fileService=FileServiceFactory.getFileService(); 
                for (Asset ass : result.getOutputDoc().getAssets()) {
                    AppEngineFile file=fileService.createNewBlobFile("image/png", "test3file.png");
                    FileWriteChannel writeChannel=fileService.openWriteChannel(file, true);
                    writeChannel.write(ByteBuffer.wrap(ass.getData()));

                    writeChannel.closeFinally();
                  }
                } else {
                    log.log(Level.WARNING,"error");

                }


文章来源: Appengine conversion Api (java)