简体   繁体   English

如何在Http POST请求中发送图像文件? (Java)的

[英]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 因此,我正在编写一个小型应用程序,以使用其提供的API将图像目录转储到用户的tumblr博客中: 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. 我已经开始使用纯文本发布,但是现在我需要找出如何在POST中发送图像文件而不是UTF-8编码文本的方法,我迷路了。 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. 目前,我的代码返回了403禁止的错误,好像用户名和密码不正确(不是),而我尝试的所有其他操作都给我带来了错误的请求错误。 I'd rather not have to use external libraries for this if I can. 如果可以,我宁愿不必为此使用外部库。 This is my ImagePost class: 这是我的ImagePost类:

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("password", 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. 我建议你使用MultipartRequestEntity (不建议使用的继任MultipartPostMethod阿帕奇) httpclient包。 With MultipartRequestEntity you can send a multipart POST request including a file. 使用MultipartRequestEntity您可以发送包含文件的多部分POST请求。 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();
    }         
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM