1. package vikipost;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.nio.charset.Charset;
  9.  
  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.client.ClientProtocolException;
  12. import org.apache.http.client.methods.HttpPost;
  13. import org.apache.http.entity.mime.HttpMultipartMode;
  14. import org.apache.http.entity.mime.MultipartEntity;
  15. import org.apache.http.entity.mime.content.FileBody;
  16. import org.apache.http.entity.mime.content.StringBody;
  17. import org.apache.http.impl.client.DefaultHttpClient;
  18.  
  19. /**
  20.  *
  21.  * @author ideacall
  22.  */
  23. public class VikiPost {
  24.     
  25.     public static String FilePost(String url,File value) throws  Exception {
  26.     
  27.         String BOUNDARY = java.util.UUID.randomUUID().toString();
  28.         MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------"+BOUNDARY, Charset.defaultCharset());
  29.         multipartEntity.addPart("company_code",new StringBody("wh", Charset.forName("UTF-8"))); 
  30.         multipartEntity.addPart("username",new StringBody("admin",Charset.forName("UTF-8"))); 
  31.         multipartEntity.addPart("password",new StringBody("wh",Charset.forName("UTF-8")));         
  32.         multipartEntity.addPart("voiceFile", new FileBody(value));
  33.         HttpPost request = new HttpPost(url);
  34.         request.setEntity(multipartEntity);
  35.         request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------"+BOUNDARY);
  36.  
  37.         DefaultHttpClient httpClient = new DefaultHttpClient();
  38.         HttpResponse response = httpClient.execute(request);
  39.         InputStream is = response.getEntity().getContent();
  40.         BufferedReader in = new BufferedReader(new InputStreamReader(is));
  41.         StringBuffer buffer = new StringBuffer();
  42.         String line = "";
  43.         while ((line = in.readLine()) != null) {
  44.             buffer.append(line);
  45.         }
  46.         
  47.         System.out.println("发送消息收到的返回:"+buffer.toString());
  48.         
  49.         return buffer.toString();
  50.     }
  51.  
  52.     public static void main(String[] args) throws Exception {
  53.  
  54.         File file=new File("/Users/ideacall/Desktop/1/z/viki-wav-1.wav");       
  55.         String Result=FilePost("http://xxxx.wei-fu.cn/webapi/uploadwav.php",file);
  56.       
  57.     }
  58.     
  59. }