我正在尝试通过HttpURLConnection将照片上传到我的个人资料中。 我有user_status,user_photos,offline_access,publish_stream
的访问令牌我的代码中的示例:
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"}}
我可以使用文件网址从我的电脑上传文件吗? 如何使用文件中的字节数组上传文件?
非常感谢!
答案 0 :(得分: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();
}
}
答案 1 :(得分:0)