首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与泽西岛2上传文件

与泽西岛2上传文件
EN

Stack Overflow用户
提问于 2015-12-23 16:02:40
回答 3查看 1.2K关注 0票数 2

我需要使用泽西2通过rest调用上传一个二进制文件。

我已经写了以下的课:

代码语言:javascript
复制
@Path("/entry-point")
public class EntryPoint {

    @POST
    @Path("/upload")
    @Consumes(MediaType.APPLICATION_OCTET_STREAM)
    public String uploadStream( InputStream payload ) throws IOException
    {
        while(true) {
            try {
                DataInputStream dis = new DataInputStream(payload);
                System.out.println(dis.readByte());
            } catch (Exception e) {
                break;
            }
        }
        //Or you can save the inputsream to a file directly, use the code, but must remove the while() above.
        /**
         OutputStream os =new FileOutputStream("C:\recieved.jpg");
         IOUtils.copy(payload,os);
         **/
        System.out.println("Payload size="+payload.available());
        return "Payload size="+payload.available();
    }

}

当我试图做以下事情时:

代码语言:javascript
复制
curl --request POST --data-binary "@file.txt" http://localhost:8080/entry-point/upload

我得到以下答复:

代码语言:javascript
复制
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 415 </title>
</head>
<body>
<h2>HTTP ERROR: 415</h2>
<p>Problem accessing /entry-point/up. Reason:
<pre>    Unsupported Media Type</pre></p>
<hr /><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.3.6.v20151106</a><hr/>
</body>
</html>

我哪里错了?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-01-05 10:13:10

我已经解决了删除@Consumes注释的问题。

像这样

代码语言:javascript
复制
public String uploadStream(InputStream payload) throws IOException {}
票数 0
EN

Stack Overflow用户

发布于 2017-03-27 07:52:45

服务器端看起来没问题。如错误中所述,添加mime类型的内容解决了我的问题:

-H 'Content-Type: application/octet-stream'

票数 1
EN

Stack Overflow用户

发布于 2015-12-23 16:21:07

您应该使用MediaType.MULTIPART_FORM_DATA向泽西岛表示您将要接收的是一个文件。此外,你必须“提示”球衣哪个字段应该是你想要阅读的。修正后的版本如下:

代码语言:javascript
复制
 @Consumes(MediaType.MULTIPART_FORM_DATA)
 public String uploadStream( @FormDataParam("file") InputStream payload ) throws IOException {

请注意,MediaType.APPLICATION_OCTET_STREAM是一个通用的MediaType。有关更详细的解释,请检查:Do I need Content-Type: application/octet-stream for file download?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34439505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档