多檔案上傳 excel多檔案匯入(大量資料)

2021-08-15 23:52:30 字數 1967 閱讀 3589

問題:多個excel檔案匯入(上傳)

思路:由於檔案選擇只能單個檔案選擇,那麼可以將excel檔案乙個乙個上傳到本地或伺服器,將路徑儲存至資料庫中,後台使用佇列進行操作,即上傳第乙個檔案時便在後台開啟一條執行緒,注意是只開啟一條如果多條會導致死鎖等各種問題,開啟執行緒後,將陸續上傳的檔案加入佇列中,只要佇列中有資料就不結束執行緒,直到佇列中的資料全部執行完也就是前端上傳的檔案全部匯入到資料庫中就結束執行緒。前端提供進度條。即能上傳大量資料又不影響前端頁面的操作,只需檢視進度條即可知道資料上傳量。

效果:

後台實現**:

1、將上傳的檔案乙個個加入佇列中

首先將所有上傳檔案存到本地或者伺服器,將路徑儲存到資料庫

string filepath =fileservice.upload(fileinfo.filetype.file, file,true,null);

importlog importlog =importlogservice.putdowntask(total, file.getoriginalfilename(), filepath);

importsubject.addtask(importlog);

再講乙個個存到資料庫中的檔案路徑放到佇列中。addtask();方法如下

@resource(name ="productimportsubscriber")

privateimportsubscriberimportsubscriber;

@resource(name ="taskexecutor")

privatethreadpooltaskexecutorthreadpool;

@resource(name ="importevent")

privateimporteventimportevent;

public voidaddtask(importlog importlog)

});}

}

判斷isrunning是否在啟動,所有每次只有乙個檔案在匯入,並不會出現其他資源爭搶的情況。

/**

* 觀察者介面

*/public inte***ceimportsubscriber

實現importsubscriber介面:
public staticqueuequeue 

=newlinkedlist<>();

@override

publicboolean start()

@override

public voidadd(importlog importlog)

到這裡多檔案匯入就結束了,從全域性看,這種方式只是比較巧妙了運用了佇列。比如如果你要匯入乙個excel檔案,有100w+的資料,那麼前端
會非常慢,導致那個頁面除了一直匯入以外什麼事也做不了,那麼使用者體驗就非常差,而這裡是先將檔案儲存起來,
在後台慢慢執行,而前端只是給了乙個進度條,可以告知使用者什麼時候能匯入完。

多檔案上傳

這是我開發過程中用涉及到的乙個功能,現在備份下來。首先是在 web.confing 中做限制上傳大小配置和超時的配置,的節點下有 executiontimeout maxrequestlength兩個屬性。executiontimeout設定超時的時間值,預設的為90秒,如果超出這個時間,瀏覽器就會...

多檔案上傳

c 版本 upload.aspx page language c codebehind upload.aspx.cs autoeventwireup false inherits webportal.upload upload.aspx.cs using system using system.co...

多檔案上傳

多檔案上傳 param files 檔案集 param staticurl 當前指定靜態資源路徑 param fileurl 分類儲存路徑 return 資料儲存路徑,逗號拼接字串 public static string uploadfiles multipartfile files,string...