我想为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 ...
我也很好奇,如果任何人有经验的转换质量。