我正在尝试将几个参数(包括 byte[] 作为一个参数)传递给 Rest 服务。在 Service 方法中,当我使用参数并从中构造一个 File 时...我看到一个损坏的文件。 下面是我的代码:
public class MultiParameters {
@JsonProperty(value="strName")
public String strName;
@JsonProperty(value="in")
public byte[] in;
public String strName2;
public String getStrName2() {
return strName2;
}
public void setStrName2(String strName2) {
this.strName2 = strName2;
}
public String getStrName() {
return strName;
}
public void setStrName(String strName) {
this.strName = strName;
}
public byte[] getIn() {
return in;
}
public void setIn(byte[] in) {
this.in = in;
}
休息 Controller :
@RequestMapping(value= "/upload", method = RequestMethod.POST)
public void upload(@RequestBody MultiParameters obj){
try {
System.out.println("str name : "+obj.getStrName());
System.out.println("str name2 : "+obj.getStrName2());
System.out.println("bytes lenghts : "+obj.getIn());
FileOutputStream fos = new FileOutputStream(new File("D:\\Test.txt"));
fos.write(obj.getIn());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
谁能告诉我这里的错误是什么?
我正在通过使用 Post Man 以 JSON 形式将输入作为 RAW 数据传递来测试我的服务。
谢谢。
最佳答案
您可以将字节数组编码为 Base64 字符串,然后在 Controller 接收后将其解码回字节数组。
关于java - 如何在 Spring Rest API 中将字节数组作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40173624/