【问题标题】:Getting a pdf for an Alfresco Java Webscript Controller获取 Alfresco Java Webscript 控制器的 pdf
【发布时间】:2020-10-21 06:42:13
【问题描述】:

我正在尝试在 Alfresco 中构建一个简单的 Webscript 端点,它使用 Java Webscript 控制器获取 pdf。我们最终希望扩展此端点以获取多个 pdf 并进行一些操作,但目前我们只是尝试读入并保存 1 个 pdf。

问题是生成的 InputStream 是空的。尽管它对 xml 文件工作得很好。

这是我们上传的pdf.get.desc

<webscript>
    <shortname>Upload PDFs</shortname>
    <description>Upload PDFs</description>
    <url>/uploadpdf</url>
    <authentication>user</authentication>
    <format default="html"></format>  
</webscript>

这是我们上传的pdf.get.html.ftl

<html>
  <body>
    <form action="${url.service}" method="post" enctype="multipart/form-data">
      PDF1: <input type="file" name="pdf1"><br>
      XML1: <input type="file" name="xml1"><br>
      <input type="submit" name="submit" value="Upload">
    </form>
  </body>
</html>

这是我们上传的pdf.post.dec

<webscript>
    <shortname>Upload PDFs</shortname>
    <description>Upload PDFs</description>
    <url>/uploadpdf</url>
    <authentication>user</authentication>
    <format default="json"></format>  
</webscript>

这是我们上传的pdf.post.json.ftl(目前只是返回一个测试字符串)

${newFile}

这是我们的 Webscript-context.xml

<?xml version='1.0' encoding='UTF-8'?>
<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at
    
    http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="webscript.alfresco.tutorials.helloworld.get"
          class="com.test.platformsample.HelloWorldWebScript"
          parent="webscript">
    </bean>
    <bean id="webscript.uploadpdf.get"
          class="com.test.UploadPdfWebScript"
          parent="webscript">
    </bean>
    <bean id="webscript.uploadpdf.post"
          class="com.test.UploadPdfWebScript"
          parent="webscript">
    </bean>
</beans>

这是我们的 UploadPdfWebscript.java(注意我们使用的是 org.springframework.extensions.webscripts.servlet.FormData; 这是为了轻松获取文件。然后代码将文件保存到本地 docker 容器中。问题是文件和扩展输入流是空的。

package com.test;

import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.servlet.FormData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;

public class UploadPdfWebScript extends DeclarativeWebScript {
    private static Log logger = LogFactory.getLog(UploadPdfWebScript.class);

    protected Map<String, Object> executeImpl(
            WebScriptRequest req, Status status, Cache cache) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("fromJava", "HelloFromJava");

        logger.debug("Your 'UploadPdf' Web Script was called!");

        final FormData form = (FormData)req.parseContent();

        InputStream file1 = null;

        if(form == null || form.getFields() == null) {
           return model;
        }

        for (FormData.FormField field : form.getFields())
        {
            if (field.getName().equals("pdf1"))
            {
                file1 = field.getInputStream();
            }

        }

        String result = "this should be overwritten";
        try{
            
            result = processFile(file1);
        } catch(Exception e) {
            logger.error(e.getMessage());
        }

        if(result == null || result.equals("")) {
           result = "No result";
        }
        
        model.put("newFile", result);
        return model;

       }

 public String processFile(InputStream file) {
        String ret = "{\”Result\": Success}”;
        try {
        byte[] buffer = new byte[file.available()];
            file.read(buffer);
     
            File targetFile = new File("targetFile.pdf");
            OutputStream outStream = new FileOutputStream(targetFile);  
            outStream.write(buffer2);
        } catch (Exception e) {
        ret = "{\”Result\": Failure}”;
            logger.error(e.getMessage(), e);
        }
        return ret;
}

如何从 InputStream 中获取 pdf 或其他任意文件类型?每当我尝试上传 pdf 时,从表单返回的 InputStream 再次为空,因此保存的 pdf 也是如此。

注意:如果我尝试从本地文件系统读取 pdf 而不是通过 post 请求发送它,这可以正常工作。我上传的 pdf 文件绝对有效且不为空。我也知道 webscript 被正确调用,因为它正在发布日志消息、返回 Success 并创建空的 targetFile.pdf。

【问题讨论】:

    标签: java spring pdf alfresco alfresco-webscripts


    【解决方案1】:

    改变这一行:

    outStream.write(buffer2);
    

    收件人:

    outStream.write(buffer);
    

    这是我的 Docker 容器上的 tomcat 目录中显示的内容:

    -rw-r----- 1 root root     117249 Aug  7 19:28 targetFile.pdf
    

    看起来很有效!

    【讨论】:

      猜你喜欢
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多