Implementation technology of uploading and downloading SpringMVC large files (above 100M)

Folder database processing logic

public class DbFolder

{

    JSONObject root;

   

    public DbFolder()

    {

        this.root = new JSONObject();

        this.root.put("f_id", "");

        this.root.put("f_nameLoc", "根目录");

        this.root.put("f_pid", "");

        this.root.put("f_pidRoot", "");

    }

   

    /**

     * Convert JSONArray to map

     * @param folders

     * @return

     */

    public Map<String, JSONObject> toDic(JSONArray folders)

    {

        Map<String, JSONObject> dt = new HashMap<String, JSONObject>();

        for(int i = 0 , l = folders.size();i<l;++i)

        {

            JSONObject o = folders.getJSONObject(i);

            String id = o.getString("f_id");

            dt.put(id, o);

        }

        return dt;

    }

   

    public Map<String, JSONObject> foldersToDic(String pidRoot)

    {

        //Load the root directory by default

        String sql = String.format("select f_id,f_nameLoc,f_pid,f_pidRoot from up6_folders where f_pidRoot='%s'", pidRoot);

 

        SqlExec se = new SqlExec();

        JSONArray folders = se.exec("up6_folders", sql, "f_id,f_nameLoc,f_pid,f_pidRoot","");

        return this.toDic(folders);

    }

   

    public ArrayList<JSONObject> sortByPid( Map<String, JSONObject> dt, String idCur, ArrayList<JSONObject> psort) {

 

        String cur = idCur;

        while (true)

        {

            //key does not exist

            if (!dt.containsKey(cur)) break;

 

            JSONObject d = dt.get(cur);//Check the parent ID

            psort.add(0, d);//Arrange the parent node in front           

            cur = d.getString("f_pid").trim();//Take the parent ID

 

            if (cur.trim() == "0") break;

            if ( StringUtils.isBlank(cur) ) break;

        }

        return psort;

    }

   

 

    public JSONArray build_path_by_id(JSONObject fdCur) {

 

        String id = fdCur.getString("f_id").trim();//

        String pidRoot = fdCur.getString("f_pidRoot").trim();//

 

        //Root directory

        ArrayList<JSONObject> psort = new ArrayList<JSONObject>();

        if (StringUtils.isBlank(id))

        {

            psort.add(0, this.root);

 

            return JSONArray.fromObject(psort);

        }

 

        //Build a directory mapping table (id, folder)

        Map<String, JSONObject> dt = this.foldersToDic(pidRoot);

 

        //Arrange the directory in hierarchical order

        psort = this.sortByPid(dt, id, psort);

 

        SqlExec se = new SqlExec();

        //Is a subdirectory -> add root directory

        if (!StringUtils.isBlank(pidRoot))

        {

            JSONObject root = se.read("up6_files"

                    , "f_id,f_nameLoc,f_pid,f_pidRoot"

                    , new SqlParam[] { new SqlParam("f_id", pidRoot) });

            psort.add(0, root);

        }// is the root directory -> add root directory

        else if (!StringUtils.isBlank(id) && StringUtils.isBlank(pidRoot))

        {

            JSONObject root = se.read("up6_files"

                    , "f_id,f_nameLoc,f_pid,f_pidRoot"

                    , new SqlParam[] { new SqlParam("f_id", id) });

            psort.add(0, root);

        }

        psort.add(0, this.root);

 

        return JSONArray.fromObject(psort);

    }

   

    public FileInf read(String id) {

        SqlExec se = new SqlExec();

        String sql = String.format("select f_pid,f_pidRoot,f_pathSvr from up6_files where f_id='%s' union select f_pid,f_pidRoot,f_pathSvr from up6_folders where f_id='%s'", id,id);

        JSONArray data = se.exec("up6_files", sql, "f_pid,f_pidRoot,f_pathSvr","");

        JSONObject o = (JSONObject)data.get(0);

 

        FileInf file = new FileInf();

        file.id = id;

        file.pid = o.getString("f_pid").trim();

        file.pidRoot = o.getString("f_pidRoot").trim();

        file.pathSvr = o.getString("f_pathSvr").trim();

        return file;

    }

   

    public Boolean exist_same_file(String name,String pid)

    {

        SqlWhereMerge amount = new SqlWhereMerge ();

        swm.equal("f_nameLoc", name.trim());

        swm.equal("f_pid", pid.trim());

        swm.equal("f_deleted", 0);

 

        String sql = String.format("select f_id from up6_files where %s ", swm.to_sql());

 

        SqlExec se = new SqlExec();

        JSONArray arr = se.exec("up6_files", sql, "f_id", "");

        return arr.size() > 0;

    }

   

