java 如何只从服务器下载新文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/741466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:36:12  来源:igfitidea点击:

How do I only download new files from a server?

javafilesystems

提问by Ankur

I have a number of CSV files that I want to download from Yahoo finance each day. I want my application to read the file's creation date (on my computer, not the server). If the creation date is prior to today then the new file should be downloaded (as it will have new data). If not then the new file should not be downloaded, and the correlation calculator (which is essentially what my application is), should use the last downloaded file for the particular stock code.

我每天都想从雅虎财经下载许多 CSV 文件。我希望我的应用程序读取文件的创建日期(在我的计算机上,而不是服务器上)。如果创建日期早于今天,则应下载新文件(因为它将有新数据)。如果不是,则不应下载新文件,并且相关性计算器(本质上就是我的应用程序)应使用上次下载的文件作为特定股票代码。

I have done some googling and have found the Apache POI project.

我做了一些谷歌搜索并找到了 Apache POI 项目。

Is this the best way to go, is there a better way, what would you recommend. Is JNI at all relevant here?

这是最好的方法吗,有没有更好的方法,你有什么推荐的。JNI 在这里完全相关吗?

回答by Maurice Perry

I might be missing something but I can't see why you would need JNI or POI to download a file. If you are downloading the file with HTTP, you can use an HttpURLConnection with the "If-Modified-Since" request header.

我可能遗漏了一些东西,但我不明白为什么需要 JNI 或 POI 来下载文件。如果您使用 HTTP 下载文件,则可以使用带有“If-Modified-Since”请求标头的 HttpURLConnection。

回答by Peter Perhá?

Did you consider creating an FTP account for access to that particular folder and then using an FTP client like SmartFTP or FileZilla to synchronize your local folder with the remote one? Should be well easy to set up and also convenient to use... Also, you could simply create an FTP command script and execute that from your Java code, if absolutely necessary...

您是否考虑过创建一个 FTP 帐户来访问该特定文件夹,然后使用 SmartFTP 或 FileZilla 等 FTP 客户端将本地文件夹与远程文件夹同步?应该很容易设置并且使用起来也很方便...此外,如果绝对必要,您可以简单地创建一个 FTP 命令脚本并从您的 Java 代码中执行该脚本...

Or I'll try to point you into another direction: md5() or other message-digest algorithms could help you. you wouldn't have to rely on timestamps. Try to calculate md5() hash of the file you have and the file you are about to download. Then you know whether to download or not.

或者我会尝试将您指向另一个方向:md5() 或其他消息摘要算法可以帮助您。您不必依赖时间戳。尝试计算您拥有的文件和您将要下载的文件的 md5() 哈希值。然后你就知道要不要下载了。

回答by Hejazzman

I have a number of CSV files that I want to download from Yahoo finance each day. I want my application to read the file's creation date (on my computer, not the server). If the creation date is prior to today then the new file should be downloaded (as it will have new data).

我每天都想从雅虎财经下载许多 CSV 文件。我希望我的应用程序读取文件的创建日期(在我的计算机上,而不是服务器上)。如果创建日期早于今天,则应下载新文件(因为它将有新数据)。

In order to detect changes to the local file, you need the file's last modification date, which is more generic than the creation date for this kind of check (since it also shows changes to the file after it has been created).

为了检测对本地文件的更改,您需要文件的最后修改日期,该日期比此类检查的创建日期更通用(因为它还会显示文件创建后的更改)。

You can get that in Java by using the

你可以在 Java 中使用

public long lastModified()

method on a File object.

File 对象上的方法。

Note that there is no method to get the creation date in the File API, probably because this information is not available in all filesystems.

请注意,在 File API 中没有获取创建日期的方法,可能是因为此信息并非在所有文件系统中都可用。

If you absolutelyneed to have a file creation date, then (if you create the files yourself or you can ask those who do) you could encode the creation date by convention in the file name, like this: myfile_2009_04_11.csv.

如果你绝对需要一个文件创建日期,那么(如果你自己创建文件或者你可以问那些做的人)你可以在文件名中按照约定对创建日期进行编码,像这样:myfile_2009_04_11.csv。

Then you will have to parse the file name and determine the creation date.

然后您必须解析文件名并确定创建日期。

I have done some googling and have found the Apache POI project. Is this the best way to go, is there a better way, what would you recommend.

我做了一些谷歌搜索并找到了 Apache POI 项目。这是最好的方法吗,有没有更好的方法,你有什么推荐的。

The Apache POI project is a library for reading and writing MS Office files (Excel files in this case). CSV is a simple textual format, so you don't need POI to read it.

Apache POI 项目是一个用于读取和写入 MS Office 文件(在本例中为 Excel 文件)的库。CSV 是一种简单的文本格式,因此您无需 POI 即可阅读。

Also, the information you need (creation date or last modification date) is available as metadata on the file itself, not in the file's data, so you don't need POI to get to it.

此外,您需要的信息(创建日期或上次修改日期)可作为文件本身的元数据提供,而不是文件数据中的元数据,因此您无需 POI 即可获取。

Is JNI at all relevant here?

JNI 在这里完全相关吗?

Theoretically, you could use a custom JNI extension (a bridge to native code) to get the file's creation date on those filesystems that support it.

从理论上讲,您可以使用自定义 JNI 扩展(通向本机代码的桥梁)在支持它的文件系统上获取文件的创建日期。

However, you're best off using the portable last modification date thats already in the Java SDK API and/or the "creation date encoded in the filename" convention.

但是,您最好使用 Java SDK API 中已有的可移植上次修改日期和/或“文件名中编码的创建日期”约定。

Using JNI will make your program not portable for no real added benefit.

使用 JNI 会使你的程序不可移植,没有真正的额外好处。

回答by Michael Borgwardt

JNI is definitely irrelevant, and so is Apache POI, unless the creation date is stored in the file itself (unlikely). Otherwise, it's external metadata and either accessible via the HTTP headers (possible using pure Java), or not accessible at all.

JNI 绝对无关紧要,Apache POI 也是如此,除非创建日期存储在文件本身中(不太可能)。否则,它是外部元数据,可以通过 HTTP 标头访问(可能使用纯 Java),或者根本无法访问。