六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 65|回复: 0

使用HttpURLConnection向CGI(ASP/PHP/JSP)发送文件

[复制链接]

升级  81.33%

48

主题

48

主题

48

主题

秀才

Rank: 2

积分
172
 楼主| 发表于 2013-1-15 03:02:31 | 显示全部楼层 |阅读模式
//send to phpURL url = new URL("http://localhost:1108/testupload/upload2.php");HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("POST");conn.setDoOutput(true);String BOUNDARY = "---------------------------7d4a6d158c9"; // 分隔符//DataOutputStream dos = new DataOutputStream(conn.getOutputStream());StringBuffer sb = new StringBuffer();sb.append("--");sb.append(BOUNDARY);sb.append("\r\n");sb.append("Content-Disposition: form-data; name=\"pic\"; filename=\"gggg.wav\"\r\n");sb.append("Content-Type: application/octet-stream\r\n\r\n");byte[] data = sb.toString().getBytes();byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();conn.setRequestProperty("Content-Type", "multipart/form-data; boundary="+BOUNDARY);conn.setRequestProperty("Content-Length", String.valueOf(data.length + CopyOfJDKAudioRecorder.buf.length + end_data.length));OutputStream os = conn.getOutputStream();os.write(data);os.write(CopyOfJDKAudioRecorder.buf);os.write(end_data);os.flush();os.close();
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表