【问题标题】:Posting an image to the Tumblr API将图像发布到 Tumblr API
【发布时间】:2011-06-15 22:10:58
【问题描述】:

所以我正在编写一个小型 java 应用程序,使用他们提供的 API:http://www.tumblr.com/docs/en/api

将图像目录转储到用户的 tumblr 博客中

我已经开始使用纯文本发布,但现在我需要了解如何在 POST 中发送图像文件。我目前的代码正在返回 403 禁止错误,而我尝试的所有其他操作都给了我一个错误的请求错误。如果可以的话,我宁愿不必为此使用外部库。这是我的 ImagePost 课程:

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();
    }
}

【问题讨论】:

  • 嗨@Nige,我在 ReactJS 上回答了你的另一个问题,但在我提交答案之前你删除了这个问题。我已经在这里发布了我的答案,以防它仍然有帮助:gist.github.com/jlbooker/3bddb25c9482fc1b70b3760e60f0463d
  • 嗨@Jeremy - 对此我感到非常抱歉,我没想到有人会这么快开始回复!我在发布后不久意识到我的整个结构不正确 - 你完全正确,我没有完全遵循 React 背后的设计理念。我现在重写了它,但你的回答仍然非常有帮助 - 我会去的回顾我的代码并根据您的建议检查它,以确保我已经掌握了它。非常感谢您的帮助!

标签: java image http post tumblr


【解决方案1】:

您需要将帖子的内容类型设置为:

multipart/form-data

【讨论】:

  • 好的,所以我改了:http.setRequestProperty("Content-Type", "image/png"); to: http.setRequestProperty("Content-Type", "multipart/form-data");但我仍然收到 403 禁止错误。
  • 看看this看看是否有帮助。
【解决方案2】:

我建议你使用像 Jumblr 这样的外部库,它可以让你使用 java 轻松使用 tumblr API。 发布图片示例

PhotoPost post = client.newPost("blog name", PhotoPost.class);
post.setCaption("caption");
post.setSource("image url");
post.save();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2013-06-02
    • 1970-01-01
    • 2013-01-15
    • 2012-11-30
    • 2013-12-08
    • 1970-01-01
    相关资源
    最近更新 更多