2013-10-30 28 views
1

我有以下代碼:建立一個POST請求希望希伯來數據

import java.io.File; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URI; 
import java.net.URISyntaxException; 
import java.net.URLDecoder; 
import java.net.URLEncoder; 
import java.nio.charset.Charset; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.cache.HeaderConstants; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.ContentType; 
import org.apache.http.entity.mime.MultipartEntity; 
import org.apache.http.entity.mime.MultipartEntityBuilder; 
import org.apache.http.entity.mime.content.FileBody; 
import org.apache.http.entity.mime.content.StringBody; 
import org.apache.http.impl.client.HttpClientBuilder; 


public class MyMain { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    // TODO Auto-generated method stub 

    File file1 = new File("C:\\Users\\Avner.MARKETOLOGY\\Desktop\\a.jpg"); 

    URI uri; 
    try { 
     uri = new URI("http://www.amaya365.com/kinderPhp/uploadFiles.php"); 
     //uri = new URI("http://www.amaya365.com/kinderPhp/uploadFiles.php"); 
     HttpClient client = HttpClientBuilder.create().build(); 
     HttpPost post = new HttpPost(uri); 
     post.getEntity(); 


     // Http Headers 
     //post.addHeader("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"); 
     post.addHeader("Connection", "keep-alive"); 

     //Charset chars = Charset.forName("UTF-8"); 

     MultipartEntityBuilder multiEntity = MultipartEntityBuilder.create(); 
     multiEntity.addPart("file", new FileBody(file1)); 
     //multiEntity.addPart("file2", new FileBody(file2)); 

     multiEntity.addTextBody("classid", "55555");   
     multiEntity.addTextBody("albumid", "111"); 


     //multiEntity.addBinaryBody("classid",ptext); 

     // Credentials 
     /*multiEntity.addPart("username", new StringBody(ServerData.username)); 
     multiEntity.addPart("password", new StringBody(ServerData.password));*/ 


     HttpEntity httpEntity = multiEntity.build(); 

     post.setEntity(httpEntity); 

     HttpResponse response = client.execute(post); 
     //HttpEntity entity = response.getEntity(); 

     //if (entity != null) { 
      InputStream is = response.getEntity().getContent(); 
      /*int i = (int)response.getEntity().getContentLength(); 
      System.out.println(i);*/ 
      byte[] b = new byte[is.available()]; 

      String str = null; 
      while(is.read(b) > -1){ 
       str += new String(b); 

      } 
      System.out.println(str); 
      int statusCode = response.getStatusLine().getStatusCode(); 
      if (statusCode == 200) { 

       System.out.println("upload completed succefully "+statusCode); 
      }else{ 
       //try again 
      } 
     //} 



    } catch (URISyntaxException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 





} 

}

只要ALBUMID和的classid設置爲數字或英文它工作正常。 當我使用希伯來租船人我越來越????在發佈請求。

我該怎麼做才能發送希伯來字符通過發佈請求?

thx, Avner。

+0

不僅希伯來語,而且日本人都是亂碼。在我的情況下,我使用addTextBody(),它也崩潰日文字符。 – HirofumiTamori

回答

0

當使用addTextBody函數時,您應該使用ContentType參數。