gpt4 book ai didi

java - 为什么我的标题在 POST 后为空?

转载 作者:行者123 更新时间:2023-12-01 14:17:48 25 4
gpt4 key购买 nike

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

服务器端:

@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));

}

首先,文件传输正常,我很高兴。但是,我想获取服务器端的 header ,这样我就不必对文件名进行硬编码。关于为什么它会出现空值有什么想法吗?这与我使用 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 的 header 就在那里。这是否意味着我应该能够从服务器端代码检索该值?

最佳答案

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

来自 HeaderParam 的 Jersey 文档。

Binds the value(s) of a HTTP header to a resource method parameter, resource class field, or resource class bean property. A default value can be specified using the DefaultValue annotation. The type T of the annotated parameter, field or property must either:

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!");
}

关于java - 为什么我的标题在 POST 后为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17952298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com