Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 输入流编码问题(特殊字符:ñ;,á;,…)_Java_Web Services_Utf 8_Http Post_Inputstream - Fatal编程技术网

Java 输入流编码问题(特殊字符:ñ;,á;,…)

Java 输入流编码问题(特殊字符:ñ;,á;,…),java,web-services,utf-8,http-post,inputstream,Java,Web Services,Utf 8,Http Post,Inputstream,欢迎大家,我目前正在开发一个web服务,我很难让这个方法与像ñ,ç,á,è这样的字符一起工作,。。。它似乎与我的输入流有关,似乎编码不正确,下面是代码: private static String sendPost(String url, Map<String, JSONObject> params) throws Exception { String responseString; StringBuilder urlParameters = new Str

欢迎大家,我目前正在开发一个web服务,我很难让这个方法与像ñ,ç,á,è这样的字符一起工作,。。。它似乎与我的输入流有关,似乎编码不正确,下面是代码:

    private static String sendPost(String url, Map<String, JSONObject> params) throws Exception {
    String responseString;

    StringBuilder urlParameters = new StringBuilder(400);
    if (params != null) {
        for (Entry<String, JSONObject> entry : params.entrySet()) {
            urlParameters.append(entry.getKey()).append("=").append(entry.getValue().toString()).append("&");
        }

    }
    url += urlParameters.toString();
    url = url.replace(" ", "%20");
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("charset", "utf-8");
    con.setDoOutput(true);
    int responseCode = con.getResponseCode();
    if (responseCode == HttpStatus.SC_OK) {
        BufferedReader in = null;
        StringBuffer response = null;
        try{
            //when i check 'con' all seems to be fine
            in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            String inputLine;
            response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
        }finally{
            in.close();
        }
        responseString = response.toString();
    } else {
        responseString = new StringBuilder(25).append(responseCode).toString();
    }
    return responseString;
}
私有静态字符串sendPost(字符串url、映射参数)引发异常{
弦乐;
StringBuilder urlParameters=新的StringBuilder(400);
如果(参数!=null){
for(条目:params.entrySet()){
urlParameters.append(entry.getKey()).append(“=”).append(entry.getValue().toString()).append(“&”);
}
}
url+=urlParameters.toString();
url=url.replace(“,“%20”);
URL obj=新URL(URL);
HttpURLConnection con=(HttpURLConnection)obj.openConnection();
con.setRequestMethod(“POST”);
con.setRequestProperty(“字符集”、“utf-8”);
con.设置输出(真);
int responseCode=con.getResponseCode();
if(responseCode==HttpStatus.SC_OK){
BufferedReader in=null;
StringBuffer响应=null;
试一试{
//当我检查“con”时,一切似乎都很好
in=新的BufferedReader(新的InputStreamReader(con.getInputStream(),“UTF-8”);
字符串输入线;
响应=新的StringBuffer();
而((inputLine=in.readLine())!=null){
追加(inputLine);
}
}最后{
in.close();
}
responseString=response.toString();
}否则{
responseString=newStringBuilder(25).append(responseCode.toString();
}
回报率;
}
例如: 在“con”内http:\direction.dom\data\W-S\something?param={示例:“castaña”}
InputStream返回:http:\direction.dom\data\W-S\something?param={示例:“casta�a“}


提前感谢。

这是一个非常棘手的案例,因为您正在处理HTTP参数。这些可以是用户在浏览器中输入的任何编码

根据您的示例,您的用户发送的数据不是
UTF-8
。它可以是
ISO-8859-1
ISO-8859-15
windows-1252


通过将正确的HTTP头设置到web表单中,您可以将用户推向UTF-8:
response.setContentType(“text/xml;charset=UTF-8)

这是一个非常棘手的问题,因为您正在处理HTTP参数。这些参数可以是用户在浏览器中输入的任何编码

根据您的示例,您的用户发送的数据不是
UTF-8
。它可以是
ISO-8859-1
ISO-8859-15
windows-1252


通过在web表单中设置正确的HTTP头,您可以将用户推向UTF-8:
response.setContentType(“text/xml;charset=UTF-8)

我的合作伙伴正在想如何解决它:

private static String sendPost(String url, Map<String, JSONObject> params) throws Exception {
    String responseString;

    StringBuilder urlParameters = new StringBuilder(400);
    if (params != null) {
        for (Entry<String, JSONObject> entry : params.entrySet()) {
            urlParameters.append(entry.getKey()).append("=").append(entry.getValue().toString()).append("&");
        }
    }

    url = url.replace(" ", "%20");
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("accept-charset", "UTF-8");
    con.setRequestProperty("content-type", "application/x-www-form-urlencoded; charset=utf-8");
    con.setDoOutput(true);

    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
    writer.write(urlParameters.toString());
    writer.close();
    wr.close();

    int responseCode = con.getResponseCode();
    if (responseCode == HttpStatus.SC_OK) {
        BufferedReader in = null;
        StringBuffer response = null;
        try{
            in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            String inputLine;
            response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
        }finally{
            in.close();
        }
        responseString = response.toString();
    } else {
        responseString = new StringBuilder(25).append(responseCode).toString();
    }
    return responseString;
}
私有静态字符串sendPost(字符串url、映射参数)引发异常{
弦乐;
StringBuilder urlParameters=新的StringBuilder(400);
如果(参数!=null){
for(条目:params.entrySet()){
urlParameters.append(entry.getKey()).append(“=”).append(entry.getValue().toString()).append(“&”);
}
}
url=url.replace(“,“%20”);
URL obj=新URL(URL);
HttpURLConnection con=(HttpURLConnection)obj.openConnection();
con.setRequestMethod(“POST”);
con.setRequestProperty(“接受字符集”、“UTF-8”);
con.setRequestProperty(“内容类型”,“应用程序/x-www-form-urlencoded;字符集=utf-8”);
con.设置输出(真);
DataOutputStream wr=新的DataOutputStream(con.getOutputStream());
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(wr,“UTF-8”));
write(urlParameters.toString());
writer.close();
wr.close();
int responseCode=con.getResponseCode();
if(responseCode==HttpStatus.SC_OK){
BufferedReader in=null;
StringBuffer响应=null;
试一试{
in=新的BufferedReader(新的InputStreamReader(con.getInputStream(),“UTF-8”);
字符串输入线;
响应=新的StringBuffer();
而((inputLine=in.readLine())!=null){
追加(inputLine);
}
}最后{
in.close();
}
responseString=response.toString();
}否则{
responseString=newStringBuilder(25).append(responseCode.toString();
}
回报率;
}

我的搭档只是想知道如何解决这个问题:

private static String sendPost(String url, Map<String, JSONObject> params) throws Exception {
    String responseString;

    StringBuilder urlParameters = new StringBuilder(400);
    if (params != null) {
        for (Entry<String, JSONObject> entry : params.entrySet()) {
            urlParameters.append(entry.getKey()).append("=").append(entry.getValue().toString()).append("&");
        }
    }

    url = url.replace(" ", "%20");
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("POST");
    con.setRequestProperty("accept-charset", "UTF-8");
    con.setRequestProperty("content-type", "application/x-www-form-urlencoded; charset=utf-8");
    con.setDoOutput(true);

    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8"));
    writer.write(urlParameters.toString());
    writer.close();
    wr.close();

    int responseCode = con.getResponseCode();
    if (responseCode == HttpStatus.SC_OK) {
        BufferedReader in = null;
        StringBuffer response = null;
        try{
            in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
            String inputLine;
            response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
        }finally{
            in.close();
        }
        responseString = response.toString();
    } else {
        responseString = new StringBuilder(25).append(responseCode).toString();
    }
    return responseString;
}
私有静态字符串sendPost(字符串url、映射参数)引发异常{
弦乐;
StringBuilder urlParameters=新的StringBuilder(400);
如果(参数!=null){
for(条目:params.entrySet()){
urlParameters.append(entry.getKey()).append(“=”).append(entry.getValue().toString()).append(“&”);
}
}
url=url.replace(“,“%20”);
URL obj=新URL(URL);
HttpURLConnection con=(HttpURLConnection)obj.openConnection();
con.setRequestMethod(“POST”);
con.setRequestProperty(“接受字符集”、“UTF-8”);
con.setRequestProperty(“内容类型”,“应用程序/x-www-form-urlencoded;字符集=utf-8”);
con.设置输出(真);
DataOutputStream wr=新的DataOutputStream(con.getOutputStream());
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(wr,“UTF-8”));
write(urlParameters.toString());
writer.close();
wr.close();
int responseCode=con.getResponseCode();
if(responseCode==HttpStatus.SC_OK){
BufferedReader in=null;
StringBuffer响应=null;
试一试{
in=新的BufferedReader(新的InputStreamReader(con.getInputStream(),“UTF-8”);
字符串输入线;
响应=新的StringBuffer();
而((inputLine=in.readLine())!=null