【问题标题】:Spring REST API for posting jar file用于发布 jar 文件的 Spring REST API
【发布时间】:2015-06-26 19:23:54
【问题描述】:

我正在使用 RESTful 端点进行一些文件操作。我想通过 REST 向我的服务发布一个 jar 文件,我尝试了以下方法但仍然失败,我几乎尝试谷歌搜索但找不到任何解决方案。

@RestController
public class MyController {
 ...

@RequestMapping(value="/jobs/upload", method=RequestMethod.POST)
public @ResponseBody ResponseEntity<Void> handleFileUpload(HttpEntity<byte[]> requestEntity){
    byte[] payload = requestEntity.getBody();
    InputStream logo = new ByteArrayInputStream(payload);
    HttpHeaders headers = requestEntity.getHeaders();
    return ResponseEntity.ok().build();
 }
...
}

卷曲命令curl -X POST --data-binary @/Users/path/to-jar/test-jar.jar localhost:8008/ctx/jobs/upload

[EDIT] :如果我必须通过 --data-binary 实现,我的代码应该是什么样子?

我无法继续进行,任何人都可以帮忙。我在 MultiPart 上看到了很多解决方案,但我无法适应它。

【问题讨论】:

  • 这里有一个关于这个问题的很好的教程:mkyong.com/spring-mvc/spring-mvc-file-upload-example
  • @eyp :我更关注通过 Spring 与 RESTController 的集成。 Multipart 我必须进入我不需要的 Spring MVC。
  • 您的控制器已经在使用 Spring MVC(尽管您可能不会像使用相同的组件和基础架构那样体验它)。
  • @M.Deinum :是的,但我唯一需要的是我想用 --data-binary 作为 curl 发布。我该怎么做? Multipart 采用 RequestParam,这实际上是我最后的手段。

标签: spring rest post spring-4


【解决方案1】:

您需要为 MUltipart 配置一个 servlet:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="200000"/>
</bean>

然后在你的 REST 服务中

@RequestMapping(value="/jobs/upload", method=RequestMethod.POST)
@ResponseBody
public ResponseEntity<Void> handleFileUpload(@RequestParam("file") MultipartFile multipartFile,
                                             HttpServletRequest request) {
    ...
}

【讨论】:

  • 好的,让我试试这个。但是我必须使用 curl 发布为 --data-binary 的内容?我该怎么做?
  • 为什么需要使用该选项发帖? curl -F "file=@file.jar" localhost:8080/jobs/upload
  • 是的,会 curl -f 并尝试 :)
  • 接受你的回答,因为它对我来说是开箱即用的 Springboot。感谢您的快速帮助。
【解决方案2】:

试试下面的解决方案

@RequestMapping(value = APIURIConstants.GET_JAR, method = RequestMethod.GET)
    public ResponseEntity<byte[]> getJar(

            HttpServletRequest request) {
        ServletContext context = request.getServletContext();
        String fullPath = objPollBookService.getSignatureFileName(electionID);
        System.out.println("Path=" + fullPath);

        byte[] content = null;
        try {
            URL link = new URL(fullPath);
            InputStream in = new BufferedInputStream(link.openStream());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int n = 0;
            while (-1 != (n = in.read(buf))) {
                out.write(buf, 0, n);
            }
            out.close();
            in.close();
            content = out.toByteArray();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        HttpHeaders headers = new HttpHeaders();
        String mimeType = context.getMimeType(fullPath);
        if (mimeType == null) {
            mimeType = "application/octet-stream";
        }
        headers.setContentType(MediaType.parseMediaType(mimeType));
        String filename = FilenameUtils.getBaseName(fullPath);
        headers.setContentDispositionFormData(filename, filename);
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(content,
                headers, HttpStatus.OK);
        return response;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-25
    • 2019-01-10
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 2016-08-19
    相关资源
    最近更新 更多