简体   繁体   English

下载并阅读java Spring Rest服务中的大型csv文件

[英]Download and read on a large csv file in java Spring Rest service

I have a Spring rest service which runs periodically after every 10 min. 我有一个Spring Rest服务,每隔10分钟就会定期运行。 Service knows the path to a file stored on remote server. 服务知道存储在远程服务器上的文件的路径。 Size of the file is around 2GB. 文件大小约为2GB。 Service fetches the file and then reads contents of the file and then manipulates the database based on file contents. Service获取文件,然后读取文件的内容,然后根据文件内容操作数据库。 What is the best way for spring rest service to fetch large file and operate upon its contents. 弹簧休息服务获取大文件并对其内容进行操作的最佳方法是什么。

Use Apache File utils, 使用Apache File utils,

http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#copyURLToFile(java.net.URL , java.io.File, int, int) http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#copyURLToFile(java.net.URL,java.io.File,int,int

An example code to download 1 GB file from online is as below, 从在线下载1 GB文件的示例代码如下,

            package com.jai.download;

            import java.io.File;
            import java.net.URL;

            import org.apache.commons.io.FileUtils;

            public class FileDownload {

                public static void main(String[] args) {

                    File file = new File("c:/jj/1gb.zip");

                    URL url;
                    try {
                        url = new URL("http://download.thinkbroadband.com/1GB.zip");
                        long start = System.currentTimeMillis();
                        System.out.println("Downloading....");
                        FileUtils.copyURLToFile(url, file);
                        long end = System.currentTimeMillis();
                        System.out.println("Completed....in ms : " + (end - start));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

            }

With my internet speed of 100 Mbps, it downloaded this 1GB file in 94 secs. 我的互联网速度为100 Mbps,它在94秒内下载了这个1GB文件。 Once you have your csv file like this downloaded you could read its contents to do you business logic check and make appropriate action. 一旦下载了这样的csv文件,您就可以阅读其内容,进行业务逻辑检查并采取适当的措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM