上傳csv格式檔案,利用commons-fileupload
1、新增jar包依賴
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
2、配置bean
<!-- 要上傳檔案,就要引入這個bean -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="UTF-8" p:maxUploadSize="104857600">
</bean>
3、批量匯入按鈕
<button type="button" id="importButton" class="btn btn-success" onclick="importDialog()" style="width:150px">批量匯入
</button>
對應js:
//匯入資料對話方塊
function importDialog() {
$("#file").val("");
$("#uploadDiv").dialog({
resizable: false,
height: 280,
width: 480,
modal: true
});
}
彈出對應的對話方塊:
<!--批量匯入-->
<div id="uploadDiv" class="showBox" style="margin-left:5px;margin-top:5px;display:none" title="批量匯入介面">
<form id="inputForm1" name="inputForm1" action="/psadmin/uploadCPBrand.do" method="post" enctype="multipart/form-data" onsubmit="return false">
<table width="100%" class="up_table">
<tr>
<td width="30px">路徑</td>
<td align=left><input class="form-control" type="file" id="file" name="file"></input></td>
<td ><button class="btn btn-info" onclick="uploadCPBrand()"><span><span>匯入</span></span></button></td>
</tr>
</table>
</form>
<br>
<!--<HR align=left width=100% color=lightBlue SIZE=1>-->
<table width=100% cellspacing=0 cellpadding=0 border=1px class="up_table">
<tr>
<th width="30px"></th>
<th >母品牌</th>
<th >子品牌</th>
</tr>
<tr onmouseover="this.style.backgroundColor='#F0F8FF'" onmouseout="this.style.backgroundColor=''">
<td align="center">1</td>
<td>中興</td>
<td>努比亞</td>
</tr>
<tr onmouseover="this.style.backgroundColor='#F0F8FF'" onmouseout="this.style.backgroundColor=''">
<td align="center">2</td>
<td>瀘州老窖</td>
<td>老瀘州,瀘州原漿酒</td>
</tr>
</table>
<br>
<div class="up_info_t">提示</div>
<div class="up_info_c">
1、上傳檔案必須為csv格式。
<br>2、上傳檔案只包含母品牌,子品牌,子母品牌以Tab鍵分隔,例如:中興努比亞。
</div>
</div>
匯入按鈕對應js:
function uploadCPBrand() {
var files = $("#file").val();
if (files == null || files == "") {
alert("請選擇檔案");
return false;
} else if (files.substr(files.length - 4, 4).toLowerCase().match("(^.csv)") == null) {
alert("請選擇.csv格式檔案");
return false;
} else {
if (confirm('確定上傳?')) {
$("#inputForm1")[0].submit();
}
}
}
注意:如果要將引數帶入URL,請用下面的程式碼:
var url = "/ps-admin-nimitz/system/uploadAccount.do?roleId="+roleId+"&&role="+role;
$("#inputForm1").attr("action", url);
$("#inputForm1")[0].submit();
4、後臺程式碼:
controller類中方法:
@Controller
public class ChildAndParentBrandMining {
private final Logger log = SearchLogger.getLogger(ChildAndParentBrandMining.class);
@Autowired
private CPBrandService cpbrandService;
@Autowired
private PsInfoService psInfoService;
@Autowired
private ShowLogService showLogService;
private int seg = 10;// 預設一頁的記錄數
// 批量上傳
@RequestMapping("/uploadCPBrand.do")
public String uploadCPBrand(@RequestParam MultipartFile file, HttpServletRequest request, ModelMap model/*, Authentication authentication*/) throws Exception {
String userId = request.getRemoteUser();
BufferedReader br = new BufferedReader(new InputStreamReader(file.getInputStream(), "UTF-8"));
String line;
List<CPBrandBean> data = new LinkedList<CPBrandBean>();
while( (line = br.readLine()) != null ){
try{
String[] token = line.split("\t");
if( token == null || token.length != 2 )continue;
CPBrandBean brandBean = new CPBrandBean();
brandBean.setParentBrand(token[0]);
brandBean.setChildBrand(token[1]);
brandBean.setSource("本地");
data.add(brandBean);
}catch(Exception ex){
log.error("上傳檔案有髒資料",ex);
}
}
int count = cpbrandService.addBatch(data);
if(count>0){
String behavior = "本地匯入子母品牌資料";
log.info("使用者"+userId+behavior);
showLogService.addLog(userId, "子母品牌挖掘", behavior);
}
IOUtils.closeQuietly(br);
return "redirect:/psadmin/CPBrandList.do";//必須用重定向,不能直接用childAndParentBrandWeb方法,否則URL不會重定向
}
/**
* 子母品牌挖掘
**/
@RequestMapping("/CPBrandList.do")
public String childAndParentBrandWeb(ModelMap modelMap, HttpServletRequest request, @RequestParam(required = false) String keyword,
@RequestParam(required = false) String page) {
int totalpages;
String newKeyword = keyword == null ? "" : keyword.trim();
String page1 = page == null ? "1" : page.trim();
int pages = Integer.parseInt(page1);
int maxCount = cpbrandService.getTotalCount(newKeyword.toLowerCase());
if (maxCount % seg == 0) {
totalpages = maxCount / seg;
} else {
totalpages = maxCount / seg + 1;
}
List<CPBrandBean> CPBrandBeanList = new ArrayList<CPBrandBean>();
if (totalpages == 0) {
pages = 0;
} else {
if (pages > totalpages) {
pages = totalpages;
}
int start = (pages - 1) * seg + 1;
CPBrandBeanList = cpbrandService
.getQueryResult(newKeyword.toLowerCase(), start, -1);
modelMap.put("CPBrandBeanList", CPBrandBeanList);
}
modelMap.put("CPBrandBeanList", CPBrandBeanList);
modelMap.put("newKeyword", newKeyword);
modelMap.put("pages", totalpages);
modelMap.put("currentpage", pages);
psInfoService.baseInfo(request, modelMap);
modelMap.put("component", "child_parent_brand");
return "/admin/index.ftl";
}
service類:
package com.suning.web.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.suning.web.bean.CPBrandBean;
import com.suning.web.dao.CPBrandDao;
@Service
public class CPBrandService {
@Autowired
private CPBrandDao cpbrandDao;
// 返回查詢結果個數
public int getTotalCount(String newKeyword) {
return cpbrandDao.getTotalCount(newKeyword);
}
// 查詢
public List<CPBrandBean> getQueryResult(String newKeyword, int start, int segment) {
return cpbrandDao.getQueryResult(newKeyword, start, segment);
}
//修改
public boolean brandUpdate(String parentBrand, String newChildBrand, String source) {
return cpbrandDao.brandUpdate(parentBrand, newChildBrand, source);
}
//刪除任務對映
public boolean brandDelete(String parentBrand) {
return cpbrandDao.brandDelete(parentBrand.trim());
}
//匯入子母品牌資料
public int addBatch(List<CPBrandBean> data){
return cpbrandDao.addBatch(data);
}
}
dao類:
package com.suning.web.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import com.suning.web.bean.CPBrandBean;
@Repository
public class CPBrandDao {
private int seg = 10;
@Autowired
private JdbcTemplate jdbcTemplate;
public int addBatch(final List<CPBrandBean> data){
String sql = "INSERT INTO ps_child_parent_brand(parent_brand,child_brand,source) VALUES(?,?,?) ON DUPLICATE KEY UPDATE child_brand=?,source=?";
int[] res = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter(){
@Override
public void setValues(PreparedStatement ps, int i)
throws SQLException {
ps.setString(1, data.get(i).getParentBrand());
ps.setString(2, data.get(i).getChildBrand());
ps.setString(3, data.get(i).getSource());
ps.setString(4, data.get(i).getChildBrand());//更新的資料
ps.setString(5, data.get(i).getSource());//更新的資料
}
@Override
public int getBatchSize() {
return data.size();
}
});
return res[0];
}
}
對應的資料表設計:
CREATE TABLE `ps_child_parent_brand` (
`parent_brand` varchar(100) NOT NULL DEFAULT '' COMMENT '母品牌',
`child_brand` varchar(100) DEFAULT NULL COMMENT '子品牌',
`source` varchar(100) DEFAULT 'HIVE' COMMENT '資料來源',
PRIMARY KEY (`parent_brand`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='子母品牌表';
相關推薦
上傳csv格式檔案,利用commons-fileupload
1、新增jar包依賴 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload&
oracle ebs + PL/SQL實現將查詢出來的資料儲存為csv格式檔案,並定期上傳到FTP伺服器學習總結
目的 oracle ebs + PL/SQL實現將查詢出來的資料儲存為csv格式檔案,並定期上傳到FTP伺服器。 用到oracle utl_file包,FTP檔案上傳 第一次接觸這種型別的任務,也是在網上查詢了很多參考資料才弄出來。 下面是具體的例子。
如何在客戶端上傳shell指令碼檔案,並利用PHP呼叫執行指令碼
題目中的上傳包含兩部分,一部分是上傳檔案,一部分是利用PHP執行指令碼 上傳檔案到指定資料夾 所謂檔案上傳是指將本地文字檔案,圖片視訊或者音訊等檔案上傳到伺服器上,以供後續操作的過程。 上傳檔案有幾種方式,包括: - 單純的form表單上傳提交 使
讀取CSV格式檔案,修改 echarts 模板資料
需要做一個散點圖,但是excel實現的效果不太滿意 於是找到了 echarts 找個JS外掛,稍微修改了模板檔案,得到需要的散點圖 以下是讀取檔案資料,修改模板中data的程式碼 # -*- coding: utf-8 -*- """ Created on Wed Oct 17 14:5
如何使用elementUI呼叫一次介面同時上傳圖片和檔案,同時需要攜帶其他引數,實現呼叫後端介面
今天有一個坑,同時要上傳圖片和檔案,而且圖片要展示縮圖,檔案要展示列表。 我的思路是: 首先,只上傳附件照片,這個直接看ele的官方例子就行,不僅僅上傳附件照片,還同時上傳其他引數。 然後,再做上傳照片和檔案,上傳其他引數,其實也就是檔案合併。 一、上傳照片和其他引
vue上傳多個檔案,附件和其他資料一起傳給後臺
前端: 實現多圖上傳主要用到以下兩個屬性: <el-form-item label="附件上傳" label-width="80px"> <el-f
PHPExcel讀取csv格式檔案,解決中文讀取為空問題
PHPExcel讀取csv格式檔案 $file = "test.csv"; $type = strtolower( pathinfo($file, PATHINFO_EXTENSION) ); $path = __YOUR_FILE_PATH__.'/'
JAVAWEB開發之檔案的上傳與下載(開源元件commons-fileupload的詳細使用)
在Web應用系統開發中,檔案上傳和下載功能是非常常用的功能,今天來講一下JavaWeb中的檔案上傳和下載功能的實現。 對於檔案上傳,瀏覽器在上傳的過程中是將檔案以流的形式提交到伺服器端的,如果直接使用Servlet獲取上傳檔案的輸入流然後再解析裡面的請求引數是比較麻煩,所以一般選擇採用apac
1次ajax請求(XMLHttpRequest)上傳多個檔案,支援進度條
封裝了一個ajax多檔案上傳,功能就是選擇多個檔案,用ajax上傳。 呼叫方式也很簡單,寫一個json物件做為引數配置,設定要上傳的服務端url以及選擇檔案和上傳完成等的事件處理函式,new一個AjaxUploadX物件,呼叫selectFiles方法選擇檔案,呼叫upla
FTPClient上傳和下載檔案,中文亂碼問題解決
使用類: org.apache.commons.net.ftp.FTPClient 問題描述: 建立中文目錄、上傳中文檔名時,目錄名及檔名中的中文顯示亂碼 解決方法: 在網上Google了一些
winform上傳檔案,利用http,form-data格式上傳
/// <summary> /// 上傳檔案 /// </summary> /// <param name="url">服務地址</param> /// <param name="filePath">檔案路徑</param> public
利用SecureCRT上傳、下載檔案(使用sz與rz命令),超實用!
利用SecureCRT上傳、下載檔案(使用sz與rz命令) 藉助securtCRT,使用linux命令sz可以很方便的將伺服器上的檔案下載到本地,使用rz命令則是把本地檔案上傳到伺服器。 其中,對於sz和rz的理解與記憶我用瞭如下的方法(很多時候容易搞混
利用 Commons-Fileupload 實現檔案上傳
Apache FileUpload檔案上傳元件API解析(轉) Java Web開發人員可以使用Apache檔案上傳元件來接收瀏覽器上傳的檔案,該元件由多個類共同組成,但是,對於使用該元件來編寫檔案上傳功能的Java Web開發人員來說,只需要瞭解和使用其中的三個類:DiskFileUpload、FileIt
利用formdata對象上傳文件時,需要添加的參數
for alert string local art .ajax ror process 文件 function doUpload() { var formData = new FormData($( "#uploadForm" )[0]);
使用kettle將csv格式檔案輸入,sql表輸出
1. 在kettle中新建一個轉換,再儲存,再點選新建一個DB連線, 2. Csv檔案內容 3. 按住shift連線兩個圖示,
ueditor上傳圖片不好用,訪問ueditor中的controller.js變成下載檔案
由於疏忽了web.xml中的servlet配置問題,將urlpattern配置成了"/" <servlet-mapping> <servlet-name>test</servlet-name> &
檔案上傳到本地磁碟,磁碟路徑適應window和linux
private void uploadDisk(InputStream inputStream, String fileName) { OutputStream os = null; &
ossutil釋出1.4.2版本,支援上傳或複製檔案目錄指定儲存型別,支援訪問請求者付費模式的Bucket
ossutil 1.4.2 Release Note lscp命令:支援訪問開啟“請求者付費模式”的Bucket cp命令:上傳、複製檔案或目錄時,支援設定儲存型別 cp命令:上傳下載時,顯示傳輸速度 cp命令:Window版本支援斷點續傳(Linux、Mac版本已經支援) os
檔案上傳至oss後,獲取圖片縮率圖、獲取視訊截幀等後續操作
上一篇文章說了一下檔案上傳至oss:https://blog.csdn.net/new_programmer_h/article/details/84307005 這裡說一下上傳後的一些後續操作:常用的獲取圖片縮率圖、獲取視訊截幀生成封面圖。自我感覺阿里oss對於這些處理封裝的很好,只要根據:"%s|sys
Linux未安裝上傳下載的外掛,怎麼進行檔案的上傳下載
首先連上服務: 然後Alt+p,開啟SFTp視窗; 例如,我們今天要往tomcat的webappmu目錄下上傳一個檔案; 先pwd,檢視我們Linux上所處的目錄;pwd 然後進入到tomcat的webapp目錄下;cd apache-t