【问题标题】:How to pass byte array as a parameter in Spring Rest API如何在 Spring Rest API 中将字节数组作为参数传递
【发布时间】:2016-10-21 09:57:25
【问题描述】:

我正在尝试将几个参数(包括 byte[] 作为一个参数)传递给 Rest Service。在服务方法中,当我使用参数并从中构造一个文件时......我看到一个损坏的文件。 以下是我的代码:

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;
    }

休息控制器:

@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 形式将输入作为原始数据传递来测试我的服务。

谢谢。

【问题讨论】:

  • public void setIn(byte[] in) 你是否通过这个函数设置了 byte[] ? & 你在写之前打印了 obj.geIn 的大小吗?
  • 是的。它正在正确打印尺寸。
  • 尝试添加 fos.flush();
  • 我观察到的一件奇怪的事情是..我作为输入传递的字节数组是 "5049504913104950491310504950131049501310495013104950" 。当控件来服务字节数组包含“[-25, 78, 61, -25, 78, 61, -41, 125, 116, -29, -34, 116, -29, -35, 119, -41 , 78, 116, -29, -34, 116, -41, 125, 116, -29, -34, 116, -41, 125, 116, -29, -34, 116, -41, 125, 116, -29, -34, 116]"..我传递的输入已损坏或转换为我猜的另一种格式集。我对么?解决这个问题有什么帮助吗?

标签: java spring rest


【解决方案1】:

您可以将字节数组编码为 Base64 字符串,然后在控制器接收后将其解码回字节数组。

【讨论】:

  • 我已经做了同样的事情..但没有运气..这就是我所做的:
  • @RequestMapping(value="/byteUpload", method=RequestMethod.POST) public void getValues(@RequestParam(value="str1", required=true) String str1, @RequestParam(value=" str2", required=true) String str2) { byte [] bytes=Base64.decodeBase64(str1); System.out.println(str1); System.out.println(str2);尝试{ FileOutputStream fos = new FileOutputStream(new File("D:\\Test.txt")); fos.write(字节); fos.close(); } 捕捉(异常 e){ } }
【解决方案2】:

根据给定的代码,我猜您正在尝试上传文件。如果是这种情况,您可以简单地上传文件并在控制器中接受它们,如下例所述,我们接受一个客户对象和一个文件对象

@RequestMapping(value = "/registration/uploadFile", method = RequestMethod.POST)
public Customer saveFile(@RequestPart("customer") @Valid Customer customer, @RequestPart("file") @Valid MultipartFile file) {
    return customerService.saveFile(customer, file);
}

【讨论】:

  • 同意你的看法。但由于某些限制,我无法使用 MultipartFile 选项。谢谢你的建议。
  • 你还记得你是如何解决这个问题的吗?
猜你喜欢
  • 1970-01-01
  • 2016-10-28
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多