gpt4 book ai didi

Java 和 String Russian 编码错误

转载 作者:行者123 更新时间:2023-11-30 06:22:37 25 4
gpt4 key购买 nike

我正在构建一个应用程序,该应用程序从包含俄语数据的客户端 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 编码创建一个新字符串,但没有成功。请帮助,

问候,伊多

最佳答案

System.out 不会在不支持 UTF-8 的控制台上正确打印 UTF-8 字符。确保您的控制台设置已启用以使用 UTF-8 编码。如果您使用的是 Eclipse,那么您可以通过以下方式更改控制台设置:

Run Configuration -> Common-> Encoding -> select UTF-8 from the drop down.

这是一张可以帮助你的图片:

enter image description here

关于Java 和 String Russian 编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19077429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com