I am working on a Java application. I need to get UTF-8 encoding in my Java webapp to support Bengali (বাংলা) text. I have done the following:
Tomcat's server.xml
<Connector port="8080"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />
<Connector executor="tomcatThreadPool"
port="8080"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />
<Connector protocol="AJP/1.3"
address="::1"
port="8009"
redirectPort="8443"
URIEncoding="UTF-8" />
JVM defaultCharset in catalina.bat file
set JAVA_OPTS=%JAVA_OPTS% -Dfile.encoding=UTF-8
properties in application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/database_name?useUnicode=true\&characterEncoding=UTF-8
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
server.tomcat.uri-encoding=UTF-8
spring.webflux.multipart.headers-charset=UTF-8
spring.thymeleaf.encoding=UTF-8
meta tag in html file
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
<meta charset="utf-8">
</head>
<body>
</body>
</html>
utf-8 support in form tag
<form enctype="multipart/form-data" accept-charset="UTF-8" action="#" th:action="@{/create}" th:object="${object}" th:method="POST">
<div class="form-group">
<label for="name" class="col-form-label">Name</label>
<input type="text" class="form-control" id="name" name="name" th:field="*{name}" placeholder="Enter Name">
</div>
<div class="form-group">
<label for="photo">Photo</label>
<input type="file" class="form-control-file" id="photo" name="photo"/>
</div>
<div>
<button class="btn" type="submit">Submit</button>
</div>
</form>
MySQL configuration (my.ini)
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
MySQL properties:
Database:
Default collation: utf8mb4_0900_ai_ci
Default charactterset: utf8mb4
Table:
Table collation: utf8mb4_0900_ai_ci
Column:
Type: varchar(255)
Character Set: utf8mb4
Collation: utf8mb4_0900_ai_ci
Configuration:
- Java 11.0.2
- Tomcat 8.5
- MySQL 8.0.16
- Spring Boot 2.2.4
- Maven 3.8.1
- Windows Server 2019 Standard (Production) + Windows 10 Home (Development)
When I submit a form with value আনোয়ার
, it is saved as আনোয়ার
How can I solve this problem?
When I run the application from eclipse it works fine. But when the war file is deployed in Tomcat server it does not work.
I tried the following code. It prints আনোয়ার
in tomcat8-stdout file. So I think problem occurs while transferring data from browser to server, from server to database is fine.
@PostMapping("/create")
public String create(@ModelAttribute("object") Object object, @RequestParam("photo") MultipartFile photo) throws IOException {
System.out.println(object.getName());
return "redirect:/index";
}