2012-03-24 55 views
-3

我试图通过HttpURLConnection将照片上传到我的个人资料。 我有存取权限令牌用于user_status,user_photos,offline_access,从我的代码publish_stream在Java中使用HTTP请求上传Facebook照片

样品:

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的文件? 如何使用文件中的字节数组上传文件?

非常感谢!

+0

查看[restfb](http://restfb.com/)或[spring-social-facebook](http://static.springsource.org/spring-social-facebook/docs/1.0.x/reference/ HTML /)。它们是Graph API的Java包装,使事情变得更简单。 – Bozho 2012-03-25 07:33:27

+0

'url'参数设计用于上传可公开访问的外部服务图像。要上传本地图片,您应该使用['source'](http://developers.facebook.com/docs/reference/api/user/#photos)(而不是'url'),它应该包含'multipart/form-数据编码的图像数据。 – 2012-03-25 07:29:06

回答

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不解释解决方案。源代码是好的,但不是一个reak答案 – 2013-01-10 17:31:14

+0

我认为代码是借来的:http://www.thoughtwavesoft.com/cms/index.php?option=com_easyblog&view=entry&id=5&Itemid=102源必须提及答案。 – rahulserver 2013-06-14 07:53:43

+0

我正在使用此解决方案,但它说:'连接超时:连接'。请帮忙 – 2014-03-08 09:56:46