How to upload file to Oracle DB as Blob/Clob using Helidon MP REST Services

872 Views Asked by At

I am new to Helidon MP and would like to know if there is a way to upload files to Oracle DB (Blob/Clob Column) via REST service created using Helidon MP.

I am able to acheive the same requirement in SpringBoot using below code accepting the file parameter as MultiPart, how can we acheive this using Helidon MP

@PostMapping("/upload")
      public ResponseEntity<ResponseMessage> uploadFile(@RequestParam("file") MultipartFile file) {
        String message = "";
        try {
          storageService.store(file);

          message = "Uploaded the file successfully: " + file.getOriginalFilename();
          return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
        } catch (Exception e) {
          message = "Could not upload the file: " + file.getOriginalFilename() + "!";
          return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
        }
      }    
1

There are 1 best solutions below

1
On

Helidon offers support for multi-part data through both MP and SE (with buffered and streamed API version for SE) flavors.

The implementation relies on Jersey Integration with MIME MultiPart messages (would be quite similar to any JAX-RS provider).

An implementation of a controller endpoint method allowing to upload files is straightforward and looks like below:

import import javax.ws.rs.*;
import org.glassfish.jersey.media.multipart.*;

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(MultiPart multiPart) throws IOException {

    multiPart.getBodyParts().stream()
            .filter(part -> /* retrieve your file part here, should be based on the `Content-Disposition` name parameter */ )
            .map(filePart -> {
                String fileName  = pfilePartart.getContentDisposition().getFileName();
                InputStream is = filePart.getEntityAs(BodyPartEntity.class).getInputStream();
                // use the injected `storageService` to somehow store the input stream
                this.storageService.store(fileName, is);
            });
    
}

Note that the JAX-RS multi-part feature is not activated by default, hence should be activated using JAX-RS providers:

import javax.ws.rs.core.*;
import javax.ws.rs.ext.Provider;

@Provider
public class MultiPartFeatureProvider implements Feature {

    @Override
    public boolean configure(FeatureContext context) {
        return new MultiPartFeature().configure(context);
    }
}