-
Notifications
You must be signed in to change notification settings - Fork 0
/
UploadImgWhatsApp.java
91 lines (75 loc) · 3.23 KB
/
UploadImgWhatsApp.java
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
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
*
* @author renato
*/
public class UploadImgWhatsApp {
private final String url;
private final String accessToken;
private final String type;
private final String messagingProduct;
public UploadImgWhatsApp(String url, String accessToken, String type, String messagingProduct) {
this.url = url;
this.accessToken = accessToken;
this.type = type;
this.messagingProduct = messagingProduct;
}
public String uploadImg(String filePath) throws IOException {
URL requestUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) requestUrl.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Authorization", "Bearer " + accessToken);
conn.setRequestProperty("Content-Type", "multipart/form-data");
// criando form data
String boundary = "qualquercoisaesperoquedecerto";
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
OutputStream outputStream = conn.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream));
// preenchendo o campo type
writer.append("--").append(boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"type\"\r\n\r\n");
writer.append(type).append("\r\n");
// preenchendo o canmpo messagingProduct
writer.append("--").append(boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"messaging_product\"\r\n\r\n");
writer.append(messagingProduct).append("\r\n");
// preenchendo o campo file
File file = new File(filePath);
writer.append("--").append(boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(file.getName()).append("\"\r\n");
writer.append("Content-Type: ").append(type).append("\r\n\r\n");
writer.flush();
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
inputStream.close();
writer.append("\r\n");
writer.append("--").append(boundary).append("--\r\n");
writer.flush();
writer.close();
// Get the response
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
BufferedReader reader;
if (responseCode == HttpURLConnection.HTTP_OK) {
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
reader = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
System.out.println("Response from API: " + response.toString());
return response.toString();
}
}