2016-06-25 4 views
0

問題があります。私はPOST要求を介してサーバに送信しようString文字列のエンコーディングを変更するにはどうすればよいですか?

String str="Привет"; 

を持っています。この文字列を受け取るウェブサイトには、windows-1251文字セット(サーバー上のすべてのファイル、データ、テキストなどにwindows-1251があります)があります。Javaアプリケーションから受信したロシア語のシンボルを除いて、私はこの問題をGoogleのJavaアプリケーションで文字列のエンコーディングを変更することができますが、私はまだ解決策を持っていませんでした。

私はこの

String str=new String("Привет".getBytes("utf-8"),"cp1251"); 

を実行しようとしましたが、それは助けにはなりませんでした。

は、そこで質問です: cp1251エンコーディングを受け入れるWebサーバが正常に私から受信したすべてのデータを示しているように、どのように私は cp1251にJavaの utf-8文字列を変換します。

これは私がリクエストを送信する方法である:

protected Map<String, String> getFromUrl(String url, boolean post, Map<String,String> postParams, Map<String,String> files, boolean options, String fbt, String asFile){ 
    dataStream=null; 
    bodyStream=null; 
    url=url.replaceAll("(?iu)^(http(s?)[://]*)+", "http$2://").replaceAll("(\\+|\\s+)", "%20"); 
    if(url.matches("(?iu)^https://.*$"))return getFromUrlSSL(url,post,postParams,files,options,fbt,asFile); 
    else if(!url.matches("(?iu)^((http(s?))|ftp)://.*$"))url="http://"+url; 
    if(url.length()>2048)url=url.substring(0,2048); 
    System.out.println("/*Trying to connect to "+url+"*/"); 
    Map<String, String> mp = new HashMap<String, String>(); 
    String host = this.getHostName(url), content = "", UA = this.getUA(), cookie = this.getCookie(host, UA), referer = "http://"+host+"/"; 
    try{ 
     mp.put("User-Agent",UA); 
     mp.put("cookies",cookie); 
     URL U = new URL(url); 
     HttpURLConnection conn = (HttpURLConnection)U.openConnection(); 
     conn.setInstanceFollowRedirects(false); 
     conn.setRequestProperty("Host", host); 
     conn.setRequestProperty("User-Agent", UA); 
     conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
     conn.setRequestProperty("Accept-Language", "ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3"); 
     conn.setRequestProperty("Accept-Encoding", "gzip,deflate"); 
     conn.setRequestProperty("Accept-Charset", "utf-8;q=0.7,*;q=0.7"); 
     conn.setRequestProperty("Keep-Alive", "115"); 
     conn.setRequestProperty("Connection", "keep-alive"); 
     if(arp!=null)for(Map.Entry<String, String> entry : arp.entrySet())conn.setRequestProperty(entry.getKey(),entry.getValue()); 
     if(referer != null&&(arp==null||!arp.containsKey("Referer")))conn.setRequestProperty("Referer", referer); 
     if(cookie != null && !cookie.contentEquals(""))conn.setRequestProperty("Cookie", cookie); 
     if(post&&(postParams!=null&&postParams.size()>0||files!=null&&files.size()>0)){ 
      if(!options)conn.setRequestMethod("POST"); 
      else conn.setRequestMethod("OPTIONS"); 
      conn.setDoOutput(true); 
      OutputStream os; 
      BufferedWriter writer; 
      if(files!=null&&files.size()>0){ 
       String boundary = Long.toHexString(System.currentTimeMillis()); 
       conn.setRequestProperty("Content-Type","multipart/form-data; boundary="+boundary); 

       os=conn.getOutputStream(); 
       writer=new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));; 
       if(postParams!=null&&postParams.size()>0){ 
        Iterator it=postParams.entrySet().iterator(); 
        while(it.hasNext()){ 
         Map.Entry<String,String> pair=(Map.Entry<String, String>)it.next(); 
         writer.append("--" + boundary).append("\r\n"); 
         writer.append("Content-Disposition: form-data; name=\""+pair.getKey()+"\"").append("\r\n"); 
         writer.append("\r\n"); 
         writer.append(pair.getValue()).append("\r\n"); 
        } 
       } 

       Iterator fIt=files.entrySet().iterator(); 
       while(fIt.hasNext())try{ 
        Map.Entry<String, String> fPair=(Map.Entry<String, String>)fIt.next(); 
        String filename=fPair.getValue(); 
        System.out.println("/*"+filename+"*/"); 
        if(!filename.matches("(?iu)^[\\s\\S]*?Content-Disposition. form-data[\\s\\S]*$")){ 
         File file=null; 
         System.out.println("/*"+filename+"*/"); 
         String fe=filename.matches("(?iu)^.*?(\\.[a-z0-9]{1,5})$")?filename.replaceAll("(?iu)^.*?(\\.[a-z0-9]{1,5})$","$1"):".jpg"; 
         if(filename.matches("(?iu)^(http|ftp).*$")){ 
          getFileFromUrl(filename,System.getProperty("user.dir")+"\\curFile"+fe); 
          file = new File(System.getProperty("user.dir")+"\\curFile"+fe); 
         } 
         else file = new File(filename); 
         try{ 
          BufferedImage bimg = ImageIO.read(file); 
          int width = bimg.getWidth(); 
          int height = bimg.getHeight(); 
          if(width<150||height<150)file=null; 
          else if(width/height>3||height/width>3)file=null; 
          else if(width>1024||height>1024){ 
           float ii=width/height,w=ii>0?1024:height/width*1024,h=ii>0?width/height*1024:1024; 
           int type = bimg.getType() == 0? BufferedImage.TYPE_INT_ARGB : bimg.getType(); 
           BufferedImage img = resizeImage(bimg,type,(int)w,(int)h); 
           ImageIO.write(img, "jpg", file); 
           if(filename.matches("(?iu)^(http|ftp).*$"))file = new File(System.getProperty("user.dir")+"\\curFile"+fe); 
           else new File(filename); 
          } 
         } 
         catch(Exception e){ 
          Error.showError("Browser.getFromUrl() can't get data from url",e); 
          return new HashMap<String,String>(){{put("error","fileerror");}}; 
         } 
         if(file!=null){ 
          writer.append("--" + boundary).append("\r\n"); 
          writer.append("Content-Disposition: form-data; name=\""+fPair.getKey()+"\"; filename=\""+file.getName()+"\"").append("\r\n"); 
          writer.append("Content-Type: application/octet-stream").append("\r\n"); 
          writer.append("\r\n").flush(); 
          InputStream input = new FileInputStream(file); 
          //System.out.println(conn.getOutputStream()); 
          try{ 
           byte[] buffer = new byte[1024]; 
           for(int length = 0;(length = input.read(buffer))>0;)os.write(buffer, 0, length); 
           os.flush(); 
          } 
          finally{try{input.close();}catch (IOException logOrIgnore){}} 
          writer.append("\r\n").flush(); 
         } 
        } 
       } 
       catch(Exception e){ 
        Error.showError("Browser.getFromUrl() can't get data from url",e); 
        return new HashMap<String,String>(){{put("error","fileerror");}}; 
       } 
       writer.append("--" + boundary + "--").append("\r\n"); 
      } 
      else{ 
       String params=""; 
       Iterator it=postParams.entrySet().iterator(); 
       while(it.hasNext()){ 
        Map.Entry<String,String> pair=(Map.Entry<String, String>)it.next(); 
        //System.out.println(pair.getKey()+"="+pair.getValue()); 
        /*if(pair.getKey().trim().contentEquals("")&&arp.get("Content-Type")!=null&&arp.get("Content-Type").matches("(?iu)^[\\s\\S]*?application/json[\\s\\S]$"))params+=pair.getValue(); 
        else */ 
        params+=URLEncoder.encode(pair.getKey(),"UTF-8")+"="+URLEncoder.encode(pair.getValue(),"UTF-8")+"&"; 
       } 
       params=params.replaceAll("&+$",""); 
       if(fbt!=null)conn.setRequestProperty("Content-Type",fbt); 
       os=conn.getOutputStream(); 
       writer=new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 
       if(fbt!=null){ 
        writer.append("Content-Type: "+fbt).append("\r\n"); 
        writer.append("Content-Length: "+params.length()).append("\r\n").append("\r\n"); 
        if(fbt.matches("(?iu)^.*boundary=.*$")){ 
         String boundary=fbt.replaceAll("(?iu)^[\\s\\S]*?boundary=([^;]+)[\\s\\S]*$","$1"); 
         writer.append(boundary).append("\r\n"); 
         it=postParams.entrySet().iterator(); 
         //System.out.println(boundary);System.exit(1); 
         while(it.hasNext()){ 
          Map.Entry<String,String> pair=(Map.Entry<String, String>)it.next(); 
          writer.append("Content-Disposition: form-data; name=\""+pair.getKey()+"\"").append("\r\n\r\n"); 
          writer.append(pair.getValue()).append("\r\n"); 
          writer.append(boundary).append("\r\n"); 
         } 
        } 
        else writer.append(params.replaceAll("(^[=&\\s])|([\\s=&]$)","")); 
       } 
       else writer.write(params.replaceAll("(^[=&\\s])|([\\s=&]$)","")); 
      } 
      writer.flush(); 
      writer.close(); 
      os.close(); 
      //System.out.println(conn.getOutputStream()); 
      conn.setConnectTimeout(120000); 
      conn.setReadTimeout(120000); 
     } 
     else{ 
      conn.setConnectTimeout(7000); 
      conn.setReadTimeout(7000); 
     } 
     conn.connect(); 
     //System.out.println(mp.get("cookies")); 
     mp = processResponseHeaders(conn.getHeaderFields()); 
     //System.out.println(mp.get("cookies")); 
     if(mp.get("cookies")!=null && !mp.get("cookies").trim().contentEquals("") && !mp.get("cookies").contentEquals(cookie))this.setCookie(host, UA, cookie+"; "+mp.get("cookies")); 
     if((needRedirect!=null&&!needRedirect.contentEquals("")) && curRedirect<5){ 
      if(!needRedirect.matches("(?i)^(.{3,5}?://)[\\s\\S]*$"))needRedirect = needRedirect.replaceAll("^([^\\.]*?://)?[^/\\?\\=\\&]*([/\\?\\=\\&])", url.replaceAll("^((http[s]?|ftp)://([^/\\?\\=\\&]*))[\\s\\S]*$","$1")+"$2"); 
      return getFromUrl(needRedirect,false,null,null,false,null,asFile); 
     } 
     try{ 
      dataStream = conn.getErrorStream() != null ? conn.getErrorStream() : conn.getInputStream(); 
      bodyStream = mp.get("content-encoding")!=null && mp.get("content-encoding").equalsIgnoreCase("gzip") ? new BufferedInputStream(new GZIPInputStream(dataStream)) : new BufferedInputStream(dataStream); 
      if(asFile!=null&&!asFile.trim().contentEquals(""))this.getFileFromUrlWIS(bodyStream,asFile); 
      else content = parseByteData(readToByteBuffer(bodyStream, 1024 * 1024), getCharsetFromContentType(conn.getContentType())); 
     } 
     catch(Exception e){Error.showError("Browser.getFromUrl() can't get data from url",e);} 
     finally{ 
      if(bodyStream != null)bodyStream.close(); 
      if(dataStream != null)dataStream.close(); 
     } 
     conn.disconnect(); 
    } 
    catch(Exception e){Error.showError("Browser.getFromUrl() can't get data from url",e);} 
    mp.put("url", url); 
    mp.put("content", content.replaceAll("&nbsp;", " ")/*.replaceAll("<!--[\\s\\S]*?-->", "")*/.replaceAll("[\r\n\t]","").replaceAll("\\s+", " ")); 
    curRedirect=0; 
    return mp; 
} 
+0

http://stackoverflow.com/questions/5729806/encode-string-to-utf-8 – lulyon

+0

を参照してください。この投稿を送信すると、サーブレット、またはjavascript /クエリコードをリクエストできます。 –

答えて

0

文字列は、JavaでUTF-16です。バイトは他のエンコーディングにあります。バイトはネットワーク経由で送信されます。文字列はそうではありません。ネットワークにバイトを送信するときは、CharsetEncoderを使用して、必要なエンコーディングのバイトを取得し、それらをネットワーク経由で送信します。実際にネットワーク経由でデータを送信するコードは表示されていないため、正確ではありません。

関連する問題