0

我有一个上传vcf文件的客户端,我在服务器端获取这个文件并读取它的内容并将它们保存到一个 txt 文件中。但是当我尝试阅读时出现字符错误,如果有土耳其字符,它看起来像“?”。我的阅读代码在这里:

            FileItemStream item = null;
            ServletFileUpload upload = new ServletFileUpload();
            FileItemIterator iterator = upload.getItemIterator(request);
            String encoding = null;
            while (iterator.hasNext()) {
                item = iterator.next();
                if ("fileUpload".equals(item.getFieldName())) {
                    InputStreamReader isr = new InputStreamReader(item.openStream(), "UTF-8");
                    String str = "";
                    String temp="";
                    BufferedReader br = new BufferedReader(isr);
                    while((temp=br.readLine()) != null){
                        str +=temp; 
                    }
                    br.close();
                    File f = new File("C:/sedat.txt");
                    BufferedWriter buf = new BufferedWriter(new FileWriter(f));
                    buf.write(str);
                    buf.close();
             }
4

1 回答 1

1
BufferedWriter buf = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "UTF-8"));

如果这是生产代码,我建议将输出直接写入文件,而不是先将其累积到字符串中。而且,您可以通过将源读取为 InputStream 并将其写入为 OutputStream (并跳过转换为字符)来避免任何潜在的编码问题。

于 2011-12-27T13:38:13.067 回答