日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jquery传输文件到后端,后端处理数据。

發布時間:2023/12/8 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jquery传输文件到后端,后端处理数据。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<!--HTML代碼--> <input type="file" name="uploadFile" id="uploadFile"> <!--JQuery代碼--> $("#uploadFile").on("change", function() {debugger;var obj=$("#uploadFile")[0].files;var formData = new FormData(); // 創建一個form類型的數據formData.append('file',obj[0]); // 獲取上傳文件的數據$.ajax({"url": "","type": "","processData": false, // 將數據轉換成對象,不對數據做處理,故 processData: false"contentType": false, // 不設置數據類型"xhrFields": { // 這樣在請求的時候會自動將瀏覽器中的cookie發送給后臺withCredentials: true},"data": formData,success: function(data) {console.log(data)},error: function(data) {}})})

后端代碼

// 取得上傳的文件MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("file");// 得到文件名稱String realFileName = file.getOriginalFilename();String suffix = realFileName.substring(realFileName.indexOf("."),realFileName.length());Workbook workbook = null;//判斷文件類型(可忽略)if (".xlsx".equals(suffix)) {workbook = new XSSFWorkbook(file.getInputStream());} else {workbook = new HSSFWorkbook(file.getInputStream());}List<WGxZdywCrm> empList = getEmpList(workbook);message=JSONObject.fromObject(empList);

后端可以將獲取的文件傳到服務器,也可以將文件內容解析出來。

文件內容解析(excel文件)放入對象

List<WGxZdywCrm> wGxZdywCrmList = new ArrayList<>();SimpleDateFormat dateFormatUtil=new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");Sheet sheet = wookbook.getSheetAt(0);//統計excel的行數int rowLen = sheet.getPhysicalNumberOfRows();//excel總行數,記錄數=行數-1for (int i = 1; i < rowLen; i++) {WGxZdywCrm wGxZdywCrm = new WGxZdywCrm();Row row = sheet.getRow(i);int startCol = 0;if (row != null) {String fZdywSort = getValue(row.getCell(startCol++));//getValue下面貼出來了String fZdywCrmno = getValue(row.getCell(startCol++));String fZdywBusinesscode = getValue(row.getCell(startCol++));String fZdywContractno = getValue(row.getCell(startCol++));String fZdywCustomercount = getValue(row.getCell(startCol++));String fZdywPhonecount = getValue(row.getCell(startCol++));String fZdywAccepttime = getValue(row.getCell(startCol++));String fZdywFinishtime = getValue(row.getCell(startCol++));String fZdywNote = getValue(row.getCell(startCol++));wGxZdywCrm.setFZdywSort(Long.valueOf(fZdywSort));wGxZdywCrm.setFZdywCrmno(fZdywCrmno);wGxZdywCrm.setFZdywBusinesscode(fZdywBusinesscode);wGxZdywCrm.setFZdywContractno(fZdywContractno);wGxZdywCrm.setFZdywCustomercount(fZdywCustomercount);wGxZdywCrm.setFZdywPhonecount(fZdywPhonecount);wGxZdywCrm.setFZdywAccepttime(dateFormatUtil.parse(fZdywAccepttime));wGxZdywCrm.setFZdywFinishtime(dateFormatUtil.parse(fZdywFinishtime));wGxZdywCrm.setFZdywNote(fZdywNote);wGxZdywCrmList.add(wGxZdywCrm);}}return wGxZdywCrmList;

獲取內容

private String getValue(Cell cell) {if (cell == null)return "";if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {return String.valueOf(cell.getBooleanCellValue()).trim();} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {DecimalFormat df = new DecimalFormat("#");return String.valueOf(df.format(cell.getNumericCellValue())).trim();} else {cell.setCellType(cell.CELL_TYPE_STRING);return String.valueOf(cell.getStringCellValue()).trim();}}

總結

以上是生活随笔為你收集整理的jquery传输文件到后端,后端处理数据。的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。