在 GAE/J 上创建文件并上传到 Google 文档

发布于 2024-09-29 06:38:38 字数 767 浏览 1 评论 0原文

是否可以在 GAE/J 上创建任何类型的文件并上传到 google 文档?我问过类似关于创建和上传 PDF 的问题

谢谢。

更新

根据 Google Docs API “要将文档上传到服务器,您可以使用 setFile() 方法将文件附加到新的 DocumentListEntry。”。 setFile 方法需要 java.io.File,GAE/J setFile(java.io.File file, java.lang.String mimeType) 不接受该文件。有没有一种解决方案可以在不存储数据的情况下上传。我需要 java.io.File 类型作为 setFile() 方法工作的参数。我尝试过使用 gaevfs(http://code.google.com/p/gaevfs/)+appengine-java-io(http://code.google.com/p/appengine-java-io /) 但 appengine-java-io 中的 File 类型与 setFile() 方法中使用的 File 类型不匹配。

Is it possible to create any kind of file on GAE/J and upload to google docs? I've asked a similar question before about creating and uploading PDF.

Thanks.

Update

According to Google Docs API "To upload a document to the server, you can attach the file to the new DocumentListEntry using the setFile() method.". And setFile method needs java.io.File which is not accepted by GAE/J setFile(java.io.File file, java.lang.String mimeType). Is there a solution I can upload without storing data. I need java.io.File type as an argument for the setFile() method to work. I've tried using gaevfs(http://code.google.com/p/gaevfs/)+appengine-java-io(http://code.google.com/p/appengine-java-io/) but the type File within appengine-java-io does not match the type File used in setFile() method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光匆匆的小流年 2024-10-06 06:38:38

您想要使用文档的 GData API。您可以在此处找到有关 API 本身的信息(特别是关于上传文档)和此链接有助于将其设置为在 Google App Engine 中运行。

You want to use the GData APIs for Documents. You can find information about the API itself here (specifically the part about Uploading Documents), and this link will be helpful in setting it up to work in Google App Engine.

月下客 2024-10-06 06:38:38

感谢 Nick Johnson,我找到了在 GAE/J 上上传 PDF 的方法。我不知道 PdfEntry 的 setMediaSource() 方法。至少是我找到的示例或任何代码。我尝试了几乎所有示例、解决方案,但最后我在寻找其他内容时找到了另一个代码。如果其他人需要它,答案如下。大部分代码是 PDF 生成,取自 pdfjet 网页 的示例,谢谢大家帮忙。

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        PDF pdf = new PDF(os);
        Font f1 = new Font(pdf, "Helvetica");
        Font f2 = new Font(pdf, "Helvetica-Bold");
        Font f3 = new Font(pdf, "Helvetica-Bold");
        pdf.setTitle("Using TextColumn and Paragraph classes");
        pdf.setSubject("Examples");
        pdf.setAuthor("Innovatics Inc.");
        Page page = new Page(pdf, Letter.PORTRAIT);
        f1.setSize(12);
        f2.setSize(16);
        f3.setSize(14);
        TextColumn column = new TextColumn(f1);
        Paragraph p1 = new Paragraph();
        p1.setAlignment(Align.CENTER);
        p1.add(new TextLine(f2, "Switzerland"));
        Paragraph p2 = new Paragraph();
        p2.add(new TextLine(f2, "Introduction"));
        Paragraph p3 = new Paragraph();
        p3.setAlignment(Align.JUSTIFY);
        TextLine text = new TextLine(f2);
        text.setText("The Swiss Confederation was founded in 1291 as a defensive alliance among three cantons. In succeeding years, other localities joined the original three. The Swiss Confederation secured its independence from the Holy Roman Empire in 1499. Switzerland's sovereignty and neutrality have long been honored by the major European powers, and the country was not involved in either of the two World Wars. The political and economic integration of Europe over the past half century, as well as Switzerland's role in many UN and international organizations, has strengthened Switzerland's ties with its neighbors. However, the country did not officially become a UN member until 2002. Switzerland remains active in many UN and international organizations but retains a strong commitment to neutrality.");
        text.setFont(f1);
        p3.add(text);
        Paragraph p4 = new Paragraph();
        p4.add(new TextLine(f3, "Economy"));
        Paragraph p5 = new Paragraph();
        p5.setAlignment(Align.JUSTIFY);
        text = new TextLine(f1);
        text.setText("Switzerland is a peaceful, prosperous, and stable modern market economy with low unemployment, a highly skilled labor force, and a per capita GDP larger than that of the big Western European economies. The Swiss in recent years have brought their economic practices largely into conformity with the EU's to enhance their international competitiveness. Switzerland remains a safehaven for investors, because it has maintained a degree of bank secrecy and has kept up the franc's long-term external value. Reflecting the anemic economic conditions of Europe, GDP growth stagnated during the 2001-03 period, improved during 2004-05 to 1.8% annually and to 2.9% in 2006. Even so, unemployment has remained at less than half the EU average.");
        p5.add(text);
        Paragraph p6 = new Paragraph();
        p6.setAlignment(Align.RIGHT);
        text = new TextLine(f1);
        text.setColor(RGB.BLUE);
        text.setText("Source: The world fact book.");
        text.setURIAction("https://www.cia.gov/library/publications/the-world-factbook/geos/sz.html");
        p6.add(text);
        column.addParagraph(p1);
        column.addParagraph(p2);
        column.addParagraph(p3);
        column.addParagraph(p4);
        column.addParagraph(p5);
        column.addParagraph(p6);
        column.setPosition(90, 300);
        column.setSize(470, 100);
        column.drawOn(page);
        pdf.flush();
        docsService.setOAuthCredentials(getOAuthParams(), new OAuthHmacSha1Signer());
        URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full/"+folderID+"/contents?xoauth_requestor_id="+user.getEmail());
        PdfEntry newDocument = new PdfEntry();
        newDocument.setCanEdit(true); 
        newDocument.setTitle(new PlainTextConstruct("Sample Report"));
        newDocument.setMediaSource(new MediaByteArraySource(os.toByteArray(), "application/pdf"));
        docsService.insert(feedUrl, newDocument);

Thanks to Nick Johnson I found the way to upload PDF on GAE/J. I was not aware of the method setMediaSource() for PdfEntry. At least the examples nor any of the codes I've found. I've tried almost every example, solution but at last I found another code while looking for something else. In case someone else who needs it the answer is below. Most of the code is PDF generation which is taken from the samples of pdfjet webpage Thanks everyone for help.

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        PDF pdf = new PDF(os);
        Font f1 = new Font(pdf, "Helvetica");
        Font f2 = new Font(pdf, "Helvetica-Bold");
        Font f3 = new Font(pdf, "Helvetica-Bold");
        pdf.setTitle("Using TextColumn and Paragraph classes");
        pdf.setSubject("Examples");
        pdf.setAuthor("Innovatics Inc.");
        Page page = new Page(pdf, Letter.PORTRAIT);
        f1.setSize(12);
        f2.setSize(16);
        f3.setSize(14);
        TextColumn column = new TextColumn(f1);
        Paragraph p1 = new Paragraph();
        p1.setAlignment(Align.CENTER);
        p1.add(new TextLine(f2, "Switzerland"));
        Paragraph p2 = new Paragraph();
        p2.add(new TextLine(f2, "Introduction"));
        Paragraph p3 = new Paragraph();
        p3.setAlignment(Align.JUSTIFY);
        TextLine text = new TextLine(f2);
        text.setText("The Swiss Confederation was founded in 1291 as a defensive alliance among three cantons. In succeeding years, other localities joined the original three. The Swiss Confederation secured its independence from the Holy Roman Empire in 1499. Switzerland's sovereignty and neutrality have long been honored by the major European powers, and the country was not involved in either of the two World Wars. The political and economic integration of Europe over the past half century, as well as Switzerland's role in many UN and international organizations, has strengthened Switzerland's ties with its neighbors. However, the country did not officially become a UN member until 2002. Switzerland remains active in many UN and international organizations but retains a strong commitment to neutrality.");
        text.setFont(f1);
        p3.add(text);
        Paragraph p4 = new Paragraph();
        p4.add(new TextLine(f3, "Economy"));
        Paragraph p5 = new Paragraph();
        p5.setAlignment(Align.JUSTIFY);
        text = new TextLine(f1);
        text.setText("Switzerland is a peaceful, prosperous, and stable modern market economy with low unemployment, a highly skilled labor force, and a per capita GDP larger than that of the big Western European economies. The Swiss in recent years have brought their economic practices largely into conformity with the EU's to enhance their international competitiveness. Switzerland remains a safehaven for investors, because it has maintained a degree of bank secrecy and has kept up the franc's long-term external value. Reflecting the anemic economic conditions of Europe, GDP growth stagnated during the 2001-03 period, improved during 2004-05 to 1.8% annually and to 2.9% in 2006. Even so, unemployment has remained at less than half the EU average.");
        p5.add(text);
        Paragraph p6 = new Paragraph();
        p6.setAlignment(Align.RIGHT);
        text = new TextLine(f1);
        text.setColor(RGB.BLUE);
        text.setText("Source: The world fact book.");
        text.setURIAction("https://www.cia.gov/library/publications/the-world-factbook/geos/sz.html");
        p6.add(text);
        column.addParagraph(p1);
        column.addParagraph(p2);
        column.addParagraph(p3);
        column.addParagraph(p4);
        column.addParagraph(p5);
        column.addParagraph(p6);
        column.setPosition(90, 300);
        column.setSize(470, 100);
        column.drawOn(page);
        pdf.flush();
        docsService.setOAuthCredentials(getOAuthParams(), new OAuthHmacSha1Signer());
        URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full/"+folderID+"/contents?xoauth_requestor_id="+user.getEmail());
        PdfEntry newDocument = new PdfEntry();
        newDocument.setCanEdit(true); 
        newDocument.setTitle(new PlainTextConstruct("Sample Report"));
        newDocument.setMediaSource(new MediaByteArraySource(os.toByteArray(), "application/pdf"));
        docsService.insert(feedUrl, newDocument);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文