2017-08-12 91 views
1

我正在使用Google App Engine從數據庫檢索數據。當我檢索阿拉伯語文本時,它會返回類似於「????¹????????」在Java Servlet中編碼Google App Engine

這裏的HttpServlet:

public class MyServlet extends HttpServlet { 
@Override 
public void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws IOException { 
    PrintWriter writer = response.getWriter(); 

    final String contentType = "text/plain;charset=UTF-8"; 
    final String characterEncoding = "UTF-8"; 

    response.setContentType(contentType); 
    response.setCharacterEncoding(characterEncoding); 

    writer.println(response.getContentType()); 
    writer.println(response.getCharacterEncoding()); 
    writer.println("السلام عليكم:"); 
} 
} 

輸出爲:

text/plain; charset=iso-8859-1 
ISO-8859-1 
?§???³???§?? ?¹????????: 

的AppEngine-web.xml中:

<?xml version="1.0" encoding="utf-8"?> 

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> 

    <application>myApplicationID</application> 

    <use-google-connector-j>true</use-google-connector-j> 

    <version>1</version> 

    <threadsafe>true</threadsafe> 

    <system-properties> 

     <property name="java.util.logging.config.file" value="WEB-INF/logging.properties" /> 

     <property name="file.encoding" value="UTF-8" /> 

     <property name="appengine.file.encoding" value="UTF-8" /> 

     <property name="DEFAULT_ENCODING" value="UTF-8" /> 

    </system-properties> 

    <env-variables> 

     <env-var name="DEFAULT_ENCODING" value="UTF-8" /> 

    </env-variables> 
</appengine-web-app> 

我使用:Android的工作室 - 運行時:JAVA 7 - 環境:標準

這是來自我的瀏覽器Chrome:Response and Request Headers

回答

0

我找到了解決方案。

的問題是,我得到的作家設置的內容類型

之前,所以代碼應該是:

public class MyServlet extends HttpServlet { 
    @Override 
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws IOException { 
     final String contentType = "text/plain;charset=UTF-8"; 
     final String characterEncoding = "UTF-8"; 

     response.setContentType(contentType); 
     response.setCharacterEncoding(characterEncoding); 

     PrintWriter writer = response.getWriter(); 

     writer.println(response.getContentType()); 
     writer.println(response.getCharacterEncoding()); 
     writer.println("السلام عليكم:"); 
    } 
}