如何获取在java中创建的日期图片

我想提取一个jpg文件的创建日期。 Java具有File对象的lastModified方法,但似乎不支持从文件中提取创建的日期。 我相信信息存储在文件中,因为我在Win XP中将鼠标指针hover在文件上时所看到的日期与我在DOS中使用带有“dir / TC”的JNI所获得的日期不同。

信息以称为EXIF或链接文本的格式存储在图像中。 那里有几个能够读取这种格式的库,就像这个一样

日期存储在jpeg中的EXIF数据中。 java中有一个java库和一个查看器可能会有所帮助。

我使用这个元数据库: http : //www.drewnoakes.com/code/exif/

似乎工作得很好,但要记住并非所有JPEG图像都有这些信息,所以它不能100%万无一失。

如果EXIF元数据不包含创建的日期,那么您可能不得不使用Java的lastUpdated – 除非您想使用Runtime.exec(…)并使用系统函数找出(我不会’但是推荐这个!)

下面的代码示例询问用户文件路径,然后输出创建日期和时间:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(final String[] args) { try { // get runtime environment and execute child process Runtime systemShell = Runtime.getRuntime(); BufferedReader br1=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter filename: "); String fname=(String)br1.readLine(); Process output = systemShell.exec("cmd /c dir /a "+fname); // open reader to get output from process BufferedReader br = new BufferedReader (new InputStreamReader(output.getInputStream())); String out=""; String line = null; int step=1; while((line = br.readLine()) != null ) { if(step==6) { out=line; } step++; } // display process output try{ out=out.replaceAll(" ",""); System.out.println("CreationDate: "+out.substring(0,10)); System.out.println("CreationTime: "+out.substring(10,15)); } catch(StringIndexOutOfBoundsException se) { System.out.println("File not found"); } } catch (IOException ioe){ System.err.println(ioe); } catch (Throwable t) { t.printStackTrace();} } } 

您可能需要一些东西来访问exif数据。 Google建议使用此库 。