为什么POST后我的标题为空?

时间:2013-07-30 16:19:02

标签: java rest file-upload jersey jetty

我将首先放下我的代码:注意:我还在问题的底部有日志输出。

服务器端:

@Post
@Consumes("application/octet-stream")
public Representation post(InputStream zip, @HeaderParam(value = "Content-Disposition") HttpHeaders headers) throws Throwable {
    System.out.println(headers); //Prints null - want the header to not be null here
    String uploadedFileLocation = getStartingDir() + "/" + "abc.zip";
    writeToFile(zip, uploadedFileLocation); 
    return new StringRepresentation("Uploaded!");
}

客户端:

public static void main(String[] args) throws Exception {
    final String BASE_URI = "http://localhost:8080/server/upload";

    Client client = Client.create();
    WebResource service = client.resource(BASE_URI);
    client.setChunkedEncodingSize(1024);
    client.addFilter(new LoggingFilter());


    File zip = new File("C:/Users/sdery/Desktop/abc.zip");

    InputStream fileInStream = new FileInputStream(zip);
    String sContentDisposition = "attachment; filename=\"" + zip.getName()+"\"";     
    ClientResponse response = service.header("Authorization", "Basic xxx=").header("Content-Disposition", (Object)sContentDisposition).type(MediaType.APPLICATION_OCTET_STREAM).post(ClientResponse.class, fileInStream);     
    System.out.println("Response Status : " + response.getEntity(String.class));

}

首先,文件传输有效,我很高兴。但是,我想在服务器端获取标头,所以我不必硬编码文件名。关于为什么它变得无效的任何想法?是否与我使用ClientResponse而不是ClientRequest

有关
Jul 31, 2013 8:44:12 AM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client out-bound request
1 > POST http://localhost:8080/server/upload
1 > Content-Disposition: attachment; filename="abc.zip"
1 > Authorization: Basic xxx=
1 > Content-Type: application/octet-stream

(zip bytes)

INFO: 1 * Client in-bound response
1 < 200
1 < Date: Wed, 31 Jul 2013 12:44:12 GMT
1 < Date: Wed, 31 Jul 2013 12:44:12 GMT
1 < Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
1 < Content-Length: 88
1 < Set-Cookie: rememberMe=deleteMe; Path=/server; Max-Age=0; Expires=Tue, 30-Jul-2013   12:44:12 GMT
1 < Content-Type: text/plain; charset=UTF-8
1 < Accept-Ranges: bytes
1 < Server: Restlet-Framework/2.0.4
1 < Real-Token: bar
1 < 
Uploaded!

从日志输出中,似乎包含Content-Disposition的标题就在那里。这是否意味着我应该能够从服务器端代码中检索值?

2 个答案:

答案 0 :(得分:1)

您的参数类型错误。您应该将参数声明为String。 HttpHeaders用于获取所有头文件,并使用@Context进行注释。 @HttpParam只能转换为有限数量的类型。

来自HeaderParam的Jersey文档。

  

将HTTP标头的值绑定到资源方法参数,资源类字段或资源类bean属性。可以使用DefaultValue批注指定默认值。注释参数,字段或属性的类型T必须:

Be a primitive type
Have a constructor that accepts a single String argument
Have a static method named valueOf or fromString that accepts a single String argument (see, for example, Integer.valueOf(String))
Be List<T>, Set<T> or SortedSet<T>, where T satisfies 2 or 3 above. The resulting collection is read-only.

所以你的代码更像是

@Post
@Consumes("application/octet-stream")
public Representation post(InputStream zip, @HeaderParam(value = "Content-  Disposition") String contentDisposition) throws Throwable {
   System.out.println(contentDisposition); 
   String uploadedFileLocation = getStartingDir() + "/" + "abc.zip";
   writeToFile(zip, uploadedFileLocation); 
   return new StringRepresentation("Uploaded!");
}

答案 1 :(得分:0)

首先,我很抱歉我的解决方案来自JavaScript / PHP参考而不是Java,但我相信您的解决方案可能类似。

添加名为“X-FILENAME”的新标头,并将文件名设置为标题数据。我相信你的代码看起来像这样:

ClientResponse response = service.header("X-FILENAME", "abc.zip");

然后,在您的服务器上,您应该能够检索该标头参数(在PHP中它是$_SERVER全局,它看起来像在你的@HeaderParam}。

另外,仅供参考,以防万一这适用于您,在PHP中检索标题参数时,您需要使用修改后的参数名称,方法是在前面添加“HTTP_”并将所有破折号更改为下划线,如“HTTP_X_FILENAME” 。因此,在客户端上,您在服务器上发送了“X-FILENAME”,并使用“HTTP_X_FILENAME”检索相同的值。

我希望这会引导你朝着正确的方向前进。