您的位置 首页 java

Http post 请求报文格式分析与Java实现文件上传

POST报文格式

这里咱们提交的是userId、nickName和一张图片

post报文格式分析

#请求的请求方式 请求的子路径 HTTP协议的版本号

POST /api/feed/ HTTP/1.1

###########请求头信息##########

#服务器返回的数据须要使用gzip压缩

Accept-Encoding: gzip

#请求的内容长度为4579876

Content-Length: 4579876

#内容的类型为”multipart/form-data”,请求参数分隔符(boundary)为——-3cb6q348891v0

Content-Type: multipart/form-data; boundary=——-3cb6q348891v0

#请求的根域名

Host: www.myhost.com

#HTTP连接方式为持久链接,Keep-Alive功能使客户端到服务器端的链接持续有效,当出现对服务器的后续请求时,Keep-Alive功能避免了创建或者从新创建链接

Connection: Keep-Alive

#\r–回车符(return),回到一行的开头,\n–换行符(newline),另起一行

\r\n

#这样的格式:– + boundary(boundary用于做为请求参数之间的界限标识),无论boundary自己有没有这个”–“,开头”–“不能省略

———3cb6q348891v0

Content-Disposition: form-data; name=”userId”

Content-Type: text/plain; charset=UTF-8

Content-Transfer-Encoding: 8bit

\r\n

623bd5345abaf75e9a84939a

\r\n

\r\n

———3cb6q348891v0

Content-Disposition: form-data; name=”images”; filename=”/Users/xiawenquan/Desktop/ip数据报文.png”

Content-Type: application/octet-stream

这里是图片的 二进制 数据

———3cb6q348891v0–

参数实体的最后一行是: –加上boundary加上–,最后换行

—-使用纯 java 模拟post文件上传的报文——

 public  static   void  main(String[] args) {

        String filePath="/Users/xiawenquan/Desktop/ip数据报文.png";
        String unid="623bd5345abaf75e9a84939a";
        DataOutputStream out = null;
        final String newLine = "\r\n";
        final String prefix = "--";
        try {
            URL url = new URL("#34;);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();

            String BOUNDARY = "------3cb6q348891v0";
            conn.set Request Method("POST");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Charsert", " UTF-8 ");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

            out = new DataOutputStream(conn.getOutputStream());

            // 添加参数file
            File file = new File(filePath);
             StringBuilder  sb1 = new StringBuilder();
            sb1.append(prefix);
            sb1.append(BOUNDARY);
            sb1.append(newLine);
            sb1.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"" + newLine);
            sb1.append("Content-Type:application/octet-stream");
            sb1.append(newLine);
            sb1.append(newLine);
            out.write(sb1.toString().get byte s());
            DataInputStream in = new DataInputStream(new FileInputStream(file));
            byte[] bufferOut = new byte[1024];
            int bytes = 0;
            while (( bytes  = in.read(bufferOut)) != -1) {
                out.write(bufferOut, 0, bytes);
            }
            out.write(newLine.getBytes());
            in.close();

            // 添加参数userId
            StringBuilder sb = new StringBuilder();
            sb.append(prefix);
            sb.append(BOUNDARY);
            sb.append(newLine);
            sb.append("Content-Disposition: form-data;name=\"userId\"");
            sb.append(newLine);
            sb.append(newLine);
            sb.append("623bd5345abaf75e9a84939a");
            out.write(sb.toString().getBytes());

            // 添加参数nickName
            StringBuilder sb = new StringBuilder();
            sb.append(prefix);
            sb.append(BOUNDARY);
            sb.append(newLine);
            sb.append("Content-Disposition: form-data;name=\"nickName\"");
            sb.append(newLine);
            sb.append(newLine);
            sb.append("ddedhouqnickname");
            out.write(sb.toString().getBytes());


            byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
            // 写上结尾标识
            out.write(end_data);
            out.flush();
            out.close();

            // 定义BufferedReader输入流来读取URL的响应
            BufferedReader reader = new BufferedReader(new  InputStream Reader(conn.getInputStream()));
             String  line = null;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

        } catch ( Exception  e) {
            System.out.println("发送POST请求出现异常!" + e);
            e.printStackTrace();
        }
    }
  

文章来源:智云一二三科技

文章标题:Http post 请求报文格式分析与Java实现文件上传

文章地址:https://www.zhihuclub.com/174629.shtml

关于作者: 智云科技

热门文章

网站地图