簡體   English   中英

Play Framework:使用WS將圖像發布到imageshack

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

我想POST圖像使用到ImageShack的他們的API和播放框架的WSRequest對象。

我的代碼如下:

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();
}

但是,我總是從imageshack中得到以下回應:

我嘗試使用字節數組將文件作為參數發送:

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

但這也導致了Imageshack的相同反應。

這讓我很生氣。 任何人都可以指出我出錯的地方或者可能指向更好的解決方案嗎? 謝謝。


原因

經過一番研究后,我發現我忽略了這個問題中的一些重要信息....我在我的應用程序中包含了Google App Engine模塊。

根據Play Framework Google Group ,使用GAE時將文件附加到WS請求的代碼實際上只是注釋掉了。 因此它不起作用的原因。 所以沒有錯誤為你拋出,也沒有跡象表明它為什么不起作用......你只需要解決它。

我接受了@Gary的回答,因為這是使用WS將圖像上傳到ima​​geshack的正確方法 - 只是在使用GAE時沒有。

我認為您不需要直接指定內容類型或mime類型。

我使用以下代碼成功上傳。

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

我認為當您將文件附加到請求時,它會自動決定它將是多部分/表單數據。

這是我的整個控制器(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();
    }
}

這是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

我沒有做任何其他改動。 剛瀏覽到http:// localhost:9000 / Application.tryUpload ,可以在播放控制台上看到成功的XML。

您正在錯誤地設置內容類型標頭。

而不是這個:

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

嘗試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM