3

I'm trying to turn an image stored as a Blob in my DB into a MultipartFile to serve back to the client when it's requested. I retrieve the Blob as a byte[] and I'm trying to convert it into a MultipartFile to serve back to the client

I'm trying to do it this way : https://stackoverflow.com/a/25820543/7082628

But IntelliJ is telling me it can't find the MockMultipartFile part when I do the import of: import org.springframework.mock.web.MockMultipartFile
I can import this in a test class no problem, but not outside of the test class. Can I do it here?

Also, I tried to do this by implementing a class with my own version of MultipartFile as stated in another popular answer, but it's telling me it can't find a serialzer.

Any suggestions?

NateH06
  • 3,154
  • 7
  • 32
  • 56

2 Answers2

3

There are couple of issues with your approach.

  1. Multipart file is an HTTP Request format and not intended for response.
  2. For a response all you have to do is write the file to the response.getOutputStream(). Which becomes way easier using Spring

Now coming to your original question that you can't import MockMultipartFile. That's most likely because you're using Maven and the dependency (most likely spring-boot-starter-test) has scope set to test.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope><!-- CHANGE THIS TO "runtime" -->
</dependency>

But again as I said before, there's no need to do that. You don't have to consider the Multipart when you're providing a response, it's a protocol to upload files.

Here's how you can provide a download link to a Blob in Spring

@GetMapping(path = "download")
public ResponseEntity<Resource> download(String param) throws IOException {

    InputStreamResource resource = new InputStreamResource(/* InputStream of blob */);

    long fileLength = /*Length of content, bytes.length or something */

    return ResponseEntity.ok()
            .contentLength(fileLength)
            .contentType(MediaType.APPLICATION_OCTET_STREAM_VALUE)
            .body(resource);
}
11thdimension
  • 10,333
  • 4
  • 33
  • 71
  • Hmm, I guess I'm doing it in this approach because the Image is one field of a DTO that contains a variety of fields that need to be served back to the client (like name, email, address, etc), and the image is one part of that DTO. I could make a new type of DTO, but it would have to store the image as a field, to be handled client-side. Any way I could change that approach? – NateH06 Oct 10 '17 at 19:29
  • ok, is the client browser based? If not then what are you using for the deserialization? – 11thdimension Oct 10 '17 at 19:45
  • The requirements for this particular challenge do not state what the client needs to be. I'm assuming it's browser based, so I'm feeding a template the filled-out DTO in a @ResponseBody. Perhaps there's a different way? – NateH06 Oct 10 '17 at 19:54
  • If it's browser based client and you're planning to use JSON for serialization/deserialization, then you can't pass file object in DTO. As file object can not represented by `JSON`. One hack is to convert file object into `base64` String, which in your case would be ok to since you're returning Image. Base64 string can be directly used as `` – 11thdimension Oct 10 '17 at 20:22
  • As long as I don't have my image attempt in there, it does pass the fields as JSON back to the client though? But I will try this hack... – NateH06 Oct 10 '17 at 20:29
  • Doing it as the Base64 image is really the simplest way to go, I can add that to the DTO as a field and it's good to go. Thanks! – NateH06 Oct 10 '17 at 21:41
0

Finally the package i found which helped with import org.springframework.mock.web.MockMultipartFile;

<properties>
    <java.version>1.8</java.version>
    <spring.version>5.1.2.RELEASE</spring.version>
</properties>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
Parameshwar
  • 856
  • 8
  • 16