Pentaho - 使用API​​上传文件

时间:2018-02-27 04:40:34

标签: pentaho pdi spoon

我需要使用API​​上传文件。

我尝试过REST CLIENT并没有找到任何选项。 尝试使用HTTP POST,并以415响应。

请建议如何完成此操作

2 个答案:

答案 0 :(得分:0)

我使用http://www.dietz-solutions.com/2017/06/pentaho-data-integration-multi-part.html

中的解决方案解决了这个问题

谢谢Ben。

他为多部分表单提交了一个Java类。我通过为Authorization添加标题来扩展...

答案 1 :(得分:0)

此解决方案仅使用jre 7的标准类。在转换中添加步骤Modified Java Script Value。您将必须在流中添加两列:URL_FORM_POST_MULTIPART_COLUMNFILE_URL_COLUMN,可以根据需要添加任意数量的文件,而您只需要多次调用outputStreamToRequestBody.write

    try
    {
        //in this step you will need to add two columns from the previous flow -> URL_FORM_POST_MULTIPART_COLUMN, FILE_URL_COLUMN 
        var serverUrl =  new java.net.URL(URL_FORM_POST_MULTIPART_COLUMN);

        var boundaryString = "999aaa000zzz09za";
        var openBoundary  = java.lang.String.format("\n\n--%s\nContent-Disposition: form-data\nContent-Type: text/xml\n\n" , boundaryString);
        var closeBoundary = java.lang.String.format("\n\n--%s--\n", boundaryString);

        // var netIPSocketAddress = java.net.InetSocketAddress("127.0.0.1", 8888);
        // var proxy = java.net.Proxy(java.net.Proxy.Type.HTTP , netIPSocketAddress);
        // var urlConnection = serverUrl.openConnection(proxy);
        var urlConnection = serverUrl.openConnection();

        urlConnection.setDoOutput(true); // Indicate that we want to write to the HTTP request body
        urlConnection.setRequestMethod("POST");
        //urlConnection.addRequestProperty("Authorization", "Basic " + Authorization);
        urlConnection.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundaryString);


        var outputStreamToRequestBody = urlConnection.getOutputStream();

        outputStreamToRequestBody.write(openBoundary.getBytes(java.nio.charset.StandardCharsets.UTF_8));
        outputStreamToRequestBody.write(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(FILE_URL_COLUMN)));
        outputStreamToRequestBody.write(closeBoundary.getBytes(java.nio.charset.StandardCharsets.UTF_8));

        outputStreamToRequestBody.flush();


        var httpResponseReader = new java.io.BufferedReader(new java.io.InputStreamReader(urlConnection.getInputStream()));

        var lineRead = "";
        var finalText = "";
        while((lineRead = httpResponseReader.readLine()) != null) {
            finalText += lineRead;
        }

        var status = urlConnection.getResponseCode();
        var result = finalText;
        var time   = new Date();

    }
    catch(e)
    {
        Alert(e);
    }