java - 如何通过 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;
}
}
'
解决方案
推荐阅读
- python - __init__() 得到了一个意外的关键字参数“extra_args”`
- powerbi - 索引列 2 的倍数
- angular - Angular 订阅方法的值未定义
- react-native - React Native 中的 this.setState 超出了最大更新深度
- terraform - terraform 13 到 14 不支持的属性 aws_provider
- html - 在 Gatsby 中使用带有 imgStyle 的 CSS
- c++ - 有没有办法在销毁链表时取消引用 nullptr 时停止接收异常错误?
- html - Div 模块左右边距在响应式设计中不起作用
- javascript - 从文本框中获取值不会给出单行字符串?
- php - 连接错误 08001:[unixODBC][Teradata][ODBC] (10380) 无法与数据源建立连接。缺少设置:PHP 中的 {[DBCName]}