[小记]JAVA对URL中的字符串处理(分割,转码)


public static void main(String[] args) {
        //原始URL: http://localhost:9000/export/export?docids=AAwAADBYubCTAAAAb%2FPP,AAwAADBYubCTAAAAb7wE,AAwAADBYubCTAAAAb%2BHa,AAwAADBYubCTAAAAb%2BIF,AAwAADBYubCTAAAAb8aT,AAwAADBYubCTAAAAb8ae
        String docids = "AAwAADBYubCTAAAAb%2FPP,AAwAADBYubCTAAAAb7wE,AAwAADBYubCTAAAAb%2BHa,AAwAADBYubCTAAAAb%2BIF,AAwAADBYubCTAAAAb8aT,AAwAADBYubCTAAAAb8ae";
        String[] doc_id = docids.split(","); //逗号进行分割成数组   
        int i = 0;
        for(String docId:doc_id){
            if(docId.contains("%")) {  //判断是否存在字符串%
                try {
                    String s = java.net.URLDecoder.decode(docId, "utf-8");   //存在进行转码
                    doc_id[i] = new String(s);                                //数组元素赋值
                    System.out.println(s);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
            }
            i++;
        }
        System.out.print(doc_id.toString());
    }

运行结果

AAwAADBYubCTAAAAb/PP
AAwAADBYubCTAAAAb+Ha
AAwAADBYubCTAAAAb+IF
[Ljava.lang.String;@15db9742

你可能感兴趣的:(JAVA)