zhangzengfei
2022-01-10 4496b59ab27d569df1da7ef634e02273b3a9618a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.basic.security.utils;
 
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
 
public class PostData {
 
    static final String BOUNDARY = "3C3F786D6C2076657273696F6E2E302220656E636F64696E673D662D38223F3E0A3C6D616E6966";
    static final String CRLF = "\r\n";
    private static final String TAG = PostData.class.getSimpleName();
    private final String encoding;
    private StringBuilder sb;
    private String trailer;
    private List<ByteData> dataList = new ArrayList<ByteData>();
 
    public PostData() {
        this("UTF-8");
    }
 
 
    public PostData(String encoding) {
        this.encoding = encoding;
        sb = new StringBuilder();
        trailer = "--" + BOUNDARY + "--" + CRLF;
    }
 
    public String getContentType() {
        return "multipart/form-data; boundary=" + BOUNDARY;
    }
 
    public void addValue(String name, int value) {
        addValue(name, Integer.toString(value));
    }
 
    public void addValue(String name, String value) {
        sb.append("--" + BOUNDARY + CRLF);
        sb.append("Content-Disposition: form-data; name=\"");
        try {
            sb.append(URLEncoder.encode(name, encoding));
            sb.append('"');
            sb.append(CRLF + CRLF);
            sb.append(value);
            sb.append(CRLF);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
 
    public void addData(String name, String contentType, byte[] data) {
        dataList.add(new ByteData(name, contentType, data));
    }
 
    public long getLength() {
        long length = sb.toString().getBytes().length;
        length += trailer.length();
        for (ByteData byteData : dataList)
            length += byteData.getSize();
        return length;
    }
 
    public String toString() {
        return sb.toString();
    }
 
    public void write(OutputStream out) throws IOException {
        out.write(sb.toString().getBytes());
        for (ByteData byteData : dataList)
            byteData.write(out);
        out.write(trailer.getBytes());
    }
 
    class ByteData {
        byte[] data;
        String header;
        String name;
 
        ByteData(String name, String contentType, byte[] data) {
            this.name = name;
            this.data = data;
            try {
                header = "--" + BOUNDARY + CRLF + "Content-Disposition: form-data; name=\"surveillancePhoto\"; filename=\"" + URLEncoder.encode(name, encoding) + "\";" + CRLF +
                        "Content-Type: " + contentType + CRLF + CRLF;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
 
        public int getSize() {
            return header.length() + data.length + CRLF.length();
        }
 
 
        public void write(OutputStream out) throws IOException {
            out.write(header.getBytes());
            out.write(data);
            out.write(CRLF.getBytes());
        }
    }
}