2017-12-01 19 views
0

ねえ、私は、Webアプリケーションで作業し、TXTファイルからの読み込みUTF-8文字に問題がありますよ。私はUTF-8をそのように働かせます:UTF-8 web encoding(それはインポート時以外はうまくいきます)。私は多くの考え(特に:read UTF-8 string literal java)を試してみましたが、何も働かず、なぜ私は考えていません。UTF-8エンコーディングでtxtファイルをjspにインポートする方法は?

にとって必須codesnippets:

import.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
pageEncoding="UTF-8"%> 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fi"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Import</title> 
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
<script src="script.js"></script> 
<link rel="stylesheet" type="text/css" 
media="screen and (min-device-width: 500px)" href="style.css" /> 
<link rel="stylesheet" 
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> 
</head> 
<body> 
<form> 
    <!-- show import data --> 
</form> 
<form id="importForm" action="${pageContext.request.contextPath}/ImportData" method="post" onsubmit="return importValidation();" enctype="multipart/form-data"> 
    <input type="file" name="file" accept=".txt"/> 
    <input type="submit" value="Import"> 
</form> 

</body> 
</html> 

IMPORTDATAサーブレット:

import java.nio.charset.StandardCharsets; 

@WebServlet("/ImportData") 
@MultipartConfig 
public class ImportData extends HttpServlet { 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file"> 
     BufferedReader buf = new BufferedReader(new InputStreamReader(filePart.getInputStream(), StandardCharsets.UTF_8.name())); 
     String lineJustFetched = null; 
     String[] wordsArray = null; 
     ArrayList<String> texts = new ArrayList<String>(); 
     while(true){ 
      lineJustFetched = buf.readLine(); 
      if(lineJustFetched == null){ 
       break; 
      }else{ 
       wordsArray = lineJustFetched.split("\t"); 
       for(String each : wordsArray){ 
        texts.add(each); 
       } 
      } 
     } 
     buf.close(); 

     System.out.println(texts); 

     //create Import Data in Backend and write it into db 

     response.sendRedirect("import.jsp"); 
    } 
} 

システムの詳細:Javaの1.7

テキストのoutprintと Tomcatサーバー7 UTF-8の文字は正方形で、htmlの入力になります(どこで、なぜ私はUTF-8エンコーディングを失ったん:テキスト)だから私の質問がある代わりにUTF-8文字の

のですか?

+0

あなたは、端末に出力を表示していますか?そして元のファイルがUTF-8ファイルであることは確かですか? – VGR

+0

詳細情報なしで言うのは難しいですが、 文字はファイルを正しく読み込んだと思いますが、ユニコード文字を印刷できないと思います... –

+0

VGR時:私はサーバーへのパテを持つトンネルを構築します。アウトプリントを参照してください。元のファイルにはUTF-8文字が含まれているので、UTF-8ファイルだと思います。 –

答えて

0

私は正しく見えませんでした。このファイルはUTF-8でエンコードされていません(ANSIエンコードされています)。

あなただけのファイルを正しく読み取るためにInputStreamReaderエンコーディングを変更する必要があり、他のエンコーディングのため、それが実行可能にします。

(窓-ANSI用)
BufferedReader buf = new BufferedReader(new 
     InputStreamReader(filePart.getInputStream(), "Cp1252")); 

関連する問題