a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
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
package com.basic.security.utils;
 
import com.google.gson.Gson;
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
 
public class WeedUpload {
    static Gson gson = new Gson();
 
    public static String upload(File camera_image_path_file) {
        String pictureUrl = "";
        Map assignMap = null;
        try {
            HttpURLConnection connection = null;
            String urlParameters = "";
            try {
                //Create connection
//                URL url = new URL("http://123.207.143.213:9333/dir/assign");
                URL url = new URL("http://192.168.222.1:9333/dir/assign");
                connection = (HttpURLConnection) url.openConnection();
                connection.setConnectTimeout(2 * 1000);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type",
                        "application/x-www-form-urlencoded");
                connection.setRequestProperty("Content-Length",
                        Integer.toString(urlParameters.getBytes().length));
                connection.setRequestProperty("Content-Language", "en-US");
                connection.setUseCaches(false);
                connection.setDoOutput(true);
                //Send request
                DataOutputStream wr = new DataOutputStream(
                        connection.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.close();
                //Get Response
                InputStream is = connection.getInputStream();
//                String responseStr = IOUtils.toString(is);
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
                String line;
                while ((line = rd.readLine()) != null) {
                    response.append(line);
                    response.append('\r');
                }
                rd.close();
                String responseStr = response.toString();
                is.close();
//                System.out.println("WeedUpload.upload responseStr=" + responseStr);
                assignMap = gson.fromJson(responseStr, Map.class);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (assignMap != null) {
            try {
                HttpURLConnection connection = null;
                String urlParameters = "";
                try {
                    //WeedUpload.upload {"fid":"1,0206882d73","url":"127.0.0.1:8081","publicUrl":"127.0.0.1:8081","count":1}
                    //Create connection
//                    URL url = new URL("http://123.207.143.213:8081/"+assignMap.get("fid"));
                    String charset = "UTF-8";
//                    String requestURL = "http://123.207.143.213:8081/" + assignMap.get("fid");
                    String requestURL = "http://192.168.222.1:8081/" + assignMap.get("fid");
                    pictureUrl = requestURL;
//                    System.out.println("WeedUpload.upload " + requestURL + " " + FrameUtil.getFrames());
                    MultipartUtilityV2 multipart = new MultipartUtilityV2(requestURL);
                    multipart.addFilePart("file", camera_image_path_file);
                    String response = multipart.finish(); // response from server.
//                    System.out.println(response);
                    return requestURL;
                } catch (Exception e) {
                    e.printStackTrace();
                    return pictureUrl;
                } finally {
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return pictureUrl;
    }
 
    public static void main(String[] args) {
        upload(new File("d:\\a.jpg"));
    }
}