我需要使用泽西2通过rest调用上传一个二进制文件。
我已经写了以下的课:
@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();
}
}当我试图做以下事情时:
curl --request POST --data-binary "@file.txt" http://localhost:8080/entry-point/upload我得到以下答复:
<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>我哪里错了?
发布于 2016-01-05 10:13:10
我已经解决了删除@Consumes注释的问题。
像这样
public String uploadStream(InputStream payload) throws IOException {}发布于 2017-03-27 07:52:45
服务器端看起来没问题。如错误中所述,添加mime类型的内容解决了我的问题:
-H 'Content-Type: application/octet-stream'
发布于 2015-12-23 16:21:07
您应该使用MediaType.MULTIPART_FORM_DATA向泽西岛表示您将要接收的是一个文件。此外,你必须“提示”球衣哪个字段应该是你想要阅读的。修正后的版本如下:
@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?
https://stackoverflow.com/questions/34439505
复制相似问题