通过Post Request上传文件

时间:2012-11-22 05:25:00

标签: java post jersey http-post

我正在尝试按照documentation通过HTTP Post将图像上传到SmugMug。我的所有标题都正确,但令我困惑的部分是如下所述在正文中设置二进制数据:

  

此方法需要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 < 

1 个答案:

答案 0 :(得分:1)

我不确定发生了什么,但我在SmugMug帐户设置中找到上传日志后能够使其工作(要到达那里,请转到工具 - &gt;帐户设置 - &gt;统计信息 - &gt;上传 - &gt;细节)。请注意,在上传日志中有一个切换按钮,仅显示错误或所有上传。

现在回答如何设置Post Request的实际“正文”的实际答案。实际格式应该是我发布的第一个格式:

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

所以要么我搞砸了,并且认为它不起作用,当时smugmug的结尾有问题,或者我的代码中有其他东西是错的,在我的过程中得到修复试图解决这个非问题。