import java.io.BufferedOutputStream; import java.net.Socket; public class UrlClient { public static void main(String[] args) throws Exception { Socket s = null; BufferedOutputStream bis = null;
String ip = "127.0.0.1"; //对方web 服务器的ip int port = 80; //对方web 服务器的端口号,默认为:80 String url = "/test/UploadServlet"; try { s = new Socket( ip, port); bis = new BufferedOutputStream(s.getOutputStream()); StringBuffer data = new StringBuffer(); StringBuffer strBuf = new StringBuffer(); //http 协议的信息 strBuf.append("POST "+url+" HTTP/1.1\r\n"); strBuf.append("Host: "+ip+":"+port+"\r\n"); strBuf.append("Connection: close\r\n"); strBuf.append("Content-type: multipart/form-data \r\n"); //要传输的数据 data.append("user_id 007\r\n"); data.append("ring_name 童话\r\n"); data.append("singer_name 作者\r\n"); strBuf.append("Content-length: " + data.toString().getBytes().length + "\r\n\r\n"); //数据的长度 bis.write(strBuf.toString().getBytes()); bis.write(data.toString().getBytes()); bis.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (bis != null) bis.close(); } catch (Exception e) { } try { if (s != null) s.close(); } catch (Exception e) { } bis = null; s = null; } }} 这是一个简单示例程序,希望能给搂主一点启示,小数据量这个程序应该可以,大数据量可能需要做一些其他处理。
con.setDoOutput(true);
con.setDoInput(true);
con.setDefaultUseCaches(false);
con.setUseCaches(false);
con.setRequestProperty("Content-Type", "multipart/form-data");
con.setRequestMethod("POST");这个已经设置好了,可是我怎么把user_id 集团编号
ring_name 铃声名
singer_name 作者
ring_id 铃声编号 0000XXXXXX
upload_fee上传费用
ring_fee铃声费用添 0
is_pub是否在网上公开0不公开,1公开
Action填写AddNew
up_ring_file 上传文件的数这几个变量已post的方式提交过去呢?在HttpURLConnection 里面是那个方法?怎么用?
import java.net.Socket;
public class UrlClient { public static void main(String[] args) throws Exception { Socket s = null;
BufferedOutputStream bis = null;
String ip = "127.0.0.1"; //对方web 服务器的ip
int port = 80; //对方web 服务器的端口号,默认为:80
String url = "/test/UploadServlet"; try { s = new Socket( ip, port);
bis = new BufferedOutputStream(s.getOutputStream()); StringBuffer data = new StringBuffer();
StringBuffer strBuf = new StringBuffer();
//http 协议的信息
strBuf.append("POST "+url+" HTTP/1.1\r\n");
strBuf.append("Host: "+ip+":"+port+"\r\n");
strBuf.append("Connection: close\r\n");
strBuf.append("Content-type: multipart/form-data \r\n");
//要传输的数据
data.append("user_id 007\r\n");
data.append("ring_name 童话\r\n");
data.append("singer_name 作者\r\n"); strBuf.append("Content-length: "
+ data.toString().getBytes().length + "\r\n\r\n"); //数据的长度 bis.write(strBuf.toString().getBytes());
bis.write(data.toString().getBytes());
bis.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null)
bis.close();
} catch (Exception e) {
}
try {
if (s != null)
s.close();
} catch (Exception e) {
} bis = null;
s = null; } }}
这是一个简单示例程序,希望能给搂主一点启示,小数据量这个程序应该可以,大数据量可能需要做一些其他处理。