2017-05-24 22 views
0

我有這個奇怪的錯誤,我已經調試了兩天。我的服務器構建一個代表HTML文件的字符串,並將其發送到另一個API以轉換爲PDF文件。但是,像中文這樣的所有外來字符都會被轉換爲問號。Java字符串編碼在調試模式下正確,但在日誌中和通過網絡請求錯誤

我在調試模式下查看這些變量,它們都看起來不錯,但是當它發送時它將被改爲問號。我試過

new String(originalString.getBytes(), StandardCharsets.UTF_8) 這個構造函數返回的值也使得所有的中文字符都是問號。

可以查看originalString正常情況下在調試模式下帶有斷點,但我無法登錄它們或System.out.println()它們。打印的字符串將所有外來字符變成問號。

我試圖尋找到的字符串,正確的字符串具有溝364作爲一箇中國特色與代碼20000的東西,但是當它們被轉換,它們會改變?與代碼63

我有控制檯字符集設置爲UTF-8

這是我如何發送到其他API 僅供參考這是由一些隨機人編寫的舊遺留代碼。道歉可怕的風格

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); 

救我的命PLS :)

+2

字符串沒有編碼。編碼是用來確定**字節**將被轉換爲/來自哪一個字節。當您將它發送到其他API時,它很可能會被損壞。你最近怎麼樣? – Kayaman

+0

@Kayaman添加到問題 – Bobby

+0

@Kayaman一個outputStream.getEncoding()給我Cp1252 – Bobby

回答

相關問題