a
554325746@qq.com
2020-01-10 ce0902f1721b9de6c0a2e8b16cdb6be2c6bca2b3
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.basic.security.utils;
 
import org.apache.commons.io.FileUtils;
 
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class MultipartUtilityV2 {
    private final String boundary = "*****";
    private final String crlf = "\r\n";
    private final String twoHyphens = "--";
    private HttpURLConnection httpConn;
    private DataOutputStream request;
 
    public MultipartUtilityV2(String requestURL)
            throws IOException {
        URL url = new URL(requestURL);
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Connection", "Keep-Alive");
        httpConn.setRequestProperty("Cache-Control", "no-cache");
        httpConn.setRequestProperty(
                "Content-Type", "multipart/form-data;boundary=" + this.boundary);
        request = new DataOutputStream(httpConn.getOutputStream());
    }
 
    public static void uploadCrashDirectory() {
        try {
            File[] crashFiles = new File("/sdcard/crash/").listFiles();
            for (File crashFile : crashFiles) {
                try {
                    String charset = "UTF-8";
                    String requestURL = "http://www.aiotlink.com:8080/UploadServlet";
                    MultipartUtilityV2 multipart = new MultipartUtilityV2(requestURL);
                    multipart.addFilePart("file", crashFile, crashFile.getName() + RUtils.applicationID + System.currentTimeMillis() + ".txt");
                    String response = multipart.finish();
                } catch (Exception e) {
                    ExceptionUtil.printException(e);
                }
            }
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
    }
 
    public static void main(String[] args) {
        try {
            String charset = "UTF-8";
            String requestURL = "http://www.aiotlink.com:8080/UploadServlet";
            MultipartUtilityV2 multipart = new MultipartUtilityV2(requestURL);
            multipart.addFilePart("file", new File("d:\\c.gif"), new File("d:\\c.gif").getName() + "Security_simplify" + System.currentTimeMillis() + ".txt");
            String response = multipart.finish();
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
    }
 
    public static void writeAndUpload(String message) {
        try {
            File file = new File("/sdcard/crash/Security_simplify" + System.currentTimeMillis() + "txt");
            FileUtils.write(file, message);
            uploadCrashDirectory();
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
    }
 
    public void addFormField(String name, String value) throws IOException {
        request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
        request.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"" + this.crlf);
        request.writeBytes("Content-Type: text/plain; charset=UTF-8" + this.crlf);
        request.writeBytes(this.crlf);
        request.writeBytes(value + this.crlf);
        request.flush();
    }
 
    public void addFilePart(String fieldName, File uploadFile, String fileName)
            throws IOException {
        if (fieldName == null || "".equals(fieldName)) {
            fileName = uploadFile.getName();
        }
        request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
        request.writeBytes("Content-Disposition: form-data; name=\"" +
                fieldName + "\";filename=\"" +
                fileName + "\"" + this.crlf);
        request.writeBytes(this.crlf);
        byte[] bytes = FileUtils.readFileToByteArray(uploadFile);
        request.write(bytes);
    }
 
    public void addFilePart(String fieldName, File uploadFile)
            throws IOException {
        String fileName = "";
        if (fieldName == null || "".equals(fieldName)) {
            fileName = uploadFile.getName();
        }
        request.writeBytes(this.twoHyphens + this.boundary + this.crlf);
        request.writeBytes("Content-Disposition: form-data; name=\"" +
                fieldName + "\";filename=\"" +
                fileName + "\"" + this.crlf);
        request.writeBytes(this.crlf);
        byte[] bytes = FileUtils.readFileToByteArray(uploadFile);
        request.write(bytes);
    }
 
    public String finish() throws IOException {
        String response = "";
        request.writeBytes(this.crlf);
        request.writeBytes(this.twoHyphens + this.boundary +
                this.twoHyphens + this.crlf);
        request.flush();
        request.close();
        int status = httpConn.getResponseCode();
        if (status == HttpURLConnection.HTTP_OK || 1 == 1) {
            InputStream responseStream = new
                    BufferedInputStream(httpConn.getInputStream());
            BufferedReader responseStreamReader =
                    new BufferedReader(new InputStreamReader(responseStream));
            String line = "";
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = responseStreamReader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }
            responseStreamReader.close();
            response = stringBuilder.toString();
            httpConn.disconnect();
        } else {
            throw new IOException("Server returned non-OK status: " + status);
        }
        return response;
    }
}