首页 > 解决方案 > 如何通过 REST API Mule 4 上传 zip 文件?

问题描述

我正在尝试将 zip 文件上传到网址https://anypoint.mulesoft.com/designcenter/api-designer/projects/{projectId}/branches/master/import。Content-Type 必须是 application/zip,不能​​更改为 multipart/form-data。在 Mule 3 中,使用了一个 java 转换类 (com.test.FileReader),FileReader.class 存储在 lib 中。它在 Mule 3 中工作。我尝试使用 ReadFile 组件来读取 test.zip 并设置为有效负载,但它不起作用。有什么建议如何在 Mule 4 中上传 zip 文件吗?

package com.test;
 
import org.mule.transformer.*;
import org.mule.api.*;
import org.mule.api.transformer.*;
import java.io.*;
 
public class PayloadFileReader extends AbstractMessageTransformer
{
    public Object transformMessage(final MuleMessage message, final String outputEncoding) throws TransformerException {
        byte[] result = null;
        try {
            result = this.readZipFile("test.zip");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        message.setPayload((Object)result);
        return message;
    }
    
    public String readFileTest(final String path) throws FileNotFoundException, IOException, Exception {
        final ClassLoader classLoader = this.getClass().getClassLoader();
        final File file = new File(classLoader.getResource(path).getFile());
        final FileReader fileReader = new FileReader(file);
        BufferedReader bufferReader = null;
        final StringBuilder stringBuffer = new StringBuilder();
        try {
            bufferReader = new BufferedReader(fileReader);
            String line;
            while ((line = bufferReader.readLine()) != null) {
                stringBuffer.append(line);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
            if (bufferReader != null) {
                try {
                    bufferReader.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        finally {
            if (bufferReader != null) {
                try {
                    bufferReader.close();
                }
                catch (IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
        return stringBuffer.toString();
    }
    
    public byte[] readZipFile(final String path) {
        final ClassLoader classLoader = this.getClass().getClassLoader();
        final File file = new File(classLoader.getResource(path).getFile());
        final byte[] b = new byte[(int)file.length()];
        try {
            final FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read(b);
            fileInputStream.close();
        }
        catch (FileNotFoundException e) {
            System.out.println("Not Found.");
            e.printStackTrace();
        }
        catch (IOException e2) {
            System.out.println("Error");
            e2.printStackTrace();
        }
        return b;
    }
}

'

标签: javamule

解决方案


假设你的 zip 文件对应一个有效的 API 规范,在 Mule 4 中,你不需要使用自定义的 java 代码来实现你想要的:你可以使用 File 连接器读取文件内容读取操作,并使用 HTTP请求使用设计中心 API 将其上传到设计中心。您的流程应如下所示:

设计中心上传流程

对于读取操作,您只需在文件路径操作属性中设置文件位置。

无需在 HTTP 请求中设置内容类型(Mule 4 会根据读取操作加载的文件内容自动配置内容类型)。


推荐阅读