顯示具有 Unicode 標籤的文章。 顯示所有文章
顯示具有 Unicode 標籤的文章。 顯示所有文章

星期五, 8月 05, 2011

[Java] Servlet file upload filename encoding (中文亂碼)

解決中文檔名亂碼問題:在init ServletFileUpload呼叫setHeaderEncoding指定編碼為utf-8,
就能正確取出中文了

ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
List items = null;
   try {
    items = upload.parseRequest(request);
//    System.out.println("item is added.");
   } catch (FileUploadException e) {
    System.out.println("FileUploadException:" + e.getMessage());
   }
   if (items != null) {
//    System.out.println("items count:" + items.size());
    Iterator iter = items.iterator();
    while (iter.hasNext()) {
                                  FileItem item = iter.next();
                                  String filename = item.getName();
                                }
                        }
Reference:
servlet file upload filename encoding

星期一, 6月 06, 2011

[Java] UnicodeFormatter

/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 


import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;

public class UnicodeFormatter  {

   static public String byteToHex(byte b) {
      // Returns hex String representation of byte b
      char hexDigit[] = {
         '0', '1', '2', '3', '4', '5', '6', '7',
         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
      };
      char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
      return new String(array);
   }

   static public String charToHex(char c) {
      // Returns hex String representation of char c
      byte hi = (byte) (c >>> 8);
      byte lo = (byte) (c & 0xff);
      return byteToHex(hi) + byteToHex(lo);
   }
}

星期三, 6月 01, 2011

[Java] Fake UTF-16

static public String CoverterUtf8ToFakeUtf16(String utf8String) throws UnsupportedEncodingException {
    
    byte[] inputBytes = utf8String.getBytes("UTF-8");
    printBytes(inputBytes,"input bytes:");
    
    ArrayList list = new ArrayList();
    //convert input bytes to utf-16
    for(int i=0;i<inputBytes.length;i++){
     list.add(new Byte("00"));
     list.add(inputBytes[i]);
    }
    
    byte[] returnByte = new  byte[list.size()];
    for(int i=0;i<returnByte.length;i++){
     returnByte[i] = (Byte)list.get(i);
    }
    
    returnByte = new String(returnByte, Charset.forName("UTF-16")).getBytes("UTF-8");
    printBytes(returnByte,"return bytes:");
    
    String fakeUtf16 = new String(returnByte,Charset.forName("UTF-8"));
    printBytes(fakeUtf16.getBytes("UTF-8"),"fakeUtf16 bytes:");
    
    return fakeUtf16;
   }

Testing
public void fakeUtf16(InputStream requestBodyStream ) throws UnsupportedEncodingException {
String requestBODY = RequestBODY.get(requestBodyStream,Charset.forName("utf-8"));
System.out.println("requestBODY:" + requestBODY);
String fakeUtf16 = UnicodeFormatter.CoverterUtf8ToFakeUtf16(requestBODY);
System.out.println("fakeUtf16:" + fakeUtf16);
}
Console 輸出訊息
--fakeUtf16--
charset:UTF-8
requestBODY:中
input bytes:[0] = 0xe4
input bytes:[1] = 0xb8
input bytes:[2] = 0xad
return bytes:[0] = 0xc3
return bytes:[1] = 0xa4
return bytes:[2] = 0xc2
return bytes:[3] = 0xb8
return bytes:[4] = 0xc2
return bytes:[5] = 0xad
fakeUtf16 bytes:[0] = 0xc3
fakeUtf16 bytes:[1] = 0xa4
fakeUtf16 bytes:[2] = 0xc2
fakeUtf16 bytes:[3] = 0xb8
fakeUtf16 bytes:[4] = 0xc2
fakeUtf16 bytes:[5] = 0xad
fakeUtf16:中
fakeUtf16:[0] = 0xfe
fakeUtf16:[1] = 0xff
fakeUtf16:[2] = 0x00
fakeUtf16:[3] = 0xe4
fakeUtf16:[4] = 0x00
fakeUtf16:[5] = 0xb8
fakeUtf16:[6] = 0x00
fakeUtf16:[7] = 0xad
--/fakeUtf16--

其他你感興趣的文章

Related Posts with Thumbnails