gpt4 book ai didi

java - 从 BufferedReader 获取 null 返回

转载 作者:行者123 更新时间:2023-12-01 18:46:50 25 4
gpt4 key购买 nike

所以我尝试使用他们的 v3 API ( http://api.imgur.com/ ) 将图像上传到 Imgur,当我收到响应时,它会返回给我

null : null

这是我的完整代码。

import java.util.List;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
//import org.omg.DynamicAny.NameValuePair;
import org.apache.http.NameValuePair;

public class Upload {

public static void main (String[] args) {

System.out.println(Imgur("C:\\Users\\wiesa\\Desktop\\image.jpg", "2313fab45c25337"));
}

public static String Imgur (String imageDir, String clientID) {
//create needed strings
String address = "https://api.imgur.com/3/image";

//Create HTTPClient and post
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(address);

//create base64 image
BufferedImage image = null;
File file = new File(imageDir);

try {
//read image
image = ImageIO.read(file);
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
ImageIO.write(image, "png", byteArray);
byte[] byteImage = byteArray.toByteArray();
String dataImage = new Base64().encodeAsString(byteImage);

//add header
post.addHeader("Authorization", "Client-ID" + clientID);
//add image
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("image", dataImage));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

//execute
HttpResponse response = client.execute(post);

//read response
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String all = null;

//loop through response
while (rd.readLine() != null) {
all = all + " : " + rd.readLine();
}

return all;

}
catch (Exception e){
return "error: " + e.toString();
}
}
}

取决于我将字符串“all”初始化为什么

null

那么返回的是

null : null

但是如果我将其初始化为

""

那么返回的是

: null

我将行更改为

String all = null;

//loop through response
String line = null;
while ((line = rd.readLine()) != null) {
all = all + " : " + line;
}

return all;

我得到了返回

null : {"data":{"error":"Malformed auth header","request":"/3/image","parameters":"image = iVBORw0KGgoAAAANSUhEUgAAB4AAAASwCAIAAACVUsChAACAAElEQVR42uzdCXebyrI2YEuyY8fzPCbZOyfZd597v///...","method":"POST"},"success":false,"status":403}

最佳答案

在每次迭代中,您都会阅读两行而不是一行。改变

while (rd.readLine() != null) { // first line read
all = all + " : " + rd.readLine(); // second line read
}

String line;
while ((line = rd.readLine()) != null) {
all = all + " : " + line;
}

关于java - 从 BufferedReader 获取 null 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17390870/

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