我有以下代码:
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 设置为数字或英语,它就可以正常工作。当我使用希伯来语租船人时,我得到了????在发布请求上。
我应该怎么做才能通过发布请求发送希伯来语字符?
谢谢,阿夫纳。