2013-09-29 27 views
2

我正在構建一個應用程序,它從包含俄羅斯數據的客戶端JSON獲取,但是當我嘗試打印它時,使用數據或將其返回給客戶端我會得到一堆「? ???」Java和字符串俄羅斯錯誤的編碼

這是客戶端:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<script src="js/angular.min.js"></script> 
<script type="text/javascript"> 
     function Ctrl($scope,$http) { 
       $scope.submit = function() { 
        //alert($scope.first+" "+$scope.last+" "+$scope.email+$scope.phone+$scope.subject+$scope.message); 
        $http({ 
          method : 'GET', 
          url : '/healtcare', 
          headers: {'Content-Type': 'application/json; charset=UTF-8'}, 
          params: {items:$scope.a} 
          /* params : { 
            first : $scope.first, 
            last : $scope.last, 
            email : $scope.email, 
            phone : $scope.phone, 
            subject : $scope.subject, 
            message : $scope.message 
          } */ 
        }).success(function(data, status, headers, config) { 
          alert(data); 
        }); 
       }; 
     } 
</script> 

這是服務器端:

import java.io.ByteArrayOutputStream; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.UnsupportedEncodingException; 

import javax.servlet.ServletException; 
import javax.servlet.http.*; 

import java.util.Date; 
import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

import javax.activation.DataHandler; 
import javax.mail.Multipart; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMultipart; 

import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 
import org.json.simple.parser.ParseException; 

import com.google.appengine.api.datastore.Blob; 





@SuppressWarnings("serial") 
public class HealtCareServlet extends HttpServlet { 

     public void doGet(HttpServletRequest req, HttpServletResponse resp) 
        throws IOException, UnsupportedEncodingException { 
       // resp.setContentType("text/plain"); 
       resp.setContentType("text/html; charset=UTF-8"); 
       resp.setCharacterEncoding("UTF-8"); 
       //req.setCharacterEncoding("UTF-8"); 
       System.out.println("TEST"); 
       System.out.println(req.getParameter("items")); 



       String items = req.getParameter("items"); 

       //OutputStreamWriter out = new OutputStreamWriter(new ByteArrayOutputStream()); 
       //String encoding = out.getEncoding(); 
       System.out.println("TEST3 "+new String(items.getBytes("UTF-8"), "UTF-8")); 

       JSONParser parser = new JSONParser(); 
       Object obj; 
       try { 
        obj = parser.parse(items); 
        JSONObject jsonObject = (JSONObject) obj; 

        Properties props = new Properties(); 
        Session session = Session.getDefaultInstance(props, null); 

        try { 
          MimeMessage msg = new MimeMessage(session); 
          msg.setFrom(new InternetAddress("[email protected]")); 
          msg.addRecipient(
             Message.RecipientType.TO, 
             new InternetAddress("[email protected]", 
                 "Client request from " 
                    + (String) jsonObject.get("phone") 
                    + " " + (String) jsonObject.get("last") 
                    + " Client phone no:" 
                    + (String) jsonObject.get("phone"),"UTF-8")); 

          MimeBodyPart textPart = new MimeBodyPart(); 
          // textPart.setContent(jsonObject.get("message"), "text/plain"); 

          String htmlBody = "<html><body><h1 style='background:#ffc;border:2px solid #ffc;margin:0 0 5px 0;float:left;width:100%;padding:6px 0;'>" 
             + (String) jsonObject.get("first") 
             + " " 
             + (String) jsonObject.get("last") 
             + " " 
             + (String) jsonObject.get("phone") 
             + " " 
             + (String) jsonObject.get("email") 
             + "</h1>" 
             + "<h2 style='border:1px solid gray;margin:0 6px;'>" 
             + (String) jsonObject.get("message") 
             + "</h2></body></html>"; 

          Multipart mp = new MimeMultipart(); 

          MimeBodyPart htmlPart = new MimeBodyPart(); 
          htmlPart.setContent(htmlBody, "text/html"); 
          htmlPart.setHeader("Content-type", "text/html; charset=UTF-8"); 

          mp.addBodyPart(textPart); 
          mp.addBodyPart(htmlPart); 
          msg.setSubject((String) jsonObject.get("subject"), "UTF-8"); 
          msg.setSentDate(new Date()); 

          msg.setContent(mp); // msg.setText(message,"UTF-8"); 
          Transport.send(msg); 
          resp.setContentType("text/plain"); 
          resp.getWriter() 
             .println(
                 "Thank you for your feedback. An Email has been send out."); 
        } catch (AddressException e) { 
          resp.setContentType("text/plain"); 
          resp.getWriter().println(
             "Something went wrong || error - 1. Please try again."); 
          throw new RuntimeException(e); 
        } catch (MessagingException e) { 
          resp.setContentType("text/plain"); 
          resp.getWriter() 
             .println(
                 "Something went wrong || error - 2. Please try again."); 
          throw new RuntimeException(e); 
        } catch (IOException e) { 
          resp.setContentType("text/plain"); 
          resp.getWriter() 
             .println(
                 "Something went wrong || error - 3. Please try again."); 
          throw new RuntimeException(e); 
        } 
       } catch (ParseException e) { 
        e.printStackTrace(); 
       } 

       // System.out.println(req.getParameter("first")); 
       // System.out.println(req.getParameter("last")); 
       // System.out.println(req.getParameter("email")); 
       // System.out.println(req.getParameter("phone")); 
       // System.out.println(req.getParameter("subject")); 

       // resp.getWriter().println("Hello, "+first+" "+email+" "+last+" "+phone+" "+subject+" "+message); 
       // resp.sendRedirect("/index.html"); 
     } 
} 

這是我送:

{"first":"ИВАН","last":"ВГОВНО","email":"[email protected]","phone":"3245324324","subject":"ТЕМА","message":"ВСЯКАЯ ФИГНЯ И ДРУГОЙ ТЕКСТ"} 

這是我所得到的(系統出)

{"first":"????","last":"??????","email":"[email protected]","phone":"3245324324","subject":"????","message":"?????? ????? ? ?????? ?????"} 

我試圖用UTF8編碼創建一個新的字符串,但它沒有工作。 請幫幫忙,

問候, 伊

+0

尼斯姓。那麼,至少它是好的,它顯示爲問號...... :)) –

回答

5

的System.out將不能正常不支持UTF-8在控制檯上打印UTF-8字符。確保您的控制檯設置已啓用以使用UTF-8編碼。如果使用的是Eclipse,那麼你可以去改變控制檯設置: - >共>編碼 -

運行配置>從下拉 下選擇UTF-8。

這裏是幫助ü的圖像:

enter image description here

+0

感謝生病檢查它,但它也發生在我嘗試發送郵件時。我把信息的價值(俄語),不能發送。 –

+0

它沒有工作.....請幫助 –