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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
| public class FileV2Proxy {
public static String getToken() throws Exception { String authId = SpringUtil.getEvnProperty("file.authId"); final String url = getInSideUrlRoot() + "/api/v2/token?authId=" + authId; String res = HttpUtil2.sendGet(url); JsonResult json = JSON.parseObject(res, JsonResult.class); if ( !json.getSuccess() ) throw new Exception( String.format("token申请失败:%s", res) ); return String.valueOf( json.getData() ); }
public static byte[] readFile(String fileId) throws Exception{ String url = getOutSideUrlRoot() + "/api/v2/ext/" + fileId; byte[] file = visitUrl(url); return file; }
public static byte[] readFile(String fileId, String key) throws Exception{ String url = new StringBuilder( getOutSideUrlRoot() ) .append("/api/v2/ext/") .append(fileId) .append("?key=") .append(key).toString(); byte[] file = visitUrl(url); return file; }
public static LllFileV2 uploadFile(ULFileInfoVo fileInfo, byte[] cont) throws Exception { if (ToolUtil.isNull( fileInfo.getToken() )){ String token = getToken(); fileInfo.setToken(token); } String url = getOutSideUrlRoot() + "/api/v2/ext/uploadFile"; String base64Cont = Base64Util.encode(cont); Map<String,String> params = fileInfo.toMap(); params.put("base64Cont", base64Cont); String res = HttpUtil2.sendPost(url, params); LllFileV2 file = parseLllFileV2(res); return file; }
public static LllFileV2 transferTempFile(String fileId, ULFileInfoVo fileInfo) throws Exception { String url = getInSideUrlRoot() + "/api/v2/transfer-temp-file"; if ( StringUtil.isNull( fileInfo.getToken() ) ){ String authId = SpringUtil.getEvnProperty("file.authId"); fileInfo.setToken(authId); } Map<String,String> params = fileInfo.toMap(); params.put("fileId", fileId); String res = HttpUtil2.sendPost(url, params);
LllFileV2 file = parseLllFileV2(res); return file; }
public static List<LllFileV2> listFile(String authId, FileSearchParam params, boolean isCont) throws Exception{ String url = getInSideUrlRoot() + "/api/v2/list"; Map<String,String> _params = params.toMap(); _params.put("authId", authId);
int readContent = isCont ? 1 : 0; _params.put("cont", String.valueOf(readContent)); String res = HttpUtil2.sendPost(url, _params); List<LllFileV2> datas = parseLllFileV2List(res); return datas; } public static List<LllFileV2> listFile(String authId, FileSearchParam params) throws Exception{ return listFile(authId, params, false); } public static List<LllFileV2> listFile(FileSearchParam params, boolean isCont) throws Exception{ String authId = SpringUtil.getEvnProperty("file.authId"); return listFile(authId, params, isCont); }
public static int countFile(String authId, FileSearchParam params) throws Exception { String url = getInSideUrlRoot() + "/api/v2/count"; Map<String,String> _params = params.toMap(); _params.put("authId", authId); String res = HttpUtil2.sendPost(url, _params); int total = parseLllFileV2Int(res); return total; } public static int countFile(FileSearchParam params) throws Exception { String authId = SpringUtil.getEvnProperty("file.authId"); return countFile(authId, params); }
public static LllFileV2 getFile(String authId, String fileId, boolean cont) throws Exception{ String url = getInSideUrlRoot() + "/api/v2/getFileInfo"; Map<String, String> params = new HashMap<String, String>(); params.put("authId", authId); params.put("fileId", fileId);
int readContent = cont ? 1 : 0; params.put("cont", String.valueOf(readContent)); String res = HttpUtil2.sendPost(url, params); LllFileV2 file = parseLllFileV2(res); return file; } public static LllFileV2 getFile(String authId, String fileId) throws Exception{ return getFile(authId, fileId, false); } public static LllFileV2 getFile(String fileId, boolean cont) throws Exception{ String authId = SpringUtil.getEvnProperty("file.authId"); return getFile(authId, fileId, cont); }
public static void removeFile(String authId, List<String> fileIds) throws Exception{ String url = getInSideUrlRoot() + "/api/v2/removeFile"; String _fileIds = packFileIds(fileIds); Map<String, String> params = new HashMap<String, String>(); params.put("authId", authId); params.put("fileIds", _fileIds); String res = HttpUtil2.sendPost(url, params); parseResult(res); } public static void removeFile(List<String> fileIds) throws Exception{ String authId = SpringUtil.getEvnProperty("file.authId"); removeFile(authId, fileIds); } public static void removeFile(String authId, String fileId) throws Exception{ List<String> ls = Arrays.asList(fileId); removeFile(authId, ls); } public static void removeFile(String fileId) throws Exception{ String authId = SpringUtil.getEvnProperty("file.authId"); removeFile(authId, fileId); } private static String packFileIds(List<String> fileIds) { StringJoiner joiner = new StringJoiner(","); fileIds.forEach(s -> joiner.add(s));
return joiner.toString(); } private static String getOutSideUrlRoot() { return SpringUtil.getEvnProperty("file.rootUrl.outside"); } private static String getInSideUrlRoot() { return SpringUtil.getEvnProperty("file.rootUrl.inside"); } private static byte[] visitUrl(String url) throws Exception{ return HttpUtil2.sendGet(url, null, null, content -> content.asBytes()); } private static LllFileV2 parseLllFileV2(String res) throws Exception{ JsonObject data = parseJsonObject(res); LllFileV2 file = LllFileV2.create(data); return file; } private static List<LllFileV2> parseLllFileV2List(String res) throws Exception{ JsonArray datas = parseJsonArray(res); List<LllFileV2> ls = new ArrayList<LllFileV2>(); for(int i=0;i<datas.size();i++) { JsonObject json = datas.get(i).getAsJsonObject(); LllFileV2 file = LllFileV2.create(json); ls.add(file); } return ls; } private static int parseLllFileV2Int(String res) throws Exception{ String data = parseResult(res); return Integer.valueOf(data); } private static void parseResultSucc(JsonObject json) throws Exception{ boolean success = JsonUtil.getBoolean(json, "success"); if(!success) { JsonObject exJson = JsonUtil.getJsonObject(json, "ex"); String code = JsonUtil.getString(exJson, "code"); String reason = JsonUtil.getString(exJson, "reason"); String msg = "["+code+"]"+reason; throw new Exception(msg); } } private static String parseResult(String res) throws Exception{ JsonObject json = JsonUtil.parseJsonObject(res); parseResultSucc(json); String data = JsonUtil.getString(json, "data"); return data; } private static JsonObject parseJsonObject(String res) throws Exception{ JsonObject json = JsonUtil.parseJsonObject(res); parseResultSucc(json); JsonObject data = JsonUtil.getJsonObject(json, "data"); return data; } private static JsonArray parseJsonArray(String res) throws Exception{ JsonObject json = JsonUtil.parseJsonObject(res); parseResultSucc(json); JsonArray data = JsonUtil.getJsonArray(json, "data"); return data; } }
|