使用 Jersey 2 上传文件
Upload File with Jersey 2
我需要使用 Jersey 2 通过 rest 调用上传二进制文件
我写了以下内容class:
@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>
我哪里错了?
您应该使用 MediaType.MULTIPART_FORM_DATA 向 Jersey 表明您将要接收的是一个文件。另外,你还得 'hint' jersey 哪个领域应该是你想看的。更正后的版本为:
@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?
我已决定删除 @Consumes
注释。
像这样
public String uploadStream(InputStream payload) throws IOException {}
服务器端看起来不错。添加错误中所述的内容 mime 类型为我解决了问题:
-H 'Content-Type: application/octet-stream'
我需要使用 Jersey 2 通过 rest 调用上传二进制文件
我写了以下内容class:
@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>
我哪里错了?
您应该使用 MediaType.MULTIPART_FORM_DATA 向 Jersey 表明您将要接收的是一个文件。另外,你还得 'hint' jersey 哪个领域应该是你想看的。更正后的版本为:
@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?
我已决定删除 @Consumes
注释。
像这样
public String uploadStream(InputStream payload) throws IOException {}
服务器端看起来不错。添加错误中所述的内容 mime 类型为我解决了问题:
-H 'Content-Type: application/octet-stream'