简单的Java Imgur上传[英] Simple Java Imgur Upload

本文是小编为大家收集整理的关于简单的Java Imgur上传的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试使用Java Web应用程序将图像上传到IMGUR.我从 imgur api api上传.我的方法似乎"成功地"运行,但我认为没有任何事情发生.除了建立成功之外,我没有其他反馈,也不确定如何获得反馈(错误).

我愿意花费大量时间来使它工作,但是想知道有人可以通过暗示为什么我目前的实施似乎不起作用来开始我?

我做了一个普通的Java课,并在以下内容中粘贴:

package main;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.imageio.ImageIO;

/**
 *
 * @author J
 */
public class TestImgur1 {

     public static String getImgurContent(String clientID) throws Exception {

         clientID = "(edited out)";

    URL url;

    url = new URL("https://api.imgur.com/3/image");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    String data = URLEncoder.encode("image", "UTF-8") + "="
            + URLEncoder.encode("http://i.imgur.com/FB9OZWQ.jpg", "UTF-8");

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "Client-ID " + clientID);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");

    conn.connect();
    StringBuilder stb = new StringBuilder();
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        stb.append(line).append("\n");
    }
    wr.close();
    rd.close();

    System.out.println(stb.toString());

    return stb.toString();
}

}

我做了第二个平原Java类,以称呼第一款,以便我可以运行它:

package main;

import java.io.IOException;


public class ImgurMainTest1 {

      public static void main(String[] args) throws IOException
  {
      try{
    TestImgur1 TestImgur1 = new TestImgur1();


  }catch (Exception e) {}
  }
}

如果您只能提出为什么什么都没发生(除了建立成功之外),这确实可以帮助我入门.我已经四处寻找有关该主题的其他问题/答案,但是似乎每个人都在尝试以不同的方式实施它,并且对我来说真的很难解释.谢谢您阅读

推荐答案

只需致电testimgur1.getimgurcontent()在main方法

本文地址:https://www.itbaoku.cn/post/1937799.html

问题描述

I am trying to upload an image to Imgur using their API with a Java web application. I have copy-pasted this guy's code from Imgur API uploading . My method seems to run "successfully" but I don't think anything is happening. I get no feedback other than BUILD SUCCESS and am not sure how to get feedback (errors).

I am willing to spend a lot of time to get this to work but am wondering if someone can start me out by suggesting why my current implementation doesn't seem to work?

I made a plain Java class and pasted in the following:

package main;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.imageio.ImageIO;

/**
 *
 * @author J
 */
public class TestImgur1 {

     public static String getImgurContent(String clientID) throws Exception {

         clientID = "(edited out)";

    URL url;

    url = new URL("https://api.imgur.com/3/image");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    String data = URLEncoder.encode("image", "UTF-8") + "="
            + URLEncoder.encode("http://i.imgur.com/FB9OZWQ.jpg", "UTF-8");

    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Authorization", "Client-ID " + clientID);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");

    conn.connect();
    StringBuilder stb = new StringBuilder();
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    wr.write(data);
    wr.flush();

    // Get the response
    BufferedReader rd = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        stb.append(line).append("\n");
    }
    wr.close();
    rd.close();

    System.out.println(stb.toString());

    return stb.toString();
}

}

I made a 2nd plain Java class to call the 1st one so I can run it:

package main;

import java.io.IOException;


public class ImgurMainTest1 {

      public static void main(String[] args) throws IOException
  {
      try{
    TestImgur1 TestImgur1 = new TestImgur1();


  }catch (Exception e) {}
  }
}

If you could just suggest why nothing is happening (apart from BUILD SUCCESS) that would really help me get started. I have looked around for other questions/answers about the subject but it seems like everyone is trying to implement it in a different way and is really hard for me to interpret. Thankyou for reading

推荐答案

just call TestImgur1.getImgurContent() in main method