package vikipost;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
/**
*
* @author ideacall
*/
public class VikiPost {
public static String FilePost(String url,File value) throws Exception {
String BOUNDARY = java.util.UUID.randomUUID().toString();
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------"+BOUNDARY, Charset.defaultCharset());
multipartEntity.addPart("company_code",new StringBody("wh", Charset.forName("UTF-8")));
multipartEntity.addPart("username",new StringBody("admin",Charset.forName("UTF-8")));
multipartEntity.addPart("password",new StringBody("wh",Charset.forName("UTF-8")));
multipartEntity.addPart("voiceFile", new FileBody(value));
HttpPost request = new HttpPost(url);
request.setEntity(multipartEntity);
request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------"+BOUNDARY);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
InputStream is = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
System.out.println("发送消息收到的返回:"+buffer.toString());
return buffer.toString();
}
public static void main(String[] args) throws Exception {
File file=new File("/Users/ideacall/Desktop/1/z/viki-wav-1.wav");
String Result=FilePost("http://xxxx.wei-fu.cn/webapi/uploadwav.php",file);
}
}