相同的JAVA程序在不同的平台上运行不同。
例如,我写了一个 JAVA,用于将存储在一个文件夹中的不同 Tiff 文件组合成多页 Tiff。
请在下面找到程序。
public String merge(String dirPath) {
String inputDir = dirPath;
File faxSource = new File(inputDir);
File file[] = faxSource.listFiles();
int numImages = file.length;
String name = "";
List<BufferedImage> images = new ArrayList<BufferedImage>();
Arrays.sort(file, new Comparator<File>() {
public int compare(File f1, File f2) {
return Long.compare(f1.lastModified(), f2.lastModified());
}
});
try {
for (int i = 0; i < numImages; i++) {
name = name + file[i].getName();
SeekableStream ss = new FileSeekableStream(file[i]);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff",
ss, null);
int numPages = decoder.getNumPages();
for (int j = 0; j < numPages; j++) {
PlanarImage op = new NullOpImage(
decoder.decodeAsRenderedImage(j), null, null,
OpImage.OP_IO_BOUND);
images.add(op.getAsBufferedImage());
}
}
// name=UUID.randomUUID().toString()+".tiff";
TIFFEncodeParam params = new TIFFEncodeParam();
params.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
OutputStream out = new FileOutputStream(inputDir + "\\" + name);
ImageEncoder encoder = ImageCodec.createImageEncoder("tiff", out,
params);
// encoder.
List<BufferedImage> imageList = new ArrayList<BufferedImage>();
for (int i = 1; i < images.size(); i++) {
imageList.add(images.get(i));
}
params.setExtraImages(imageList.iterator());
encoder.encode(images.get(0));
out.close();
} catch (Exception e) {
}
return inputDir + "\\" + name;
}
假设该文件夹包含4张tiff图片(A.tiff,B.tiff,C,.tiff,D.tiff)。这些Tiff文件是从S#中依次下载的。
如果我在 Windows Server 中运行上述程序,它会按照 A.tiff+B.tiff+C.tiff+D.tiff 的顺序进行整理。
如果我在 Amazon EC2 Linux 中运行相同的程序,我将得到输出 A.tiff+B.tiff+D.tiff+C.tiff。
知道为什么相同的 JAVA 代码在 Windows 和 Linux 中运行不同吗?
最佳答案
检查 Linux 机器上的文件系统; EXT3
的修改日期精度,我猜它正在使用,是 1 秒。如果您在一秒钟内下载两个文件,它们的时间可能相同。
另一方面,Windows 通常使用 NTFS,其文件时间精度为 100 纳秒。
关于java - 为什么同一个 JAVA 程序在 Windows 和 Linux 等不同平台上运行不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30114601/