2

サーブレット (eap 6.1) で xml ファイル (utf-8 文字を含む) をアップロードしていますが、解析されたデータは utf-8 ではありません。多くのチュートリアルを試しましたが、うまくいきませんでした。解析されたデータは、ファイル、コンソール、およびデータベースに出力されます。すべてがutf-8で構成されています。だからコード:

@WebServlet(name = "penDataConsumer", urlPatterns = { "/pens" })
public class DigiPenServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String path = "/Users/didar/";

@Inject
private Logger log;

@Inject
private Pens pens;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("Hello World! Digital Pen");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        request.setCharacterEncoding("UTF-8");
        processUpload(request, response);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void processUpload(HttpServletRequest request, HttpServletResponse response) throws Exception {
    PrintWriter outResp = response.getWriter();
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(this.path + "log.txt", true)));
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    String sendfile = "";
    try {
        FileItemIterator iter = new ServletFileUpload(new DiskFileItemFactory()).getItemIterator(request);
        while(iter.hasNext()){
            FileItemStream item = iter.next();
            if (item.getFieldName().equals("sendfile")) {
                String filename = FilenameUtils.getName(item.getName());
                InputStream filecontent = item.openStream();
//                  sendfile = Streams.asString(filecontent, "UTF-8");
                PrintWriter outFile = new PrintWriter(new File(this.path + "files/" + filename));
                Date date = new Date();
                System.out.println("blanks/pens: " + dateFormat.format(date) + " - " + filename + ": ");
                StringWriter writer = new StringWriter();
                IOUtils.copy(filecontent, writer, Charset.forName("UTF-8"));
                sendfile = writer.toString();

                outFile.print(sendfile);
                outFile.close();
            }
            out.flush();
        }

        System.out.println("Input file start-----------------------------------------------------------");
        System.out.println(sendfile);
        System.out.println("Input file end-------------------------------------------------------------");
        outResp.print("done");
    } catch (FileUploadException e) {
        throw new ServletException("Cannot parse multipart request.", e);
    } catch (Exception e) {
        throw new ServletException("Other Exception", e);
    } finally {
        out.close();
        outResp.close();
    }
}

}

およびhtmlページ:

<html>
<body>
<form enctype="multipart/form-data" action="http://localhost:8080/blanks/pens"     method="post">
File: <input type="file" name="sendfile">
<input type="submit" value="Submit">
</form>
</body>
</html>

バージョン: io.utils - 2.4 および fileupload - 1.3

4

0 に答える 0