Posting an image to the Tumblr API

2020-08-05 10:22发布

So I'm writing a small java 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. My code at the moment is returning a 403 forbidden error 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:

import java.io.*;
import java.net.*;

public class ImagePost {

    String data = null;
    String enc = "UTF-8";
    String type;
    File img;
    byte[] bytes;
    FileReader reader;
    ByteArrayOutputStream bytesOut;

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

        //Construct data
        type = "photo";
        img = new File(imgPath);
        bytes = fileToByteArray(img);
        bytesOut = new ByteArrayOutputStream();

        data = URLEncoder.encode("email", enc) + "=" + URLEncoder.encode(Main.getEmail(), enc);
        data += "&" + URLEncoder.encode("password", enc) + "=" + URLEncoder.encode(Main.getPassword(), enc);
        data += "&" + URLEncoder.encode("type", enc) + "=" + URLEncoder.encode(type, enc);
        data += "&" + URLEncoder.encode("data", enc) + "=" + URLEncoder.encode(bytes.toString(), enc);
        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 byte[] fileToByteArray(File img) throws IOException {
        long length = img.length();
        InputStream in = new FileInputStream(img);
        byte[] byteArray;

        if (length > Integer.MAX_VALUE) {
            System.out.println("File too large!");
            return null;
        }

        byteArray = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        while (offset < byteArray.length && (numRead = in.read(byteArray, offset, byteArray.length - offset)) >= 0) {
            offset += numRead;
        }
        in.close();
        return byteArray;
    }

    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-Encoding", "application/x-www-form-urlencoded");
        http.setRequestProperty("Content-Type", "image/png");
        OutputStreamWriter out = new OutputStreamWriter(http.getOutputStream());

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

2条回答
何必那么认真
2楼-- · 2020-08-05 10:46

You need to set the post to have a content type of:

multipart/form-data
查看更多
别忘想泡老子
3楼-- · 2020-08-05 10:55

I suggest you use an external library like Jumblr which will allow you to use tumblr API in easy way using java. Example of posting image

PhotoPost post = client.newPost("blog name", PhotoPost.class);
post.setCaption("caption");
post.setSource("image url");
post.save();
查看更多
登录 后发表回答