添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

You shouldn't use that as that is for testing only. Just stream the byte[] directly to the client using an OutputStream . M. Deinum Oct 10, 2017 at 19:51
  • Multipart file is an HTTP Request format and not intended for response .
  • 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);
                    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, 2017 at 19:29
                    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, 2017 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 <img src="data:image/png;base64, base64_string....">
    – 11thdimension
                    Oct 10, 2017 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, 2017 at 20:29
    

    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>
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.