开发者

How do I send an image file in a Http POST request? (java)

So I'm writing a small app to dump a directory of images into the user's tumblr blog, using their provided API: http://www.tumblr.com/docs/en/api

I've gotten plaintext posting to work, but now I need to find out how to send an image file in the POST instead of UTF-8 encoded text, and I'm lost. My code at the moment is returning a 403 forbidden error, as if the username and password were incorrect (they're not), and everything else I try gives me a bad request error. I'd rather not have to use external libraries for this if I can. This is my ImagePost class:

public class ImagePost {

String data = null;
String enc = "UTF-8";
String type;
File img;

public ImagePost(String imgPath, String caption, String tags) throws IOException {

    //Construct data
    type = "photo";
    img = new File(imgPath);

    data = URLEncoder.encode("email", enc) + "=" + URLEncoder.encode(Main.getEmail(), enc);
    data += "&" + URLEncoder.encode("pass开发者_StackOverflowword", enc) + "=" + URLEncoder.encode(Main.getPassword(), enc);
    data += "&" + URLEncoder.encode("type", enc) + "=" + URLEncoder.encode(type, enc);
    data += "&" + URLEncoder.encode("data", enc) + "=" + img;
    data += "&" + URLEncoder.encode("caption", enc) + "=" + URLEncoder.encode(caption, enc);
    data += "&" + URLEncoder.encode("generator", "UTF-8") + "=" + URLEncoder.encode(Main.getVersion(), "UTF-8");
    data += "&" + URLEncoder.encode("tags", "UTF-8") + "=" + URLEncoder.encode(tags, "UTF-8");

}

public void send() throws IOException {
    // Set up connection
    URL tumblrWrite = new URL("http://www.tumblr.com/api/write");
    HttpURLConnection http = (HttpURLConnection) tumblrWrite.openConnection();
    http.setDoOutput(true);
    http.setRequestMethod("POST");
    http.setRequestProperty("Content-Type", "image/png");
    DataOutputStream dout = new DataOutputStream(http.getOutputStream());
    //OutputStreamWriter out = new OutputStreamWriter(http.getOutputStream());

    // Send data
    http.connect();
    dout.writeBytes(data);
    //out.write(data);
    dout.flush();
    System.out.println(http.getResponseCode());
    System.out.println(http.getResponseMessage());
    dout.close();
}
}


I suggest you use MultipartRequestEntity (successor of deprecated MultipartPostMethod) of the Apache httpclient package. With MultipartRequestEntity you can send a multipart POST request including a file. An example is below:

public static void postData(String urlString, String filePath) {

    log.info("postData");
    try {
        File f = new File(filePath);
        PostMethod postMessage = new PostMethod(urlString);
        Part[] parts = {
                new StringPart("param_name", "value"),
                new FilePart(f.getName(), f)
        };
        postMessage.setRequestEntity(new MultipartRequestEntity(parts, postMessage.getParams()));
        HttpClient client = new HttpClient();

        int status = client.executeMethod(postMessage);
    } catch (HttpException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        //  TODO Auto-generated catch block
        e.printStackTrace();
    }         
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