3

帮助修复servlet中的编码,它在输出中不显示俄语字符。我将非常感谢您的回答。

那是servlet代码

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static List<String> getFileNames(File directory, String extension) {

        List<String> list = new ArrayList<String>();
        File[] total = directory.listFiles();
        for (File file : total) {
            if (file.getName().endsWith(extension)) {
                list.add(file.getName());
            }
            if (file.isDirectory()) {
                List<String> tempList = getFileNames(file, extension);
                list.addAll(tempList);          
            }
        }
        return list;
    }



@SuppressWarnings("resource")
        protected void doPost(HttpServletRequest request, HttpServletResponse response) 
                throws ServletException, IOException{ 
                request.setCharacterEncoding("utf8");
                response.setContentType("text/html; charset=UTF-8");
                String myName = request.getParameter("text");

                List<String> files = getFileNames(new File("C:\\Users\\vany\\Desktop\\test"), "txt");
                for (String string : files) {
                if (myName.equals(string)) {
                       try {
                            File file = new File("C:\\Users\\vany\\Desktop\\test\\" + string);
                            FileReader reader = new FileReader(file);
                            int b;
                            PrintWriter writer = response.getWriter();
                            writer.print("<html>");
                            writer.print("<head>");
                            writer.print("<title>HelloWorld</title>");
                            writer.print("<body>");
                            writer.write("<div>");
                            while((b = reader.read()) != -1) {
                                writer.write((char) b);
                            }
                            writer.write("</div>");
                            writer.print("</body>");
                            writer.print("</html>");

                        } 
                       catch (Exception ex) {

                        }
                    }

                }
               }
        }

这是我显示的而不是字母

Псђсђс€‰Рос€€€€€€€€€€€ріс‰с€€рісрірсђрісђрісђрс,сђрісђРс,сквасђрμр,сђрμр,Сђрμр±сђрμс,Сђрμс,Сѓсђр°

4

2 回答 2

3

您在请求而不是响应上设置字符编码。更改request.setCharacterEncoding("utf8");response.setCharacterEncoding("UTF-8");

另外:如果您系统的默认字符编码不是 UTF-8,您应该在读取文件时明确设置编码。为此,您需要使用FileInputStream

于 2012-11-07T17:11:45.570 回答
0
pRes.setContentType("text/html; charset=UTF-8");
    PrintWriter out = new PrintWriter(new OutputStreamWriter(pRes.getOutputStream(), "UTF8"), true);

使用这个我得到了确切的结果:)

于 2015-07-20T13:49:43.323 回答