|  |  |  | 
|---|
|  |  |  | package com.zy.common.web; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.excel.EasyExcel; | 
|---|
|  |  |  | import com.zy.common.utils.excel.EnInitData; | 
|---|
|  |  |  | import com.zy.common.utils.excel.matcode.MatCodeExcelListener; | 
|---|
|  |  |  | import com.zy.common.utils.excel.ExcelUtils; | 
|---|
|  |  |  | import com.core.common.BaseRes; | 
|---|
|  |  |  | import com.core.common.Cools; | 
|---|
|  |  |  | import com.core.common.R; | 
|---|
|  |  |  | import org.springframework.stereotype.Controller; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.GetMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.PostMapping; | 
|---|
|  |  |  | import com.zy.common.config.AdminInterceptor; | 
|---|
|  |  |  | import com.zy.common.utils.BarcodeUtils; | 
|---|
|  |  |  | import com.zy.common.utils.QrCode; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.ResponseBody; | 
|---|
|  |  |  | import org.springframework.web.multipart.MultipartFile; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RequestParam; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RestController; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.imageio.ImageIO; | 
|---|
|  |  |  | import javax.servlet.http.HttpServletResponse; | 
|---|
|  |  |  | import java.awt.image.BufferedImage; | 
|---|
|  |  |  | import java.io.IOException; | 
|---|
|  |  |  | import java.net.URLEncoder; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * todo | 
|---|
|  |  |  | * Created by vincent on 2019-11-25 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Controller | 
|---|
|  |  |  | @RequestMapping("excel/") | 
|---|
|  |  |  | @RestController | 
|---|
|  |  |  | @RequestMapping("file/") | 
|---|
|  |  |  | public class FileController { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 文件上传 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @PostMapping("upload") | 
|---|
|  |  |  | @ResponseBody | 
|---|
|  |  |  | public R upload(MultipartFile file) throws IOException { | 
|---|
|  |  |  | EasyExcel.read(file.getInputStream(), EnInitData.class, new MatCodeExcelListener()).sheet().doRead(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @RequestMapping(value = "/barcode/qrcode/auth") | 
|---|
|  |  |  | //    @ManagerAuth(memo = "商品编号条形码获取(type:1(条形码);2(二维码)") | 
|---|
|  |  |  | public R matCodeBarcode(@RequestParam(defaultValue = "1") Integer type | 
|---|
|  |  |  | , @RequestParam String param | 
|---|
|  |  |  | , @RequestParam(required = false) Integer width | 
|---|
|  |  |  | , @RequestParam(required = false) Integer height | 
|---|
|  |  |  | , HttpServletResponse response) throws Exception { | 
|---|
|  |  |  | AdminInterceptor.cors(response); | 
|---|
|  |  |  | if (Cools.isEmpty(param)){ | 
|---|
|  |  |  | return R.parse(BaseRes.EMPTY); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BufferedImage img; | 
|---|
|  |  |  | if (type == 1) { | 
|---|
|  |  |  | img = BarcodeUtils.encode(param, width, height); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | img = QrCode.createImg(param, width, height); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!ImageIO.write(img, "jpg", response.getOutputStream())) { | 
|---|
|  |  |  | throw new IOException("Could not write an image of format jpg"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | response.getOutputStream().flush(); | 
|---|
|  |  |  | response.getOutputStream().close(); | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 文件下载(失败了会返回一个有部分数据的Excel) | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @GetMapping("download") | 
|---|
|  |  |  | public void download(HttpServletResponse response) throws IOException { | 
|---|
|  |  |  | // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman | 
|---|
|  |  |  | response.setContentType("application/vnd.ms-excel"); | 
|---|
|  |  |  | response.setCharacterEncoding("utf-8"); | 
|---|
|  |  |  | // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系 | 
|---|
|  |  |  | String fileName = URLEncoder.encode("测试", "UTF-8"); | 
|---|
|  |  |  | response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); | 
|---|
|  |  |  | EasyExcel.write(response.getOutputStream(), EnInitData.class).sheet("模板").doWrite(ExcelUtils.data()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|