简体   繁体   English

Play Framework:使用WS将图像发布到imageshack

[英]Play Framework: Post image to imageshack using WS

I am trying to POST an image to imageshack using their API and Play Framework's WSRequest object. 我想POST图像使用到ImageShack的他们的API和播放框架的WSRequest对象。

My code is as follows: 我的代码如下:

public static void upload( Picture picture ) throws Exception {

    //set file parameter - in this case the image
    WS.FileParam fp = new WS.FileParam( picture.asFile, "fileupload");

    //set other parameters
    Map<String,Object> params = new HashMap<String, Object>();
    params.put( "optsize", "resample" );
    params.put( "rembar", "yes" );
    params.put( "public", "no" );
    params.put( "a_username", username );
    params.put( "a_password", password );
    params.put( "key", a_key );

    //POST request
    Document doc = WS.url( "http://www.imageshack.us/upload_api.php" )
        .setHeader( "Content-Type", picture.contentType )
        .mimeType( "multipart/form-data" )
        .params( params )
        .files( fp )
        .post()
        .getXml();
}

However, I always reveive the following response from imageshack: 但是,我总是从imageshack中得到以下回应:

Sorry, but we've detected that unexpected data is received. 抱歉,我们检测到收到了意外数据。 Required parameter 'fileupload' is missing or your post is not multipart/form-data. 缺少必需参数'fileupload'或您的帖子不是multipart / form-data。

I have tried sending the file as a parameter using a byte array: 我尝试使用字节数组将文件作为参数发送:

params.put( "fileupload", Base64.encode( picture.asBytes )  )

But this also results in the same response from Imageshack. 但这也导致了Imageshack的相同反应。

This is driving me mad. 这让我很生气。 Can anyone point out where I am going wrong or possibly point me in the direction of a better solution? 任何人都可以指出我出错的地方或者可能指向更好的解决方案吗? Thanks. 谢谢。


The cause 原因

After a bit of research I found that I had neglected a bit of important information from this question....I am including the Google App Engine module within my app. 经过一番研究后,我发现我忽略了这个问题中的一些重要信息....我在我的应用程序中包含了Google App Engine模块。

According to the Play Framework Google Group the code associated with attaching Files to a WS request when using GAE is actually just commented out. 根据Play Framework Google Group ,使用GAE时将文件附加到WS请求的代码实际上只是注释掉了。 Hence the reason it just doesn't work. 因此它不起作用的原因。 So no error thrown for you and no indication why it doesn't work...you just have to work it out. 所以没有错误为你抛出,也没有迹象表明它为什么不起作用......你只需要解决它。

I have accepted @Gary's answer as it is the correct way to upload an image to imageshack using WS - just not when using GAE. 我接受了@Gary的回答,因为这是使用WS将图像上传到ima​​geshack的正确方法 - 只是在使用GAE时没有。

I don't think you need to specify the content type or mime type directly. 我认为您不需要直接指定内容类型或mime类型。

I used the following code to upload successfully. 我使用以下代码成功上传。

WS.FileParam fp = new WS.FileParam(
      new File("d:\\workspace\\ImageShackTest\\sample_picture.png"), "fileupload");

    Map<String,Object> params = new HashMap<String, Object>();
    params.put( "optsize", "resample" );
    params.put( "rembar", "yes" );
    params.put( "public", "yes" );
    //params.put( "a_username", username );
    //params.put( "a_password", password );
    params.put( "key", API_KEY );

    //POST request
    Document doc = WS.url( "http://www.imageshack.us/upload_api.php" )
        .params( params )
        .files( fp )
        .post()
        .getXml();

I think when you attach a file to a request it automatically decides its going to be multipart/form-data. 我认为当您将文件附加到请求时,它会自动决定它将是多部分/表单数据。

This is my entire controller (except for the API Key) 这是我的整个控制器(API密钥除外)

package controllers;

import play.*;
import play.mvc.*;
import java.util.*;
import models.*;
import play.libs.*;
import java.io.File;

public class Application extends Controller {

    public static void index() { render(); }

    private static final String API_KEY = "API KEY REMOVED TO PROTECT THE INNOCENT";

    public static void tryUpload() {
        WS.FileParam fp = new WS.FileParam( new File("d:\\workspace\\ImageShackTest\\sample_picture.png"), "fileupload");

        Map<String,Object> params = new HashMap<String, Object>();
        params.put( "optsize", "resample" );
        params.put( "rembar", "yes" );
        params.put( "public", "yes" );
        params.put( "key", API_KEY );

        String doc = WS.url( "http://www.imageshack.us/upload_api.php" )
            .params( params )
            .files( fp )
            .post()
            .getString();

        System.out.println(doc);

        index();
    }
}

and this is the application.conf file 这是application.conf文件

# This is the main configuration file for the application.
# ~~~~~
application.name=ImageShackTest
application.mode=dev
%prod.application.mode=prod
application.secret=JIVQE8y3y1lCzXRGprFJvoXBdi8Jpa8qE1U1mBIooLLOOYk5yyhAI5cxbEf4q4pl
date.format=yyyy-MM-dd
attachments.path=data/attachments
mail.smtp=mock

I didn't make any other changes. 我没有做任何其他改动。 Just browsed to http://localhost:9000/Application.tryUpload and could see the success XML on the play console. 刚浏览到http:// localhost:9000 / Application.tryUpload ,可以在播放控制台上看到成功的XML。

You are setting the content type header incorrectly. 您正在错误地设置内容类型标头。

Instead of this: 而不是这个:

.setHeader( "Content-Type", picture.contentType )

Try this: 尝试这个:

.setHeader( "Content-Type", "multipart/form-data" )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM