前端文件上传及后端接收(el-upload标签的使用)

时间:2024-10-07 13:52:52
package com.han.sale.handler; import com.alibaba.fastjson.JSONArray; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.han.common.entity.ResponseBean; import com.han.common.entity.StatusCode; import com.han.common.util.ReadExcel; import com.han.common.util.Word; import com.han.sale.entity.ExportList; import com.han.sale.service.CodeNumberService; import com.han.sale.service.ExportListService; import com.han.sale.service.OrderGoodsService; import com.han.sale.service.OrderService; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.util.List; @Controller @RequestMapping("export") public class ExportListHandler { @Resource(name = "orderService") private OrderService orderService; @Resource(name = "orderGoodsService") private OrderGoodsService orderGoodsService; @Resource(name = "exportListService") private ExportListService exportListService; @Resource(name = "codeNumberService") private CodeNumberService codeNumberService; @GetMapping("query") public ResponseEntity query(ExportList exportList, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) { PageHelper.startPage(pageNum, pageSize); if ("全部".equals(exportList.getGoodsstatus())) exportList.setGoodsstatus(null); List<ExportList> lists = exportListService.getAllBy(exportList); PageInfo<ExportList> pageInfo = new PageInfo<>(lists); return ResponseEntity.ok(new ResponseBean(StatusCode.OPERATE_SUCCESS, pageInfo)); } @GetMapping("update") public ResponseEntity update(ExportList exportList) { exportList.setCreatetime(null); int i = exportListService.updateByPrimaryKeySelective(exportList); if (i > 0) return ResponseEntity.ok(new ResponseBean(StatusCode.OPERATE_SUCCESS, null)); else return ResponseEntity.ok(new ResponseBean(StatusCode.OPERATE_FAILED, null)); } @GetMapping("check") public ResponseEntity check() { List<ExportList> exportLists = exportListService.getAll(); //遍历所有的出库单,根据IMEI,将商品状态同步为order_goods表中对应的商品状态 for (ExportList exportList : exportLists) { ExportList list = new ExportList(); list.setExportid(exportList.getExportid()); list.setGoodsstatus(orderGoodsService.selectByIMEI(exportList.getImei()).getGoodsstatus()); exportListService.updateByPrimaryKeySelective(list); } return ResponseEntity.ok(new ResponseBean(StatusCode.OPERATE_SUCCESS, null)); } @Resource private Word word; @PostMapping("generate") public ResponseEntity generate(@RequestBody String str) throws Exception { List<ExportList> lists = JSONArray.parseArray(str, ExportList.class); ExportListHandler.NAME = word.export(lists); return ResponseEntity.ok(new ResponseBean(StatusCode.OPERATE_SUCCESS, null)); } // E:\maven\shopping\target\shopping\WEB-INF\\download\出库单 private static String NAME; @GetMapping("download") public HttpServletResponse download(HttpServletResponse response) throws Exception { File file = new File(ExportListHandler.NAME); String name = file.getName(); BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); byte[] bytes = new byte[inputStream.available()]; inputStream.read(bytes); inputStream.close(); // 清空response response.reset(); // 设置response的Header response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(new String(name.getBytes()), "UTF-8")); response.addHeader("Content-Length", "" + file.length()); BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); outputStream.write(bytes); outputStream.flush(); outputStream.close(); //删除服务器上的临时文件 file.delete(); return response; } private static String UPLOAD_PATH; @Resource private ReadExcel readExcel; @PostMapping("upload") public ResponseEntity upload(@RequestParam MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception { // tb_export.xlsx String filename = file.getOriginalFilename(); // E:\maven\shopping\target\shopping\\upload\ String upload = request.getServletContext().getRealPath("upload/"); File fileDir = new File(upload); File[] files = fileDir.listFiles(); for (File f : files) { f.delete(); } String path = upload + filename; File filePath = new File(path); ExportListHandler.UPLOAD_PATH = path; BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath)); outputStream.write(file.getBytes()); outputStream.flush(); outputStream.close(); //遍历读取Excel readExcel.read(upload); return null; } }