gpt4 book ai didi

java - 上传到imgur,已经一周失败了

转载 作者:行者123 更新时间:2023-12-01 04:20:57 25 4
gpt4 key购买 nike

我一直在努力解决这个问题,并且可能阅读了一百篇关于它的帖子,但是,我仍然没有得到正确的解决方案,并且担心我正在做一些非常愚蠢的事情。(使用apache commons编解码器库)

    import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;

public class UploadTest
{
public static void main (String[] args)
{
try
{
File pic = new File ("C:\\pic.jpg");
upload ( (BufferedImage) ImageIO.read (pic) );
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage() );
e.printStackTrace();
}
}

public static void upload(BufferedImage image)
{
String IMGUR_POST_URI = "http://api.imgur.com/2/upload.xml";
String IMGUR_API_KEY = "my_api_key";

try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.out.println("Writing image...");
ImageIO.write(image, "png", baos);
URL url = new URL(IMGUR_POST_URI);

System.out.println("Encoding...");
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBase64String(baos.toByteArray()).toString(), "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(IMGUR_API_KEY, "UTF-8");

System.out.println("Connecting...");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

System.out.println("Sending data...");
wr.write(data);
wr.flush();

System.out.println("Finished.");

//just display the raw response
BufferedReader in = new BufferedReader (new InputStreamReader (conn.getInputStream()) );
String line;
while ( (line = in.readLine()) != null )
{
System.out.println(line);
}
in.close();

}
catch(Exception e)
{
System.out.println("Error: " + e.getMessage() );
e.printStackTrace();
}
}
}

好的,这是我的输出:

    Writing image...
Encoding...
Connecting...
Sending data...
Finished.
Error: Server returned HTTP response code: 400 for URL: http://imgur.com/api/upload.xml
java.io.IOException: Server returned HTTP response code: 400 for URL: http://imgur.com/api/upload.xml
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1615)
at UploadTest.upload(UploadTest.java:52)
at UploadTest.main(UploadTest.java:15)

我不确定问题是什么,我怀疑它甚至没有上传,即使没有抛出错误

最佳答案

尝试-->

public static void upload(BufferedImage image) {
String IMGUR_POST_URI = "https://api.imgur.com/3/upload";
String IMGUR_API_KEY = "Your Client ID";

try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.out.println("Writing image...");
ImageIO.write(image, "png", baos);
URL url = new URL(IMGUR_POST_URI);

System.out.println("Encoding...");
String data = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(Base64.encodeBase64String(baos.toByteArray()).toString(), "UTF-8");
data += "&" + URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(IMGUR_API_KEY, "UTF-8");

System.out.println("Connecting...");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Authorization", "Client-ID " + IMGUR_API_KEY);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

System.out.println("Sending data...");
wr.write(data);
wr.flush();

System.out.println("Finished.");

//just display the raw response
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();

} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}

关于java - 上传到imgur,已经一周失败了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18923528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com