Skip to content

Commit ecb01c3

Browse files
committed
Resources
1 parent 3bfaa0f commit ecb01c3

File tree

6 files changed

+100
-38
lines changed

6 files changed

+100
-38
lines changed

filemanager/src/main/java/read/ReadFileToByteArray.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package read;
2+
3+
import org.apache.commons.codec.binary.Base64;
4+
import org.apache.commons.io.FileUtils;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.util.Objects;
10+
11+
/**
12+
* If we use Spring: https://www.baeldung.com/spring-classpath-file-access
13+
*/
14+
public class ResourceFilesUtils {
15+
16+
private ClassLoader classLoader;
17+
18+
public ResourceFilesUtils() {
19+
classLoader = getClass().getClassLoader();
20+
}
21+
22+
public String convertResourceToByteArray() {
23+
InputStream inputStream = classLoader.getResourceAsStream("img/test.png");
24+
try {
25+
return Objects.isNull(inputStream) ? "RESOURCE NOT FOUND" : new String(inputStream.readAllBytes());
26+
} catch (IOException e) {
27+
return e.getMessage();
28+
}
29+
}
30+
31+
/**
32+
* Need check with \Users\ONE_NAME\ because Nombre%20Apellido
33+
*
34+
* @return
35+
*/
36+
public String convertResourceFileToBase64() {
37+
File file = new File(classLoader.getResource("img/test.png").getFile());
38+
System.out.println(file.getAbsolutePath());
39+
System.out.println(file.getAbsoluteFile());
40+
System.out.println(file.getPath());
41+
try {
42+
return Objects.isNull(file) ? "RESOURCE NOT FOUND" : new String(Base64.encodeBase64(FileUtils.readFileToByteArray(file)));
43+
} catch (IOException e) {
44+
return e.getMessage();
45+
}
46+
}
47+
48+
public String convertResourceInputStreamToBase64() {
49+
InputStream inputStream = classLoader.getResourceAsStream("img/test.png");
50+
try {
51+
if (Objects.isNull(inputStream)) {
52+
return "RESOURCE NOT FOUND";
53+
} else {
54+
return new String(Base64.encodeBase64(inputStream.readAllBytes()));
55+
}
56+
} catch (IOException e) {
57+
return e.getMessage();
58+
}
59+
}
60+
61+
}
67.3 KB
Loading

filemanager/src/test/java/read/ReadFileToByteArrayTest.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package read;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Objects;
8+
9+
class ResourceFilesUtilsTest {
10+
11+
private ResourceFilesUtils resourceFilesUtils;
12+
13+
@BeforeEach
14+
void setUp() {
15+
resourceFilesUtils = new ResourceFilesUtils();
16+
}
17+
18+
@Test
19+
void test_convertResourceToByteArray() {
20+
String result = resourceFilesUtils.convertResourceToByteArray();
21+
System.out.println(result);
22+
Assertions.assertTrue(Objects.nonNull(result));
23+
}
24+
25+
@Test
26+
void test_convertResourceToBase64() {
27+
String result = resourceFilesUtils.convertResourceFileToBase64();
28+
System.out.println(result);
29+
Assertions.assertTrue(Objects.nonNull(result));
30+
}
31+
32+
@Test
33+
void test_convertResourceInputStreamToBase64() {
34+
String result = resourceFilesUtils.convertResourceInputStreamToBase64();
35+
System.out.println(result);
36+
Assertions.assertTrue(Objects.nonNull(result));
37+
}
38+
39+
}
67.3 KB
Loading

0 commit comments

Comments
 (0)