java代码实现简体繁体转换
汉字分为简体和繁体字,这是众所周知的,虽然繁体字中国⼤陆这边⽤的并不会太多,但时不时的还是会使⽤到。那么,如何做到简体字和繁体字的转换呢?其实很简单,这⾥附上⼀个java版本的简繁体转换,⽤⽹上的实现的,⾮常简洁,省去不少⿇烦,直接上代码:新股申购时间
package ample;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.HttpURLConnection;
import java.URL;
import java.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class Application {
public static final String DEF_CHATSET = "UTF-8";
public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000;
public static String userAgent =  "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";
//配置您申请的appKey和openId
public static final String APP_KEY ="yours";
public static final String OPEN_ID ="yours";
//将map型转为请求参数
public static String urlEncode(Map<String,Object> params) {
if(params==null){return "";};
StringBuilder sb = new StringBuilder();
for (Map.Entry<String,Object> i : Set()) {
try {
sb.Key()).append("=").Value()+"","UTF-8")).append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
String String();
dsWith("&")){
r = r.substring(0,r.length()-1);
}
return r;
}
/**
*
* @param requestUrl 请求地址
* @param params 请求参数
* @param method 请求⽅法
* @return 请求结果
* @throws Exception
*/
public static String requestContent(String requestUrl, Map<String,Object> params,String method) throws Exception {
HttpURLConnection conn = null;
BufferedReader reader = null;
String rs = null;
try {
//组装请求链接
StringBuffer sb = new StringBuffer();
if(method!=null&&method.equalsIgnoreCase("get")){
requestUrl = requestUrl+"?"+urlEncode(params);
}
//默认get
URL url = new URL(requestUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
if(method!=null&&method.equalsIgnoreCase("post")){
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
}
//参数配置
conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_READ_TIMEOUT);
conn.setInstanceFollowRedirects(false);
if (params!= null && method.equalsIgnoreCase("post")) {
try {
DataOutputStream out = new OutputStream());                    out.writeBytes(urlEncode(params));
} catch (Exception e) {
e.printStackTrace();
}
}
//读取数据
InputStream is = InputStream();
reader = new BufferedReader(new InputStreamReader(is, DEF_CHATSET));
String strRead = null;
while ((strRead = adLine()) != null) {
sb.append(strRead);
}
rs = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
if (conn != null) {
conn.disconnect();
}
}
return rs;
}
public static void main(String[] args) throws Exception{
String domain="api.xiaocongjisuan/";
String servlet="life/simtrachinese/convert";
情侣适合住什么样的酒店String method="get";
String requestUrl=domain+servlet;
Map<String,Object> params=new HashMap<String,Object>();
params.put("appKey",APP_KEY);
params.put("openId",OPEN_ID);
//变动部分
params.put("simpleChinese","我是⼀只⼩⼩鸟");非主流网
params.put("tradtionalChinese","⽂學家的故事是這樣的");
String result=requestContent(requestUrl,params,method);
System.out.println(result);
}
}
php的代码实现如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<?php
/**
* @author
* @copyright 2019
*/
header("content-type:text/html;charset=utf-8");        //设置编码
//配置您申请的appKey和openId
$app_key = "***";
$open_id = "***";
/**
$url 请求地址
$params 请求参数
$ispost 请求⽅法
*/
function http_curl($url,$params=false,$ispost=false){
$httpInfo = array();
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );    curl_setopt( $ch, CURLOPT_USERAGENT , "xiaocongjisuan");
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
if( $ispost )黄秋葵的做法
{
curl_setopt( $ch , CURLOPT_POST , true );
重阳节卡片简单制作curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($params){
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
}
五年级下册英语试卷function main(){
global $app_key;
global $open_id;
$domain="api.xiaocongjisuan/";
$servlet="life/simtrachinese/convert";
$method="get";
$url=$domain."".$servlet;
$params['appKey']=$app_key;
$params['openId']=$open_id;
//变动部分
$params["simpleChinese"]="我是⼀只⼩⼩鸟";
$params["tradtionalChinese"]="⽂學家的故事是這樣的";
//编码转换
foreach ($params as $key=>$value) {
$params[$key]=mb_convert_encoding($value, "UTF-8", "GBK");
}
$paramstring = http_build_query($params);
$content = http_curl($url,$paramstring,true);
return $content;
}
echo main();
>
其他代码的实现⽅式可以,感谢阅读。