Imgur API 上传 [英] Imgur API uploading
问题描述
所以有这行代码
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBase64String(baos.toByteArray()).toString(), "UTF-8");数据 += "&"+ URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(你的 API 密钥在这里,"UTF-8");
当我注册 Imgur API 时,我得到了一个 client_id 和一个 client_secret,我想知道我使用哪个,它在第二行的第一部分也写着你的 API 密钥在这里"键"我在那里输入什么?也是上传它的网站 http://imgur.com/api/upload 因为我看到了几个不同的.
试试这个:
public static String getImgurContent(String clientID) throws Exception {网址网址;url = new URL("https://api.imgur.com/3/image");HttpURLConnection conn = (HttpURLConnection) url.openConnection();字符串数据 = URLEncoder.encode("image", "UTF-8") + "="+ URLEncoder.encode(IMAGE_URL, "UTF-8");conn.setDoOutput(true);conn.setDoInput(true);conn.setRequestMethod("POST");conn.setRequestProperty("Authorization", "Client-ID" + clientID);conn.setRequestMethod("POST");conn.setRequestProperty("内容类型","application/x-www-form-urlencoded");conn.connect();StringBuilder stb = new StringBuilder();OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());写(数据);wr.flush();//获取响应BufferedReader rd = 新的 BufferedReader(新的 InputStreamReader(conn.getInputStream()));字符串线;while ((line = rd.readLine()) != null) {stb.append(line).append("\n");}wr.close();rd.close();返回 stb.toString();}
几乎就像一个矮胖子,把每一部分都重新组合在一起,来自各地的代码,至少它按预期工作,很遗憾他们没有例子......
享受.
ps:你也可以用 FILES 制作(还没试过),但你需要先把图片转为 base64,然后转为 utf8(替换 url)
编辑,使用它代替 URL,以便您可以上传文件:
//创建base64图像BufferedImage 图像 = null;File file = new File(imageDir);//读取图像图像 = ImageIO.read(file);ByteArrayOutputStream byteArray = new ByteArrayOutputStream();ImageIO.write(image, "png", byteArray);byte[] byteImage = byteArray.toByteArray();String dataImage = Base64.encode(byteImage);字符串数据 = URLEncoder.encode("image", "UTF-8") + "="+ URLEncoder.encode(dataImage, "UTF-8");
So there is this line of code
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBase64String(baos.toByteArray()).toString(), "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(YOUR API KEY GOES HERE, "UTF-8");
and when I registered for the Imgur API I was given a client_id and a client_secret and was wondering which one I use for where it says "YOUR API KEY GOES HERE" also in the first part in the second line where it says "key" what do I enter there? Also is the site to upload it http://imgur.com/api/upload because I have seen a few different ones.
try this out:
public static String getImgurContent(String clientID) throws Exception {
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(IMAGE_URL, "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();
return stb.toString();
}
was almost like humpty dumpty, getting every piece back together, codes from everywhere, at least it worked as expected, its a shame they don't have examples...
enjoy.
ps: ou can also make with FILES (haven't tried yet) but you need to convert an image to base64 and then to utf8 (to replace the url)
edit, use this instead of the URL, so you can upload files:
//create base64 image BufferedImage image = null; File file = new File(imageDir); //read image image = ImageIO.read(file); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); ImageIO.write(image, "png", byteArray); byte[] byteImage = byteArray.toByteArray(); String dataImage = Base64.encode(byteImage); String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(dataImage, "UTF-8");
这篇关于Imgur API 上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!