我一直在尝试使用java
向NodeJS
API发送base64图像,在工作和搜索了数小时后,我不知道是什么原因导致了以下问题,如下所示:
在nodejs中记录base64图像后,我看到所有
+
字符都被space
取代了
这是Java
中原始base64的一部分>
f8A0NH2qH+/+hooouAfaof7/wCho+1Q/
这里是NodeJS
中接收到的图像的一部分>
f8A0NH2qH / hooouAfaof7/wCho 1Q/
我尝试通过POSTMAN
发送图像,一点都没有问题。
所有步骤如下:
1-我正在使用以下代码段将图像转换为base64
public static String imgToBase64String(final RenderedImage img, final String formatName) { final ByteArrayOutputStream os = new ByteArrayOutputStream(); try { ImageIO.write(img, formatName, Base64.getEncoder().wrap(os)); return os.toString(StandardCharsets.ISO_8859_1.name()); } catch (final IOException ioe) { throw new UncheckedIOException(ioe); } } public static BufferedImage base64StringToImg(final String base64String) { try { return ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(base64String))); } catch (final IOException ioe) { throw new UncheckedIOException(ioe); } }
并获取屏幕截图
final Robot robot = new Robot(); final Rectangle r = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); final BufferedImage bi = robot.createScreenCapture(r); final String base64String = Base64Converter.imgToBase64String(bi, "jpg");
2-我正在使用
Gson
库对对象进行字符串化3-我正在使用
bodyParser
中的NodeJS
4-发送HTTP
请求为:
public static void sendPOST(String image) throws Exception { String POST_PARAMS = "screenShotData"; URL obj = new URL(POST_URL); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); con.setConnectTimeout(5000); // 5 seconds con.setReadTimeout(5000); // 5 seconds Gson gson = new Gson(); Http.ScreenShot screenShot = new ScreenShot(); // This is just a class with a string property screenShot.setImage(image); POST_PARAMS += gson.toJsonTree(screenShot).getAsJsonObject(); con.setDoOutput(true); OutputStream os = con.getOutputStream(); byte[] outputBytesArray = POST_PARAMS.getBytes(); os.write(outputBytesArray); os.flush(); os.close(); int responseCode = con.getResponseCode(); System.out.println("POST Response Code :: " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { //success BufferedReader in = new BufferedReader(new InputStreamReader( con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); Object responseObject = gson.fromJson(response.toString(), Object.class); System.out.println("Res: " + responseObject); } else { System.out.println(con.getResponseMessage()); } }
我一直在尝试使用Java将Base64图像发送到NodeJS API,在工作和搜索了数小时后,我不知道是什么原因导致了以下问题,如下所示:...
在URL编码的文本中,+
字符表示空格字符。例如,
https://example.com/?s=nodejs+bodyparser
您忘记了关闭包装好的Base64编码器流。只有关闭它,base64编码数据的末尾才能被写入:
public static String imgToBase64String(final RenderedImage img, final String formatName) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
try (OutputStream wrapped = Base64.getEncoder().wrap(os)) {
ImageIO.write(img, formatName, wrapped);
}
return os.toString(StandardCharsets.ISO_8859_1.name());
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}