1
1
package com .youdemy .service ;
2
2
3
+ import java .io .*;
4
+ import java .net .HttpURLConnection ;
3
5
import java .io .ByteArrayOutputStream ;
4
6
import java .io .File ;
5
7
import java .io .IOException ;
@@ -233,6 +235,13 @@ public void init() throws IOException {
233
235
orderRepository .save (order6 );
234
236
}
235
237
238
+ // public byte[] loadRandomImage() throws IOException {
239
+ // int randomImgNum = (int) Math.floor(Math.random() * 9) + 1;
240
+ // File image = ResourceUtils.getFile("classpath:./fakeImages/" + randomImgNum + ".jpg");
241
+ //
242
+ // return Files.readAllBytes(image.toPath());
243
+ // }
244
+
236
245
public byte [] loadRandomImage () throws IOException {
237
246
URL url = new URL ("https://picsum.photos/1920/1080" );
238
247
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
@@ -245,17 +254,18 @@ public byte[] loadRandomImage() throws IOException {
245
254
while ( (n = is .read (byteChunk )) > 0 ) {
246
255
baos .write (byteChunk , 0 , n );
247
256
}
257
+
258
+ return baos .toByteArray ();
248
259
}
249
260
catch (IOException e ) {
250
- System .err .printf ("Failed while reading bytes from %s: %s" , url .toExternalForm (), e .getMessage ());
261
+ System .err .printf ("Failed while reading bytes from %s: %s" , apiURI .toExternalForm (), e .getMessage ());
251
262
e .printStackTrace ();
252
263
}
253
264
finally {
254
265
if (is != null ) { is .close (); }
255
266
}
256
267
257
- return baos .toByteArray ();
258
-
268
+ return null ;
259
269
}
260
270
261
271
}
0 commit comments