Java和String俄罗斯编码错误

时间:2013-09-29 11:18:31

标签: java encoding

我正在构建一个从包含俄语数据的客户端JSON获取的应用程序,但是当我尝试打印它时,使用数据或将其返回到客户端我得到一堆“?????”

这是客户方:

<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("someemail@gmail.com"));
                           msg.addRecipient(
                                         Message.RecipientType.TO,
                                         new InternetAddress("someanotheremail@gmail.com",
                                                       "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":"sadsad@sdsad.com","phone":"3245324324","subject":"ТЕМА","message":"ВСЯКАЯ ФИГНЯ И ДРУГОЙ ТЕКСТ"}

这就是我得到的(系统输出)

{"first":"????","last":"??????","email":"sadsad@sdsad.com","phone":"3245324324","subject":"????","message":"?????? ????? ? ?????? ?????"}

我尝试使用UTF8编码创建一个新字符串但它不起作用。 请帮忙,

此致 IDO

1 个答案:

答案 0 :(得分:5)

System.out不会在不支持UTF-8的控制台上正确打印UTF-8字符。确保启用控制台设置以使用UTF-8编码。如果您正在使用eclipse,那么您可以通过转到:

来更改控制台设置
  

运行配置 - &gt;共&GT;编码 - &gt;从drop中选择UTF-8   下来。

这是一张图片来帮助你:

enter image description here