网络编程(4)——使用post方式提交数据和文件
2020-07-09 本文已影响0人
让时间走12138
本节内容
1.常规方式post普通数据
2.底层方式post普通数据
3.文件上传
一、常规方式post普通数据
1.首先创建一个URL对象
URL url= new URL("http://127.0.0.1/login.php");
2.获取链接对象
HttpURLConnection coon=(HttpURLConnection) url.openConnection();
coon.setRequestMethod("POST");
coon.setDoOutput(true);
coon.setConnectTimeout(5*1000);
3.拼接上传的普通数据
String params ="user_name=jack&user_password=123";
4.获取输出流对象,注意:使用DataOutputStream包裹字节输出流,DataOutputStream是一个平台无关类
OutputStream os= coon.getOutputStream();
DataOutputStream out=new DataOutputStream(os);
5.将自己的数据写出到输出流中
out.writeBytes(params);
out.flush();
out.close();
6.获取服务器的响应信息
InputStream is =coon.getInputStream();
DataInputStream in =new DataInputStream(is);
byte []buffer =new byte[1024];
int len= in.read(buffer);
String content= new String(buffer,0,len);
System.out.println(content);
二、底层方式POST普通数据(按照html上传的数据格式来上传)
//post底层方式提交普通数据
public static void postData2()throws Exception{
URL url= new URL("http://127.0.0.1/login.php");
HttpURLConnection coon =(HttpURLConnection)url.openConnection();
//设置请求方式为post
coon.setRequestMethod("POST");
//打开输出流
coon.setDoOutput(true);
//设置请求头部信息
coon.setRequestProperty("Content-type","multipart/form-data, boundary==AaB03x");
//发送请求体数据
OutputStream os = coon.getOutputStream();
DataOutputStream out = new DataOutputStream(os);
out.writeBytes("--AaB03x\r\n");
out.writeBytes("content-disposition: form-data; name=\"user_name\"\r\n");
out.writeBytes("\r\n");
out.writeBytes("jack\r\n");
out.writeBytes("--AaB03x\r\n");
out.writeBytes("content-disposition: form-data; name=\"user_password\"\r\n");
out.writeBytes("\r\n");
out.writeBytes("123\r\n");
out.writeBytes("--AaB03x--\r\n");
out.flush();
out.close();
//获取服务器的响应信息
InputStream is =coon.getInputStream();
DataInputStream in =new DataInputStream(is);
byte []buffer =new byte[1024];
int len= in.read(buffer);
String content= new String(buffer,0,len);
System.out.println(content);
}
三、文件上传
public static void postFile() throws Exception{
URL url= new URL("http://127.0.0.1/upload.php");
HttpURLConnection coon =(HttpURLConnection)url.openConnection();
coon.setRequestMethod("POST");
coon.setDoOutput(true);
coon.setRequestProperty("Content-type","multipart/form-data, boundary==AaB03x");
OutputStream os = coon.getOutputStream();
DataOutputStream out= new DataOutputStream(os);
out.writeBytes("--AaB03x\r\n");
out.writeBytes("content-disposition: form-data; name=\"file\";filename=\"abc.jpg\"\r\n");
out.writeBytes("Content-Type: image/jpg\r\n\r\n");
//图片的具体内容
//创建文件的输入流对象
FileInputStream fis = new FileInputStream("C:\\Users\\86178\\Pictures\\Saved Pictures\\1.jpg");
BufferedInputStream bis=new BufferedInputStream(fis);
byte [] buffer =new byte[1024];
int len =0;
while((len=bis.read(buffer))!=-1){
out.write(buffer,0,len);
}
out.writeBytes("\r\n");
out.writeBytes("--AaB03x--\r\n");
out.flush();
out.close();
bis.close();
//获取服务器的响应信息
InputStream is =coon.getInputStream();
DataInputStream in =new DataInputStream(is);
len= in.read(buffer);
String content= new String(buffer,0,len);
System.out.println(content);
}