|
5 | 5 | import com.drew.metadata.Directory; |
6 | 6 | import com.drew.metadata.Tag; |
7 | 7 | import com.drew.imaging.ImageMetadataReader; |
8 | | -import java.io.File; |
9 | | -import java.io.FileInputStream; |
10 | | -import java.io.FileNotFoundException; |
11 | | -import java.io.IOException; |
12 | 8 |
|
13 | | -import java.nio.file.Path; |
| 9 | +import java.io.*; |
| 10 | + |
| 11 | +import java.net.URL; |
14 | 12 | import java.nio.file.Paths; |
15 | 13 | import java.util.Scanner; |
16 | 14 |
|
@@ -40,16 +38,48 @@ public static void getHiddenSecrets(File file) { |
40 | 38 | } |
41 | 39 | } |
42 | 40 |
|
43 | | - public static void main(String[] args) { |
44 | | - System.out.print("File path to JPEG: "); |
45 | | - Scanner scanner = new Scanner(System.in); |
46 | | - Path filePath = Paths.get(scanner.nextLine()); |
47 | | - |
48 | | - getHiddenSecrets(filePath.toFile()); |
49 | | - // Put your code to request a file path, |
50 | | - // read in a string from System.in, |
51 | | - // convert that string into A Path type using Paths class, |
52 | | - // and call the getHiddenSecrets method to get the file's meta-data |
53 | | - // HERE |
| 41 | + public static void getHiddenSecretsFromURL(URL url) { |
| 42 | + try { |
| 43 | + Metadata metadata = ImageMetadataReader.readMetadata( |
| 44 | + url.openStream() |
| 45 | + ); |
| 46 | + for (Directory directory : metadata.getDirectories()) { |
| 47 | + for (Tag tag : directory.getTags()) { |
| 48 | + System.out.format("[%s] - %s = %s%n", |
| 49 | + directory.getName(), tag.getTagName(), tag.getDescription()); |
| 50 | + } |
| 51 | + if (directory.hasErrors()) { |
| 52 | + for (String error : directory.getErrors()) { |
| 53 | + System.err.format("ERROR: %s%n", error); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } catch (ImageProcessingException e) { |
| 58 | + System.out.println("Failed to process the image meta-data."); |
| 59 | + throw new RuntimeException(e); |
| 60 | + } catch (IOException e) { |
| 61 | + System.out.println("Problem reading from URL."); |
| 62 | + throw new RuntimeException(e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String[] args) throws IOException { |
| 67 | + while (true) { |
| 68 | + System.out.print("File path or URL to image: "); |
| 69 | + Scanner scanner = new Scanner(System.in); |
| 70 | + String location = scanner.nextLine().trim(); |
| 71 | + |
| 72 | + // add a lazy exit clause |
| 73 | + if (location.equalsIgnoreCase("end") || location.equalsIgnoreCase("exit")) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (location.startsWith("https://") || location.startsWith("http://")) { |
| 78 | + getHiddenSecretsFromURL(new URL(location)); |
| 79 | + } else { |
| 80 | + getHiddenSecrets(Paths.get(location).toFile()); |
| 81 | + } |
| 82 | + System.out.println(); |
| 83 | + } |
54 | 84 | } |
55 | 85 | } |
0 commit comments