2012-03-24 11 views
-3

HttpURLConnection経由でプロフィールに写真をアップロードしようとしています。 私は自分のコードからJavaでHTTPリクエストを使用したFacebook写真のアップロード

サンプルをpublish_stream、ACCESはUSER_STATUS、user_photos、offline_accessのトークンがあります。

url = new URL("https://graph.facebook.com/me/photos"); 

String content = 
    "access_token=" + URLEncoder.encode ("my_token") + 
    "&message=" + URLEncoder.encode ("SUNT !!!")+ 
    "&url=" + URLEncoder.encode("file:///D:\\personale\\Images\\P0030_07-02-11_00.JPG"); 

私は

{"error":{"message":"file:\/\/\/D:\\personale\\Images\\P0030_07-02-11_00.JPG is an internal url, but this is an external request.","type":"CurlUrlInvalidException"}} 

は私がアップロードでき、次のエラーを得たの要求を行うとファイルURLを使用して私のPCからのファイル? ファイルからバイト配列を使用してファイルをアップロードするにはどうすればよいですか?

多くの感謝!

+0

[restfb](http://restfb.com/)または[spring-social-facebook](http://static.springsource.org/spring-social-facebook/docs/1.0.x/reference/)を確認してください。 html /)。それらは、グラフAPIのJavaラッパーであるため、操作が簡単です。 – Bozho

+0

'url'パラメータは公的にアクセス可能な外部提供画像のアップロード用に設計されています。ローカルイメージをアップロードするには、 '' source''(http://developers.facebook.com/docs/reference/api/user/#photos)( 'url'ではなく)を使うべきです。' multipart/form-データ符号化された画像データである。 –

答えて

1

The solution, as posted by dnp


多くの感謝!

これが解決策です:

public class Main2 { 

static final String BOUNDARY = "----------V2ymHFg03ehbqgZCaKO6jy"; 

public static void main(String [] args) throws IOException{ 
    URL url; 
    HttpURLConnection urlConn; 
    DataOutputStream printout; 
    DataInputStream input; 

    //------------------------------------------------------------------- 

    File image = new File("D:/personale/Images/P1025[01]_03-07-11.JPG"); 


    FileInputStream imgStream = new FileInputStream(image); 

    byte [] buffer = new byte[(int) image.length()]; 

    imgStream.read(buffer); 

    //------------------------------------------------------------------- 


    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.18.1.1", 4444)); 

    //url = new URL ("https://graph.facebook.com/me/feed"); 
    url = new URL("https://graph.facebook.com/me/photos?access_token=AAACkMOZA41QEBACsafBxqVfXX54JqGLQSaE6YQ062NuTe3XUZBTdTEvy3R2H9Yr4PZA9r38JvLni7r1hYLuZCnBZAAPPH3krMMSKtIraiswZCiIZBu0nyYT"); 
    System.out.println("Before Open Connection"); 

    urlConn = (HttpURLConnection) url.openConnection(proxy); 

    urlConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString()); 

    urlConn.setDoOutput (true); 
    urlConn.setUseCaches (false); 
    urlConn.setRequestMethod("POST"); 

    // String content = "access_token=" + URLEncoder.encode ("AAACkMOZA41QEBAHQHUyYcMsLAewOYIe1j5dlOVOlMZBm6h9rvCQEFhmcBHg7ETHrdlrgv4sau573xMVuxIt8DzRxKFuqRqqBskDvOZA9iIkZCdPyI4Bu"); 

    String boundary = getBoundaryString(); 

    String boundaryMessage = getBoundaryMessage(boundary, "upload_field", "P1025[01]_03-07-11.JPG", "image/png"); 

    String endBoundary = "\r\n--" + boundary + "--\r\n"; 

    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

    bos.write(boundaryMessage.getBytes()); 

    bos.write(buffer); 

    bos.write(endBoundary.getBytes()); 

    printout = new DataOutputStream (urlConn.getOutputStream()); 

    //printout.writeBytes(content); 

    printout.write(bos.toByteArray()); 

    printout.flush(); 
    printout.close(); 
    // Get response data. 



    //input = new DataInputStream (urlConn.getInputStream()); 

    if (urlConn.getResponseCode() == 400 || urlConn.getResponseCode() == 500) { 
     input = new DataInputStream (urlConn.getErrorStream()); 
    } else { 
     input = new DataInputStream (urlConn.getInputStream()); 
    } 

    String str; 
    while (null != ((str = input.readLine()))) 
    { 
     System.out.println (str); 
    } 
    input.close(); 

} 

public static String getBoundaryString() 
{ 
    return BOUNDARY; 
} 

public static String getBoundaryMessage(String boundary, String fileField, String fileName, String fileType) 
{ 
    StringBuffer res = new StringBuffer("--").append(boundary).append("\r\n"); 
    res.append("Content-Disposition: form-data; name=\"").append(fileField).append("\"; filename=\"").append(fileName).append("\"\r\n") 
     .append("Content-Type: ").append(fileType).append("\r\n\r\n"); 

    return res.toString(); 
} 
} 
+0

解決策を説明しないためのdownvote。ソースコードは良いですが、リークの回答ではありません –

+0

私はコードが借用されていると思います:http://www.thoughtwavesoft.com/cms/index.php?option=com_easyblog&view=entry&id=5&Itemid=102ソースは答え。 – rahulserver

+0

私はこの解決策を使用していますが、「Connection timed out:connect」と言っています。助けてください –

0

あなたは、最新リリースバージョン3.0がこの機能を持っている

http://code.google.com/p/socialauth/

デフォルトアルバムにFB上の画像をアップロードするには、次のAPIを使用することができます。

関連する問題