使用HttpURLConnection向CGI(ASP/PHP/JSP)发送文件
//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();
页:
[1]