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

Java 如何在Http POST请求中发送图像文件?(爪哇),java,http,request,Java,Http,Request,因此,我正在编写一个小应用程序,使用用户提供的API将图像目录转储到用户的tumblr博客中: 我已经得到了明文发布的工作,但现在我需要找到如何发送一个图像文件,而不是UTF-8编码的文字后,我迷路了。目前我的代码返回403禁止的错误,就好像用户名和密码不正确一样(它们不是),而我尝试的其他一切都会给我一个错误的请求错误。如果可以的话,我不想使用外部库来实现这一点。这是我的ImagePost课程: public class ImagePost { String data = null; Str

因此,我正在编写一个小应用程序,使用用户提供的API将图像目录转储到用户的tumblr博客中:

我已经得到了明文发布的工作,但现在我需要找到如何发送一个图像文件,而不是UTF-8编码的文字后,我迷路了。目前我的代码返回403禁止的错误,就好像用户名和密码不正确一样(它们不是),而我尝试的其他一切都会给我一个错误的请求错误。如果可以的话,我不想使用外部库来实现这一点。这是我的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();
}
}
我建议您使用Apache
httpclient
包的继承者。使用
MultipartRequestEntity
可以发送包含文件的多部分POST请求。一个例子如下:

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