2014-02-20 19 views
0

我有一個非常基本的測試失敗,我無法弄清楚爲什麼。Java URLDecoder返回?

這裏是我的代碼

System.out.println(URLEncoder.encode("去", "UTF-8")); // result = "%E5%8E%BB" 
System.out.println(URLDecoder.decode("%E5%8E%BB", "UTF-8")); result = ? 

爲什麼第二的System.out導致?我期待再次看到去。

要添加到更大的圖片,我將在JavaScript中使用encodeURIComponent()將我的數據發佈到我想使用URLDecoder.decode的servlet,但我甚至無法獲得上述示例的工作。我錯過了什麼?

更新:剛剛注意到一些奇怪的事情,當我在servlet中運行代碼時,我得到了我描述的結果,但是如果我只是在它的主要方法中運行它。這裏是我的servlet代碼

import java.io.BufferedWriter; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.net.URLDecoder; 
import java.net.URLEncoder; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class SaveFile extends BasicServiceServlet { 
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    //request.setCharacterEncoding("UTF-8"); 
    //response.setContentType("text/html; charset=UTF-8"); 
    String DIR = getBaseUrl(); 

    String project = request.getParameter("project"); 
    String foldername = request.getParameter("foldername"); 
    String filename = request.getParameter("filename"); 
    String fileContent = (String)request.getParameter("content"); 
    String ch = (String)request.getParameter("char"); //char = 去 
    String pathToFile = DIR + project + "/" + foldername + "/" + filename; 
    System.out.println(URLEncoder.encode("去", "UTF-8")); //reults in %E5%8E%BB 
      System.out.println(URLDecoder.decode(ch, "UTF-8")); // results in ? 
    System.out.println(URLDecoder.decode("%E5%8E%BB", "UTF-8")); //results in ? 
    System.out.println("去".equals(URLDecoder.decode("%E5%8E%BB", "UTF-8"))); //this results in true 

    try { 
     //writing it to file results in ? 
     BufferedWriter out = new BufferedWriter(new FileWriter(pathToFile)); 
     out.write(URLDecoder.decode(fileContent, "UTF-8")); 
     out.close(); 
     System.out.println("STAT - SaveFile " + filename); 
    }catch(IOException e){ 
     System.out.println("STAT - SaveFile Error"); 
    } 
    } 
} 

但運行一個簡單的方法主要適用於我

import java.io.UnsupportedEncodingException; 
import java.net.URLDecoder; 
import java.net.URLEncoder; 

public class test { 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    try { 
     System.out.println(URLEncoder.encode("去", "UTF-8")); 
     System.out.println(URLDecoder.decode("%E5%8E%BB", "UTF-8")); 
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
} 
+1

您的控制檯輸出格式應該是utf-8。你在使用哪個IDE? – Rohan

+0

我正在使用eclipse – ryuutatsuo

+1

您是否嘗試過寫入文件並在編輯器中查看它?您的控制檯可能不使用UTF-8編碼。 – cosjav

回答

2

只有您的控制檯的顯示問題。 編碼/解碼工作正常,你可以看到使用下面的代碼:

System.out.println("去".equals(URLDecoder.decode("%E5%8E%BB", "UTF-8"))); //displays "true" 

- 編輯 -

你的servlet代碼不工作,可能是因爲你不指定字符編碼時編寫器,所以它使用默認的編碼。改爲使用以下內容:

new OutputStreamWriter(new FileOutputStream(pathToFile), "UTF-8"); 
0

首先必須正確編碼java文件。查看該文件的屬性,並正確設置編碼。

在Properties中,在Resources標籤中將Text File Encoding設置爲UTF-8。

控制檯輸出使用爲文件定義的編碼。那就是你得到的原因?

0

沒有錯過你的代碼,也編譯好。 只檢查兩樣東西: 1.如果此代碼到像JSP網頁然後確保charset & pageEncoding是UTF-8 2.檢查項目屬性&確保您的項目編碼是UTF-8,見附件圖片 enter image description here