    /**

     * Check if there is a directory with the same name

     * @param name

     * @param pid

     * @return

     */

    public Boolean exist_same_folder(String name,String pid)

    {

        SqlWhereMerge amount = new SqlWhereMerge ();

        swm.equal("f_nameLoc", name.trim());

        swm.equal("f_deleted", 0);

        swm.equal("LTRIM (f_pid)", pid.trim());

        String where = swm.to_sql();

 

        String sql = String.format("(select f_id from up6_files where %s ) union (select f_id from up6_folders where %s)", where,where);

 

        SqlExec se = new SqlExec();

        JSONArray fid = se.exec("up6_files", sql, "f_id", "");

        return fid.size() > 0;     

    }

   

    public Boolean rename_file_check(String newName,String pid)

    {

        SqlExec se = new SqlExec();           

        JSONArray res = se.select("up6_files"

            , "f_id"

            ,new SqlParam[] {

                new SqlParam("f_nameLoc",newName)

                ,new SqlParam("f_pid",pid)

            },"");

        return res.size() > 0;

    }

   

    public Boolean rename_folder_check(String newName, String pid)

    {

        SqlExec se = new SqlExec();

        JSONArray res = se.select("up6_folders"

            , "f_id"

            , new SqlParam[] {

                new SqlParam("f_nameLoc",newName)

                ,new SqlParam("f_pid",pid)

            },"");

        return res.size() > 0;

    }

 

    public void rename_file(String name,String id) {

        SqlExec se = new SqlExec();

        se.update("up6_files"

            , new SqlParam[] { new SqlParam("f_nameLoc", name) }

            , new SqlParam[] { new SqlParam("f_id", id) });

    }

   

    public void rename_folder(String name, String id, String pid) {

        SqlExec se = new SqlExec();

        se.update("up6_folders"

            , new SqlParam[] { new SqlParam("f_nameLoc", name) }

            , new SqlParam[] { new SqlParam("f_id", id) });

    }

}

 

1. About 4880 lines of code in webuploader.js, add the webkitdirectory attribute under the dynamically generated input component (you can also search for input directly).

function FileUploader(fileLoc, mgr)

{

    var _this = this;

    this.id = fileLoc.id;

    this.ui = { msg: null, process: null, percent: null, btn: { del: null, cancel: null,post:null,stop:null }, div: null};

    this.isFolder = false; //Not a folder

    this.app = mgr.app;

    this.Manager = mgr; //Upload manager pointer

    this.event = mgr.event;

    this.Config = mgr.Config;

    this.fields = jQuery.extend((), mgr.Config.Fields, fileLoc.fields);//Each object comes with its own fields

    this.State = this.Config.state.None;

    this.uid = this.fields.uid;

    this.fileSvr = {

          pid: ""

        , id: ""

        , pidRoot: ""

        , f_fdTask: false

        , f_fdChild: false

        , uid: 0

        , nameLoc: ""

        , nameSvr: ""

        , pathLoc: ""

        , pathSvr: ""

        , pathRel: ""

        , md5: ""

        , lenLoc: "0"

        , sizeLoc: ""

        , FilePos: "0"

        , lenSvr: "0"

        , perSvr: "0%"

        , complete: false

        , deleted: false

    };//json obj, server file information

    this.fileSvr = jQuery.extend(this.fileSvr, fileLoc);

2. Can get the path

     this.open_files = function (json)

     {

         for (var i = 0, l = json.files.length; i < l; ++i)

        {

             this.addFileLoc(json.files[i]);

         }

         setTimeout(function () { _this.PostFirst(); },500);

     };

     this.open_folders = function (json)

    {

        for (var i = 0, l = json.folders.length; i < l; ++i) {

            this.addFolderLoc(json.folders[i]);

        }

         setTimeout(function () { _this.PostFirst(); }, 500);

     };

     this.paste_files = function (json)

     {

         for (var i = 0, l = json.files.length; i < l; ++i)

         {

             this.addFileLoc(json.files[i]);

         }

     };

 

Most of the back-end code logic is the same, and it currently supports MySQL, Oracle, and SQL. You need to configure the database before use. You can refer to this article I wrote: http://blog.ncmem.com/wordpress/2019/08/07/java Large file upload and download/
 

You can join the group to discuss together: 374992201

Guess you like

Origin blog.csdn.net/weixin_45525177/article/details/108512647