首页 博客 娱乐 图库 新闻 体育 视频

JAVA 客户端POST提交wav文件到PHP服务器

package vikipost; import knowledge.daletripp.com import knowledge.daletripp.com import knowledge.daletripp.com import knowledge.daletripp.com import knowledge.daletripp.com 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://knowledge.daletripp.com/webapi/uploadwav.php",file); } }


除非特别注明,本站所有文字均为原创文章,作者:luojie

No Comment

留言

电子邮件地址不会被公开。 必填项已用*标注

感谢你的留言。。。