【问题标题】:How to upload file to Oracle DB as Blob/Clob using Helidon MP REST Services如何使用 Helidon MP REST 服务将文件作为 Blob/Clob 上传到 Oracle DB
【发布时间】:2020-11-30 14:24:08
【问题描述】:

我是 Helidon MP 的新手,想知道是否可以通过使用 Helidon MP 创建的 REST 服务将文件上传到 Oracle DB(Blob/Clob 列)。

我可以使用下面的代码在 SpringBoot 中实现相同的要求,接受文件参数作为 MultiPart,我们如何使用 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));
        }
      }    

【问题讨论】:

  • 来自“关于”页面:“Helidon 是用于编写微服务的 Java 库的集合......”所以当然有办法做到这一点,但是你需要提供有关您尝试了什么以及您的代码/尝试有什么问题的更多详细信息。
  • 您好,我使用 SpringBoot 完成了相同的要求,我可以通过将参数作为 MultipartFile 接受并对其进行相应的处理来实现相同的要求。在 Helidon MP 中,我不确定我们如何接受该文件,因为 @RequestParam 是 SpringBoot 组件。下面的代码显示了我如何使用 SpringBoot 实现相同的目标
  • public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) { String message = "";尝试 { storageService.store(file); message = "上传文件成功:" + file.getOriginalFilename(); return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message)); } catch (Exception e) { message = "无法上传文件:" + file.getOriginalFilename() + "!";返回 ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message)); } }
  • 请通过edit将此信息添加到问题中,而不是cmets。

标签: java oracle rest file helidon


【解决方案1】:

Helidon 通过 MPSE 提供对 multi-part 数据的支持(带有缓冲和流式 API SE) 口味的版本。

实现依赖于 Jersey 与 MIME MultiPart 消息的集成(与任何 JAX-RS 提供程序非常相似)。

允许上传文件的控制器端点方法的实现很简单,如下所示:

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

请注意,JAX-RS 多部分功能默认未激活,因此应使用 JAX-RS 提供程序激活:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-09-24
  • 1970-01-01
  • 2012-01-26
  • 2010-10-25
  • 2010-09-30
  • 1970-01-01
  • 2018-10-27
相关资源
最近更新 更多