I have this strange bug that I have been debugging for like two days. My server constructs a string representing a HTML file and sent it over to another API to get transferred into a PDF file. However, all the foreign characters like Chinese will be converted to question marks.
I view these variables in debug mode, they all look fine, but when it is send it will be changed to question marks. I tried
new String(originalString.getBytes(), StandardCharsets.UTF_8)
The value returned by this constructor makes all the chinese character question mark as well.
I can view the originalString normally in debug mode with breakpoints, but I can't log them or System.out.println()
them. The printed string will turn all foreign character into question marks.
I tried to look into the String, the correct String have slot 364 as a Chinese character with code 20000 something, but when they are converted, they get changed to ?
with code 63
I do have console Charset set to UTF-8
This is how I am sending over to the other API FYI This is old legacy code written by some random guy. Apologize for horrible style
String htmlData = princeServices.createPdf(pdfData, teacher, connection);
htmlData = tidyHTML(htmlData, pdfData); // This is the html data, can be viewed in debug mode
URL urlobj = null;
if ("Landscape".equalsIgnoreCase(pdfData.getPrintLayout())) {
urlobj = new URL(princePath + "Landscape");
} else {
urlobj = new URL(princePath);
}
HttpURLConnection conn = (HttpURLConnection) urlobj.openConnection();
HttpURLConnection.setFollowRedirects(true);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Authorization", "Basic " + printBase64Binary(/* password here */ );
conn.setRequestProperty("Content Length", Integer.toString(htmlData.length()));
outToPrinceServer = new OutputStreamWriter(conn.getOutputStream());
outToPrinceServer.write(htmlData);
outToPrinceServer.flush();
resp.setContentType("application/pdf");
resp.addHeader("Content-Disposition", "attachment; filename=" + "planbook.pdf");
ServletOutputStream outToBrowser = resp.getOutputStream();
ByteStreams.copy(conn.getInputStream(), outToBrowser);
Save my life pls :)