0

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

服务器端:

@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在那里。这是否意味着我应该能够从服务器端代码中检索值?

4

2 回答 2

1

您的参数类型错误。您应该将参数声明为字符串。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!");
}
于 2013-07-30T16:33:42.423 回答
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”检索相同的值。

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

于 2013-07-30T16:52:43.577 回答