依赖jar包 :
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<!-- 生成二维码依赖jar包 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
//读取excel文件
public static ExcelReader readData(String path) throws IOException {
File file = FileUtil.file(path);
InputStream is = new FileInputStream(file);
return ExcelUtil.getReader(is, 0);
}
@Test
public void initTest() throws Exception {
ExcelReader excelReader = ReadExcelUtil.readData("E:/log/ud_shop.xlsx");
Sheet sheet = excelReader.getSheet();
// 遍历每一行
for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row == null) {
continue;
}
// 遍历每一行的每一列
ShopVO shop = new ShopVO();
shop.setUserId(userId);
for (int cellIndex = 0; cellIndex < row.getLastCellNum(); cellIndex++) {
Cell cell = row.getCell(cellIndex);
if (cellIndex == 0) {
shop.setName((String) excelReader.readCellValue(cellIndex,rowIndex));
}else if (cellIndex == 1) {
shop.setCoverUrl((String) excelReader.readCellValue(cellIndex,rowIndex));
}else if (cellIndex == 2) {
shop.setLogoUrl((String) excelReader.readCellValue(cellIndex,rowIndex));
}else if (cellIndex == 3) {
shop.setBrief((String) excelReader.readCellValue(cellIndex,rowIndex));
}else {
shop.setAddr((String) excelReader.readCellValue(cellIndex,rowIndex));
}
}
log.info("shop={}",shop);
// shopProvider.insert(shop);
}
}
//生成验证码
@GetMapping("/code")
public void writeCode(HttpServletRequest request, HttpServletResponse response) {
//生成验证码图片
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(250, 100,6,200);
try {
request.getSession().setAttribute("CAPTCHA_KEY", lineCaptcha.getCode());
response.setContentType("image/png");//告诉浏览器输出内容为图片
response.setHeader("Pragma", "No-cache");//禁止浏览器缓存
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expire", 0);
lineCaptcha.write(response.getOutputStream());
log.info("code={}",lineCaptcha.getCode());
} catch (IOException e) {
e.printStackTrace();
}
}
//生成二维码
@GetMapping("/qrCode")
public void generateQrCode(HttpServletResponse response) {
QrConfig config = new QrConfig(300, 300);
// 高纠错级别
config.setErrorCorrection(ErrorCorrectionLevel.H);
// 设置边距,既二维码和背景之间的边距
config.setMargin(3);
// 设置前景色,既二维码颜色
config.setForeColor(Color.WHITE.getRGB());
// 设置背景色
config.setBackColor(Color.BLACK.getRGB());
// 生成二维码到文件,写入流
try {
QrCodeUtil.generate("http://g5zdsc.natappfree.cc/hello/code", config, "jpg",response.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/70932.html