6

I am trying to pass few parameters, which includes byte[] as one, to a Rest Service. In the Service method when I consume the parameter and construct a File out of it ...I see a corrupted file. Below is my code:

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

RestController:

@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();
    }
}

Can any one let me know what is the error over here?

I am testing my service by passing input as RAW Data in the form of JSON using Post Man.

Thanks.

4
  • public void setIn(byte[] in) Did you set the byte[] from this function ? & did u printed the size of obj.geIn before wrtiting? Commented Oct 21, 2016 at 10:00
  • Yes. And it is printing the Size properly. Commented Oct 21, 2016 at 10:51
  • try adding fos.flush(); Commented Oct 21, 2016 at 11:26
  • one strange thing what I observed was..byte array which I am passing as an input is "5049504913104950491310504950131049501310495013104950" . When the control comes to service byte array contains "[-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]"..Input I am passing is getting corrupted or getting converted to another format set I guess. Am I correct? Any help in resolving this issue? Commented Oct 21, 2016 at 12:03

2 Answers 2

2

You can encode your byte array into Base64 string, then decode it back to byte array after receiving in controller.

Sign up to request clarification or add additional context in comments.

2 Comments

I did the same already..but no luck..this is what I had done:
@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); try{ FileOutputStream fos = new FileOutputStream(new File("D:\\Test.txt")); fos.write(bytes); fos.close(); } catch(Exception e) { } }
1

By given code, I am guessing that you are trying to upload a file. If that is the case you can simply upload file and accept them in controller as mentioned in below example where we are accepting one customer object and one file object

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

2 Comments

Agree with you. But because of some limitation I am unable to use MultipartFile option. Thank you for your suggestion.
Do you remember how you get to resolve this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.