用Java向API发送文件



我正在使用库QRCodeWriter创建QR并将其发送到API。

问题是,我有变量os图像存储的地方,但我不能将其添加到multiparentity发送它(注释行)。

错误是:不兼容的类型:ByteArrayOutputStream不能转换为ContentBody entity.addPart("file", os)

我尝试了很多代码来转换图像,但没有成功。

任何帮助将不胜感激

String url =  "https://apiURL.com";
String accessToken = "123456789";
QRCodeWriter writer = new QRCodeWriter();
BitMatrix matrix = writer.encode("https://www.google.com/", BarcodeFormat.QR_CODE, 350, 350);
BufferedImage image = MatrixToImageWriter.toBufferedImage(matrix);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.setUseCache(false);
ImageIO.write(image,"png",os);

context.log(os.toString());
basicIO.write("TestData");
MultipartEntity entity = new MultipartEntity();
//entity.addPart("file", os);
HttpResponse returnResponse = Request.Post(url).addHeader("Authorization", accessToken).body(entity).execute().returnResponse();
context.log("Response status: " + returnResponse.getStatusLine().getStatusCode());
context.log(EntityUtils.toString(returnResponse.getEntity()));

它可以改变:

entity.addPart("file", os);

:

entity.addPart("file", new ByteArrayBody(os.toByteArray(), "qr.png"));

最新更新