2012-11-22 191 views
2

我試圖通過HTTP Post將圖像上傳到SmugMug,因爲它們的documentation。我把所有的標題是正確的,但令人困惑的說我是設置在機身的二進制數據的部分:通過發佈請求上傳文件

這種方法需要在身體 二進制數據和其他所有元數據的POST請求標題。

我曾嘗試:

  • SMResponse response = builder.post(SMResponse.class, Files.readAllBytes(image.toPath())); 
    
  • SMResponse response = builder.post(SMResponse.class, new String(Files.readAllBytes(image.toPath()))); 
    
  • SMResponse response = builder.post(SMResponse.class, new String(Base64.encode(Files.readAllBytes(image.toPath())))); 
    
  • SMResponse response = builder.post(SMResponse.class, Base64.encode(Files.readAllBytes(image.toPath()))); 
    

我最好的猜測是第一位的工作,但所有這些回報:

{"stat":"fail","method":"smugmug.images.upload","code":5,"message":"system error"} 

這裏是不會上傳,萬一完整的方法我錯過了一些東西:

public boolean upload(File image, int albumId, String caption, String keywords, 
     Boolean hidden, Integer imageId, Integer altitude, Float latitude, 
     Float longitude, boolean pretty) throws IOException, InvalidKeyException, NoSuchAlgorithmException, SmugMugException { 
    logger.debug("upload() called"); 
    byte[] imageBytes = Files.readAllBytes(image.toPath()); 

    WebResource resource = SmugMugAPI.CLIENT.resource("http://upload.smugmug.com/"); 

    LoggingFilter logFilter = new LoggingFilter(); 
    resource.addFilter(logFilter); 

    OAuthSecrets secrets = new OAuthSecrets().consumerSecret(smugmug.getConsumerSecret()); 
    OAuthParameters oauthParams = new OAuthParameters().consumerKey(smugmug.getCosumerKey()). 
      signatureMethod("HMAC-SHA1").version("1.0"); 
    // Create the OAuth client filter 
    OAuthClientFilter filter = new OAuthClientFilter(SmugMugAPI.CLIENT.getProviders(), oauthParams, secrets); 
    // Add the filter to the resource 
    if (smugmug.getToken() != null){ 
     secrets.setTokenSecret(smugmug.getToken().getSecret()); 
     oauthParams.token(smugmug.getToken().getId()); 
    } 
    resource.addFilter(filter); 

    WebResource.Builder builder = resource.getRequestBuilder(); 
    //User agent 
    builder = builder.header("User-Agent", smugmug.getAppName()); 
    //API Version header 
    builder = builder.header("X-Smug-Version", "1.3.0"); 
    //Response Type header 
    builder = builder.header("X-Smug-ResponseType", "JSON"); 
    //Content-Length header 
    builder = builder.header("Content-Length", Long.toString(image.length())); 
    //Content-MD5 header 
    builder = builder.header("Content-MD5", DigestUtils.md5Hex(imageBytes)); 
    //X-Smug-FileName header 
    builder = builder.header("X-Smug-FileName", image.getName()); 
    //X-Smug-AlbumID header 
    builder = builder.header("X-Smug-AlbumID", Integer.toString(albumId)); 
    //X-Smug-Caption header 
    if(caption != null){ 
     builder = builder.header("X-Smug-Caption", caption); 
    } 
    //X-Smug-Caption header 
    if(keywords != null){ 
     builder = builder.header("X-Smug-Keywords", keywords); 
    } 
    //X-Smug-Hidden header 
    if(hidden != null){ 
     builder = builder.header("X-Smug-Hidden", hidden.toString()); 
    } 
    //X-Smug-ImageID header 
    if(imageId != null){ 
     builder = builder.header("X-Smug-ImageID", imageId.toString()); 
    } 
    //X-Smug-Altitude header 
    if(altitude != null){ 
     builder = builder.header("X-Smug-Altitude", altitude.toString()); 
    } 
    //X-Smug-Latitude header 
    if(latitude != null){ 
     builder = builder.header("X-Smug-Latitude", latitude.toString()); 
    } 
    //X-Smug-Latitude header 
    if(longitude != null){ 
     builder = builder.header("X-Smug-Longitude", longitude.toString()); 
    } 
    //X-Smug-Pretty header 
    if(pretty){ 
     builder = builder.header("X-Smug-Pretty", Boolean.toString(pretty)); 
    } 

    SMResponse response = builder.post(SMResponse.class, new String(imageBytes)); 
    if (!"ok".equals(response.getStat())) { 
     throw new SmugMugException(response); 
    } 
    return true; 
} 

我在哪裏出了錯?


嘗試只是爲了看看回應:

SMResponse response = builder.entity(image).post(SMResponse.class); 

實際上,它發回一個空白響應(無JSON),這本身就是奇怪,因爲我本來期望一些消息發回。下面是輸出:

Nov 21, 2012 11:55:48 PM com.sun.jersey.api.client.filter.LoggingFilter log 
INFO: 1 * Client in-bound response 
1 < 200 
1 < Edge-Control: no-store 
1 < X-SmugMug-Hiring: How to love what you do: http://www.smugmug.com/jobs/ 
1 < Date: Thu, 22 Nov 2012 05:55:48 GMT 
1 < Content-Length: 0 
1 < X-SmugMug-Values: 4/4 - It's the product, stupid 
1 < Expires: Thu, 22 Nov 2012 05:55:49 GMT 
1 < Connection: keep-alive 
1 < Content-Type: application/json; charset=utf-8 
1 < X-Powered-By: SmugMug/0.9 
1 < Server: Apache 
1 < Cache-Control: private, no-store, no-cache, max-age=1, must-revalidate 
1 < 
+0

你可以試試SMResponse response = builder.entity(image).post(SMResponse.class); – Reddy

+0

@Reddy問題在於,我在哪裏設置圖像本身的數據。 –

+0

它本質上是一個HTTP請求,因此嘗試使用調試工具(如Fiddler/BurpSuite)捕獲流量,我發現這種方法在嘗試查找問題時特別有用。 –

回答

1

我不完全知道發生了什麼,但我能得到的SmugMug的帳戶設置找到上傳日誌(要到那裏去工具後它的工作 - >帳戶設置 - >統計信息 - >上傳 - >詳細信息)。請注意,在上傳日誌中有一個切換,只顯示錯誤或所有上傳。

現在就如何設置Post Request的實際「主體」的實際答案。實際的格式應該是第一個我貼:

SMResponse response = builder.post(SMResponse.class, Files.readAllBytes(image.toPath())); 

所以無論是我搞砸了,並認爲這是不工作的時候它是有當時的smugmug的最終出了問題,或者有什麼事否則在我的代碼中是錯誤的,在我嘗試修復這個非問題的過程中已經修復。