-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathFileUploadModule.java
More file actions
177 lines (133 loc) · 6.22 KB
/
FileUploadModule.java
File metadata and controls
177 lines (133 loc) · 6.22 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.yoloci.fileupload;
import android.os.Bundle;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import java.io.DataInputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.DataOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.facebook.react.bridge.WritableMap;
import java.io.FileInputStream;
import org.json.JSONObject;
public class FileUploadModule extends ReactContextBaseJavaModule {
@Override
public String getName() {
return "FileUpload";
}
public FileUploadModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@ReactMethod
public void upload(final ReadableMap options, final Callback callback) {
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String uploadUrl = options.getString("uploadUrl");
String method;
if (options.hasKey("method")) {
method = options.getString("method");
} else {
method = "POST";
}
ReadableMap headers = options.getMap("headers");
ReadableArray files = options.getArray("files");
ReadableMap fields = options.getMap("fields");
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
URL connectURL = null;
FileInputStream fileInputStream = null;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
try {
connectURL = new URL(uploadUrl);
connection = (HttpURLConnection) connectURL.openConnection();
// Allow Inputs & Outputs.
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod(method);
// set headers
ReadableMapKeySetIterator iterator = headers.keySetIterator();
while (iterator.hasNextKey()) {
String key = iterator.nextKey();
connection.setRequestProperty(key, headers.getString(key));
}
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( connection.getOutputStream() );
// set fields
ReadableMapKeySetIterator fieldIterator = fields.keySetIterator();
while (fieldIterator.hasNextKey()) {
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
String key = fieldIterator.nextKey();
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd + lineEnd);
outputStream.writeBytes(fields.getString(key));
outputStream.writeBytes(lineEnd);
}
for (int i = 0; i < files.size(); i++) {
ReadableMap file = files.getMap(i);
String filename = file.getString("filename");
String filepath = file.getString("filepath");
String name = file.hasKey("name") ? file.getString("name") : filename;
filepath = filepath.replace("file://", "");
fileInputStream = new FileInputStream(filepath);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + name + "\";filename=\"" + filename + "\"" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
}
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();
if (serverResponseCode != 200) {
fileInputStream.close();
outputStream.flush();
outputStream.close();
callback.invoke("Error happened: " + serverResponseMessage, null);
} else {
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
String data = sb.toString();
JSONObject mainObject = new JSONObject();
mainObject.put("data", data);
mainObject.put("status", serverResponseCode);
BundleJSONConverter bjc = new BundleJSONConverter();
Bundle bundle = bjc.convertToBundle(mainObject);
WritableMap map = Arguments.fromBundle(bundle);
fileInputStream.close();
outputStream.flush();
outputStream.close();
callback.invoke(null, map);
}
} catch(Exception ex) {
callback.invoke("Error happened: " + ex.getMessage(), null);
}
}
}