3

私は新米プログラマーのようなもので、扱っているコードにいくつか問題があります。

基本的に、コードは別の JSP からフォームを受け取り、バイトを読み取り、データを解析し、DataInputStream を使用して結果を SalesForce に送信します。

   //Getting the parameters from request
 String contentType = request.getContentType();
 DataInputStream in = new DataInputStream(request.getInputStream());
 int formDataLength = request.getContentLength();

 //System.out.println(formDataLength);
 byte dataBytes[] = new byte[formDataLength];
 int byteRead = 0;
 int totalBytesRead = 0;
 while (totalBytesRead < formDataLength) 
 {
  byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
  totalBytesRead += byteRead;
 }

正常に動作しますが、コードが通常の文字を処理する場合のみです。特殊文字 (フランス語の文字: àâäæçéèêëîïôùûü など) を処理しようとするたびに、結果として次の意味不明な結果が得られます。

ã¢ãã¦ã§ã©ã¨ãªãããããããã¹ãã¼

DataInputStream の問題である可能性があり、UTF-8 でエンコードされたテキストが返されないことを理解しています。この問題に取り組む方法について何か提案はありますか?

すべての .jsp ファイルには <%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%> が含まれており、Tomcat の設定は問題ありません (URI = UTF-8 など)。追加してみました:

request.setCharacterEncoding("UTF-8");

response.setCharacterEncoding("UTF-8");

無駄に。

データを解析する方法の例を次に示します。

    //Getting the notes for the Case 
 String notes = new String(dataBytes);
 System.out.println(notes);
 String savenotes = casetype.substring(notes.indexOf("notes"));
 //savenotes = savenotes.substring(savenotes.indexOf("\n"), savenotes.indexOf("---"));
 savenotes = savenotes.substring(savenotes.indexOf("\n")+1);
 savenotes = savenotes.substring(savenotes.indexOf("\n")+1);
 savenotes = savenotes.substring(0,savenotes.indexOf("name=\"datafile"));
 savenotes = savenotes.substring(0,savenotes.lastIndexOf("\n------"));
 savenotes = savenotes.trim();

前もって感謝します。

4

1 に答える 1

7

入力ストリームは文字を処理せず、バイトのみを処理するため、問題は入力ストリームにはありません。あなたの問題は、それらのバイトを文字に変換する時点にあります。Stringこの特定のケースでは、コンストラクターで適切なエンコーディングを指定する必要があります。

String notes = new String(dataBytes, "UTF-8");

以下も参照してください。


ところで、DataInputStream特定のコード スニペットには追加の価値はありません。あなたはそれを保つことができますInputStream

于 2010-12-21T23:56:56.013 に答える