Spring Rest - file upload to support UTF-8 file names - spring

In I am using file upload with Spring rest and MultipartFile.
everything is fine, but issue on UTF-8. it is NOT supporting international file name - it is replacing characters to "?" like this.
I tried to use encoding Filter -org.springframework.web.filter.CharacterEncodingFilter , but no Luck.
But surprised, same above filter working fine for JSP form, without Rest
like this
<form:form method="POST" action="uploadFile" enctype="multipart/form-data" accept-charset="UTF-8">
Please help, how to support this utf-8 in Spring Rest.

There are three steps to support - file upload with international name in Rest.
Configure the server to support UTF
in my case Jboss 6.1
<property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
Use UTF-8 filter
org.springframework.web.filter.CharacterEncodingFilter
pass the file name as parameter, and in host read the file from parameter instead of MultipartFile

Related

UTF-8 encoding issues with Spring resourcebundle for special characters

I am using Spring ResourceBundle to retrieve message bundle from my .properties file. It constains special european characters like so:
Ü, ß. ä, ö, ü
MessageSouce bean is as below (I am making sure UTF-8 encoding is followed)
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:/META-INF/i18/messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
When I try to retrieve the message in my Java code I am getting Junk characters.
If I use below code, it helps to recognize few characters, but rest are still showing as ??
return new String (bundleString.getBytes(), "UTF-8")
Then I used below to encode my properties file, but still no effect
native2ascii -encoding utf8 resources.utf8 resources.properties
I also tried to manually open my properties file in Notepad++ and set UTF-8 encode, but no effect.
I see a post here, having exact same problem as mine. But the solution uses PropertiesBundle, whereas I have to use Spring based solution only. However even the accepted answer in that link is not working for me, and giving junk characters.
Please suggest any possible solution.
I was having the same issue and #sunny_dev's answer worked for me.
I don't understand why there is not answer for this question yet so I'm updating the question.
#sunny_dev answer:
Karol, I solved the problem by opening and saving the .properties file
in TextPad as "UTF-8" encoding and "Unix" platform. I had taken that
same approach, however in Notepad++ without the positive outcome
earlier. – sunny_dev
Thanks again to #sunny_dev

Character Encoding Issue with spring application

I am building an application using spring mvc and jpa using jboss7 and mysql in eclipse ide. I am having a strange problem. All my jsp pages are encoded with charset: utf8, which I think is working correctly. But whenever I try to post a data from the jsp to the controller, my data gets encoded with a different encoding style. I tried to look for the header using firebug and was astonished to see that the post request has a header with content-type : "text/plain;charset=ISO-8859-1". I have already configured the SetCharacterEncodingFilter for UTF-8 in my web.xml (it is the first filter). But still the problem exists.
I also set "org.apache.catalina.connector.URI_ENCODING" to value="UTF-8".But in vain .
Also I have added bean messageSource with property defaultEncoding set to "UTF-8".
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" >
The problem still exists. Please help
Thanks in advance.
The request header is set by the browser, so your application can't control it. Usually, in your HTML form you could put an accept-charset=utf-8 attribute to specify the encoding, but that doesn't necessarily work. See this question Setting the character encoding in form submit for Internet Explorer.
you should need to set the encoding of the JVM like follow :
-Dfile.encoding=UTF-8 -Dfile.io.encoding=UTF-8 -DjavaEncoding=UTF-8
thus there wont be any doubt at all.

Hibernate Validator Not Correctly Displaying Unicode Characters

I am building a Spring MVC web application that uses JSR-303 validation. With the first form I created, I added a couple of validation annotations (including custom error message codes) to the form backing bean. I also created ValidationMessages.properties and ValidationMessages_en.properties files.
Everything seems to be working correctly with one exception: multi-byte utf-8 encoded characters are not displayed correctly (e.g., "ñ" is displayed as "ñ").
This is not a problem with my standard messages.properties and messages_en.properties files that I use for field labels and other text, so I'm assuming it's an issue with the hibernate validator code. Has anyone else had this issue and solved it? FYI, I'm using Hibernate version 4.3.0.Final.
Thanks,
Peter
In my properties files I must include special characters like this:
\u00F3 instead of ó
In that way they are shown well.
Hope it helps.
P.S.: Using ResourceBundleEditor from Eclipse also helps.

Spring Web MVC 3.1 sound files content type decorated with UTF-8

I have a Spring MVC 3.1 application, and a 3rd party application is failing because Spring decides to add ; charset=UTF-8 to requests for sound files (.mp3).
For example, "hello.mp3" is returned with a content type header of:
audio/mpeg;charset=UTF-8
How can I configure Spring so that it doesn't decorate the content type for .mp3 files with the UTF-8 charset suffix ?
The CharacterEncodingFilter is applied to every request, so each response gets the encoding appended. This is not a big problem, but having forceEncoding=false (which is the default) may help.

Unwanted i18n in Spring Security's error message

I have a problem with Spring Security 3 and probably with i18n. I use Velocity for my view templates and I have the following code in one of them:
#if($SPRING_SECURITY_LAST_EXCEPTION)
<div class="error"><p>#springMessage($SPRING_SECURITY_LAST_EXCEPTION.message)</p></div>
#end
When user tried to login and he passed bad credentials for example, there should be error message shown on the page. The problem is that application try to resolve message in locale 'pl' and I receive the following exception:
org.springframework.context.NoSuchMessageException: No message found under code 'Bad credentials' for locale 'pl'.
In my configuration files I don't have any informations about i18n because I just don't need it.
I noticed that information about 'pl' locale can be retrieved from request (header Accept-Language: pl,en-us;q=0.7,en;q=0.3).
How to ignore this and serve pages only in defult language?
If you don't want i18n, don't use #springMessage. $SPRING_SECURITY_LAST_EXCEPTION.message is a message itself, not message code, thus you need to output it as is (though I'm not sure about HTML escape in Velocity):
#if($SPRING_SECURITY_LAST_EXCEPTION)
<div class="error"><p>${SPRING_SECURITY_LAST_EXCEPTION.message}</p></div>
#end
I've had fun with il8n in the past. What I do is to force spring to only use the default language, I've done this by using a FixedLocaleResolver in the config files. Change the value to which ever locale you want to have as default.
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
<property name="defaultLocale" value="en_US" />
</bean>

Resources