Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Tomcat file upload, UTF-8 and Linux

843840Sep 18 2007 — edited Sep 18 2007
Hi.

We have some problems getting swedish characters to work correctly when uploading files and running Tomcat 5.5 on Ubuntu. When running Tomcat on windows however, it works fine. All swedish characters is displayed correctly.

We have searched around alot without any success, if anyone has any ideas or questions please let us know.


Here are some configurations and code we use:

- server.xml -
<Connector port="80" maxHttpHeaderSize="8192"
	maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
        enableLookups="false" redirectPort="8443" acceptCount="100"
        connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8"/>
- web.xml -
<filter>
	<filter-name>Charset Filter</filter-name>
	<filter-class>com.mycompany.util.CharsetFilter</filter-class>
      	<init-param>
        	<param-name>requestEncoding</param-name>
        	<param-value>UTF-8</param-value>
      	</init-param>
</filter>
<filter-mapping>
	<filter-name>Charset Filter</filter-name>
    	<url-pattern>/*</url-pattern>
</filter-mapping>
- CharsetFilter.java -
public class CharsetFilter implements Filter {
    private String encoding;
    private Logger log = Logger.getLogger(this.getClass());

    public void init(FilterConfig config) throws ServletException {
        encoding = config.getInitParameter("requestEncoding");
        if (encoding == null) encoding = "UTF-8";
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain next)
            throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        request.setCharacterEncoding(encoding);
        next.doFilter(request, response);
    }

    public void destroy() {
    }
}
upload.jsp has
<%@ page contentType="text/html; charset=UTF-8"%>
and
<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
in header.


the file upload servlet contains:
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");

List<FileItem> items = upload.parseRequest(request);
for (FileItem fi : items) {
	if (fi.isFormField()) {
        	fileName = fi.getName();
                String fileData = new String(fi.getString().getBytes("ISO-8859-1"), "UTF-8");
		// fileData is not correct in Ubuntu, but in Windows
        }
}
catalina.sh contains:
CATALINA_OPTS=-Dfile.encoding=UTF-8
export CATALINA_OPTS
and
JAVA_OPTS="$JAVA_OPTS "-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager" "-Djava.util.logging.config.file="$CATALINA_BASE/conf/logging.properties -Dfile.encoding=UTF-8"
Running locale on ubuntu:
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
Edited by: RobinS on Sep 18, 2007 5:03 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 16 2007
Added on Sep 18 2007
0 comments
444 views