-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDownloadFileSample.java
More file actions
79 lines (70 loc) · 3.42 KB
/
DownloadFileSample.java
File metadata and controls
79 lines (70 loc) · 3.42 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
import com.envisioniot.enos.iot_http_integration.FileCategory;
import com.envisioniot.enos.iot_http_integration.HttpConnection;
import com.envisioniot.enos.iot_http_integration.message.IFileCallback;
import com.envisioniot.enos.iot_mqtt_sdk.core.exception.EnvisionException;
import com.envisioniot.enos.sdk.data.DeviceInfo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @author :charlescai
* @date :2020-03-16
*/
public class DownloadFileSample {
// EnOS API Gateway URL and HTTP Integration Channel URL, which can be obtained from Environment Information page in EnOS Console
static final String API_GW_URL = "http://api_gw_url";
static final String INTEGRATION_CHANNEL_URL = "http://integration_channel_url";
// EnOS Application AccessKey and SecretKey, which can be obtain in Application Registration page in EnOS Console
static final String APP_KEY = "appKey";
static final String APP_SECRET = "appSecret";
// Device credentials, which can be obtained from Device Details page in EnOS Console
static final String ORG_ID = "orgId";
static final String ASSET_ID = "assetId";
static final String PRODUCT_KEY = "productKey";
static final String DEVICE_KEY = "deviceKey";
public static void main(String[] args) {
// Construct a http connection
HttpConnection connection = new HttpConnection.Builder(
INTEGRATION_CHANNEL_URL, API_GW_URL, APP_KEY, APP_SECRET, ORG_ID)
.build();
DeviceInfo deviceInfo = new DeviceInfo().setAssetId(ASSET_ID);
// fileUri is an enos scheme file uri
String fileUri = "enos-connect://xxx.txt";
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
InputStream inputStream = connection.downloadFile(deviceInfo, fileUri, FileCategory.FEATURE);
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
byte[] data = outputStream.toByteArray();
System.out.println(new String(data));
} catch (EnvisionException | IOException e) {
e.printStackTrace();
}
// Asynchronously call the file download request
try {
connection.downloadFile(deviceInfo, fileUri, FileCategory.FEATURE, new IFileCallback() {
@Override
public void onResponse(InputStream inputStream) throws IOException {
System.out.println("download file asynchronously");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0 ,len);
}
byte[] data = outputStream.toByteArray();
System.out.println(new String(data));
}
@Override
public void onFailure(Exception failure) {
failure.printStackTrace();
}
}
);
} catch (EnvisionException e) {
e.printStackTrace();
}
}
}