2013-07-30 25 views
0

我會先放下我的代碼:注意:我也在問題底部有日誌輸出。爲什麼我的標題在POST後是空的?

服務器端:

@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的頭文件似乎就在那裏。這是否意味着我應該能夠從服務器端代碼中檢索值?

+1

打開日誌記錄('client.addFilter(new LoggingFilter());')並驗證發出的請求實際上是否包含標頭。你使用'ClientResponse'是正確的,因爲你的POST的_result_是一個響應而不是一個請求。 – DannyMo

+0

我在過濾器中有標題。我會將其添加到上面的問題或新的問題中。 –

回答

1

你是參數是錯誤的類型。您應該將該參數聲明爲一個String。 HttpHeaders用於獲取所有標題,並用@Context註釋。 @HttpParam只能轉換爲有限數量的類型。

從HeaderParam的澤西文檔。

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

當使用String和偶對象時,我得到'null'。我使用了@HeaderParam(value =「Content- Disposition」)字符串contentDisposition',它仍然是'null'。 –

+0

@StephenD您可以嘗試使用上下文註釋注入HttpHeaders實例,然後打印出所有標題。我沒有使用澤西島的客戶端庫,但它看起來像你正確地發送標題。然後確保標題的拼寫匹配。 –

+0

由於某些原因,即使用'@ Context'抓住它也是'null'。我試過重命名,但仍然沒有運氣。 –

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'檢索相同的值。

我希望這會引導你走向正確的方向。

+0

我感謝您的回覆。我做了反映你的意見的編輯,但沒有運氣。 –