This discussion is archived
6 Replies Latest reply: Jun 24, 2011 4:51 AM by gimbal2 RSS

JSP + Servlet + multipart/form-data form UTF-8 problem

843842 Newbie
Currently Being Moderated
Hello everyone,
I have a situation and I need help.
My form looks like this:
            <form enctype="multipart/form-data" action="XMLRegister" method="post">
                <p>
                <fieldset class="fi">
                    <legend class="le">Teksto anotavimas</legend>
                    <label class="la">Pasirinkite fail&#261;:</label>
                    <input class="ip" type="file" name="upload" size="30">
                    <p>
                    <label class="la">Dokumentacija galite atsisi&#371;sti iš <a href="#">&#269;ia</a>.</label>
                </fieldset>

                <p>
                    <label class="la">Naujo dokumento pavadinimas:</label>
                    <input class="ip" type="text" name="docName" size="30">
                <p>
                    <label class="la">Teksto pavadinimas:</label>
                    <input class="ip" type="text" name="textName" size="30">
                <p>
                    <label class="la">Autorius:</label>
                    <input class="ip" type="text" name="author" size="30">
                <p>
                    <label class="la">Tekst&#261; parengusi institucija:</label>
                    <input class="ip" type="text" name="organization" size="30">
                <p>
                    <label class="la">Ledimo metai:</label>
                    <input class="ip" type="text" name="date" size="30">
                <p>
                    <label class="la">Tema:</label>
                    <select class="se" name="topic" size="1">
                        <option>Mokslini&#371; tyrim&#371; politika</option>
                        <option>Aukštojo mokslo politika</option>
                        <option>T&#281;stinio mokymosi politika</option>
                        <option>Profesinio rengimo politika</option>
                        <option>Kelios temos</option>
                    </select>
                <p>
                    <label class="la">Tipas:</label>
                    <select class="se" name="type" size="1">
                        <option>Lietuviški tekstai</option>
                        <option>ES ir verstiniai tekstai</option>
                    </select>
                <p>
                <label class="la">Žanras:</label>
                    <select class="se" name="genre" size="1">
                        <optgroup label="Lietuviški tekstai">
                            <option>&#302;statymai</option>
                            <option>Kiti aukš&#269;iausi&#371; valdžios institucij&#371; politiniai dokumentai</option>
                            <option>Strateginiai dokumentai</option>
                            <option>Po&#303;statyminiai aktai</option>
                            <option>Mokslo ir ugdymo politikos &#303;gyvendinimo institucij&#371; ir projekt&#371; dokumentai</option>
                            <option>Mokslo ir studij&#371; institucij&#371; dokumentai</option>
                            <option>Informaciniai leidiniai, srities b&#363;kl&#279;s tyrimai, vertinimai, galimybi&#371; studijos</option>
                            <option>Spaudos pranešimai, diskusij&#371; medžiaga</option>
                        </optgroup>
                        <optgroup label="ES ir verstiniai tekstai">
                            <option>ES sutartys</option>
                            <option>Dvišal&#279;s ir daugiašal&#279;s sutartys, j&#371; &#303;gyvendinim&#261; reglamentuojantys dokumentai</option>
                            <option>ES tarybos, EP, EK ir kit&#371; tarptautini&#371; institucij&#371; politiniai dokumentai</option>
                            <option>Strateginiai dokumentai</option>
                            <option>Mokslini&#371; tyrim&#371;, ugdymo sri&#269;i&#371; bendradarbiavimo program&#371; dokumentai</option>
                            <option>ES ir kit&#371; tarptautini&#371; mokslo ir ugdymo politikos &#303;gyvendinimo institucij&#371; ir projekt&#371; dokumentai</option>
                            <option>Informaciniai leidiniai, srities b&#363;kl&#279;s tyrimai, vertinimai, galimybi&#371; studijos</option>
                            <option>Spaudos pranešimai, mokslo ir ugdymo politikos tem&#371; verstiniai straipsniai, diskusijos</option>
                        </optgroup>
                    </select>
                <p>
                <input id="sub" type="submit" value="Registruoti">
            </form>
I'm uploading file using fileupload library.
My servlet looks like this:
//------------------------------------------------------------------------------
    @Override
    public void init(ServletConfig config) throws ServletException {
     super.init(config);
     realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH);
        destinationDir = new File(realPath);
     if(!destinationDir.isDirectory()) {
            throw new ServletException(DESTINATION_DIR_PATH + " is not a directory");
     }
    }
//------------------------------------------------------------------------------
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        System.out.println(request.getParameter("docName"));

        upl = new XMLFileUpload(destinationDir);
        try {
            upl.sendUpload(request, response);
            String dir = StructureHandler.getDocName();
            String outputDir = realPath + DESTINATION_DOWNLOAD_PATH + StructureHandler.getParameter()[0];
            rw.createDocument(outputDir);
        } catch (Exception ex) {
            Logger.getLogger(XMLRegister.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        RequestDispatcher rd = request.getRequestDispatcher("/succesful.jsp");
        rd.forward(request, response);
    }
Ant the upload method:
    public void sendUpload(HttpServletRequest request, HttpServletResponse response) throws Exception{
        int i = 0;
        FileItemFactory fileItemFactory = new DiskFileItemFactory();
        ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
        try {
            request.setCharacterEncoding("UTF-8");
            List items = uploadHandler.parseRequest(request);
            Iterator itr = items.iterator();
            while(itr.hasNext()) {
                FileItem item = (FileItem) itr.next();
                if(item.isFormField()) {
                    StructureHandler.getParameter() = item.getString();
i++;
} else {
File file = new File(destinationDir, item.getName());
item.write(file);
StructureHandler.setDocName(item.getName());
tr.readFile(file.getAbsolutePath());
}
}
} catch(FileUploadException ex) {
throw new Exception("Error encountered while parsing the request " + ex);
}
}
The problem is that I can not get my symbols from fields encoded correctly.
Did I miss something?