天天看點

檔案上傳 相容中文檔案名 中文亂碼

        //like12 add,20200630,相容中文檔案名

        byte buf[];

        try {

            buf = oldName.getBytes("iso8859-1");

            oldName = new String(buf, "utf-8");

            //System.out.println("相容中文檔案名:" + oldName);

        } catch (Exception e1) {

            // TODO Auto-generated catch block

            e1.printStackTrace();

        }

        //中文檔案名處理

        String fileNamePre = oldName.substring(0, oldName.indexOf("."));

        System.out.println("編碼前檔案名:" + fileNamePre);

        try {

            fileNamePre = URLEncoder.encode(fileNamePre, "UTF-8");

        } catch (UnsupportedEncodingException e1) {

            // TODO Auto-generated catch block

            e1.printStackTrace();

        }

        System.out.println("編碼後檔案名:" + fileNamePre);