0

I need to convert MultipartFile received from client to bytes array to send it to another server(without saving file on first server). I wanted to use MultipartFile.getBytes() function, by it's converting it to something like [B@71336b2e . I'm getting file from request

@RequestMapping(method = RequestMethod.POST)
    public Object doPost(ModelMap model, HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "imageFile", required = false) MultipartFile file) {

and then setter fbImg.setFile(file.getBytes());

Then I wanted to send it to other server in JSON with rest of data, and convert it to BufferedImage. Is there some other method to convert MultipartFile to bytes array, or am I oding something wrong? Or is there simpler method to send images between servers without saving them?

Parys
  • 39
  • 1
  • 6
  • `[B@71336b2e` looks like a byte array. Don't understand your question. – rustyx Sep 05 '16 at 12:17
  • You probably want: http://stackoverflow.com/questions/24339990/how-to-convert-a-multipart-file-to-file – David Newcomb Sep 05 '16 at 12:18
  • What is wrong with your cirrent solution? – talex Sep 05 '16 at 12:20
  • A comment from a security point of view: Doing this someone could easily shut down your service by sending enough files to constantly fill up the memory of your app. – Augusto Sep 05 '16 at 12:21
  • maybe this could be helpful: http://stackoverflow.com/questions/18077072/how-to-convert-multipartfile-into-byte-stream – peech Sep 05 '16 at 12:23
  • @Augusto before I accept any file I'm first checking if it's from one of our servers – Parys Sep 05 '16 at 12:25

2 Answers2

1

'[B@71336b2e' is a toString() output of byte[].

Vlad
  • 471
  • 5
  • 7
  • Ok, that explain a little. But can I then create image from that array on the other server? It wasn't created and I'm not sure if it is my fault or forgotten about something – Parys Sep 05 '16 at 12:24
  • Try to read the image from InputStream e.g. like here: https://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/ – Vlad Sep 05 '16 at 12:33
  • try this: `BufferedImage bufferedImage = ImageIO.read(file.getInputStream())` – Optio Sep 05 '16 at 14:39
0

'[B@71336b2e' is a toString() output of byte[]. if you want real file convert it into base64 or change it into file and content type

mr noface
  • 23
  • 3