简单的 Java Imgur 上传 [英] Simple Java Imgur Upload
问题描述
我正在尝试使用他们的 API 和 Java Web 应用程序将图像上传到 Imgur.我已经从 Imgur API 上传 复制粘贴了这个人的代码.我的方法似乎成功"运行,但我认为没有发生任何事情.除了 BUILD SUCCESS 之外,我没有得到任何反馈,并且不确定如何获得反馈(错误).
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?
我制作了一个普通的 Java 类并粘贴了以下内容:
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();
}
}
我制作了第二个普通 Java 类来调用第一个,以便我可以运行它:
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) {}
}
}
如果您能提出为什么没有发生任何事情(除了 BUILD SUCCESS),那真的会帮助我开始.我四处寻找有关该主题的其他问题/答案,但似乎每个人都试图以不同的方式实施它,这对我来说真的很难解释.感谢阅读
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
推荐答案
只需在main方法中调用TestImgur1.getImgurContent()
just call TestImgur1.getImgurContent() in main method
这篇关于简单的 Java Imgur 上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!