我正在尝试将压缩文件上传到url https://anypoint.mulesoft.com/designcenter/api-designer/projects/{projectId}/branches/master/import。Content-Type必须是application/zip,不能更改为multipart/form-data。在Mule3中,使用了一个java transform类(com.test.FileReader),并将FileReader.class存储在库中。我尝试使用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;
}
}
‘
发布于 2021-03-05 20:27:03
假设您的zip文件对应于一个有效的API规范,在Mule4中,您不需要使用自定义java代码来实现您想要的内容:您可以使用File connector Read操作读取文件内容,并使用一个HTTP请求通过Design Center API将其上传到Design Center。你的流程应该是这样的:
对于读取操作,您只需在file Path操作属性中设置文件位置。
不需要在HTTP请求中设置内容类型(Mule4会根据读取操作加载的文件内容自动配置内容类型)。
发布于 2021-03-05 20:36:59
在Mule 4中,你不能使用依赖于Mule 3类的Java代码。不要费心去修改代码,它并不能正常工作。它们的架构只是不同而已。
而在Mule 4中,你可以使用普通的Java代码或者用SDK创建一个模块,对于这个问题没有理由这样做,而且会适得其反。我的建议是忘记Java代码,用纯Mule 4组件来解决这个问题。
在这种情况下,似乎不需要实际使用Java代码。File连接器读取操作应该可以很好地读取文件,因为看起来Java代码除了将文件读入有效负载之外,并没有执行任何其他操作。
通过HTTP请求连接器发送应该很简单。您没有提供错误的任何细节(错误发生的位置、完整的错误消息、HTTP状态错误代码、两个版本中的HTTP请求的完整流程等),而且API Designer REST API没有记录import
端点,因此很难判断请求的构造是否正确。
https://stackoverflow.com/questions/66490754
复制相似问题