ロシア語のデータを含むクライアント 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 エンコーディングで新しい文字列を作成しようとしましたが、うまくいきませんでした。助けてください、
よろしく、イド