采用hutool后端返回文件流供前端下载

1、导入依赖

<!--hutool-->
		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.7.20</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>4.1.2</version>
		</dependency>

2、代码

import java.net.URLDecoder;
import org.apache.commons.io.IOUtils;


    /**
     * 下载房源信息导入模板
     * @param request
     * @param response
     * @throws Exception
     */
    @Override
    public void downLoadPropertyExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String fileName = "房源信息导入模板.xlsx";
        fileName = URLDecoder.decode(fileName, "UTF-8");
        InputStream in = this.getClass().getResourceAsStream("/template/房源信息导入模板.xlsx");
        String filenamedisplay = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=" + filenamedisplay);
        response.setContentType("application/x-download;charset=utf-8");
        OutputStream out = response.getOutputStream();
        IOUtils.copy(in, out);
        out.flush();
        in.close();
    }

3、前端测试

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<!-- 以下两种方式都可 -->
		<button id="downLoad">下载</button>
		<a href="http://127.0.0.1:8055/yxroom/directSeeding/downLoadBuyerExcel">点击下载文件</a>
		
	</body>
	<script>
		document.getElementById('downLoad').onclick = function(){
			alert("开始下载");
			// window.location.href = 'http://127.0.0.1:8055/yxroom/directSeeding/downLoadPropertyExcel';
			window.location.href = 'http://127.0.0.1:8055/yxroom/directSeeding/downLoadBuyerExcel';
		}
	</script>
</html>

可以使用Hutool生成图片验证码,并将验证码图片返回前端,具体步骤如下: 1. 添加Hutool依赖 在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.14</version> </dependency> ``` 2. 编写验证码生成代码 在Controller中编写生成验证码的代码,并将生成的验证码图片以Base64编码的形式返回前端。 ```java @RestController public class CaptchaController { @GetMapping("/captcha") public String captcha(HttpServletResponse response) throws IOException { // 生成验证码 LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 50); // 将验证码图片以Base64编码的形式返回前端 ByteArrayOutputStream bos = new ByteArrayOutputStream(); captcha.write(bos); byte[] bytes = bos.toByteArray(); return "data:image/png;base64," + Base64.getEncoder().encodeToString(bytes); } } ``` 在上述代码中,我们使用了Hutool的`CaptchaUtil`来生成验证码,生成的验证码图片使用`ByteArrayOutputStream`写入内存中,然后将内存中的验证码图片以Base64编码的形式返回前端。 3. 前端页面使用验证码 在前端页面中,我们可以使用`<img>`标签来显示验证码图片,同时在提交表单时,需要将用户输入的验证码与后端生成的验证码进行比较,以确保用户输入的验证码正确。 ```html <div> <label for="captcha">验证码:</label> <input type="text" name="captcha" id="captcha" required> <img id="captchaImage" src="/captcha"> </div> <button type="submit">提交</button> ``` 在上述代码中,我们使用了`<img>`标签来显示验证码图片,同时使用了`<input>`标签来让用户输入验证码。用户提交表单时,需要将用户输入的验证码与后端生成的验证码进行比较,以确保用户输入的验证码正确。 以上就是在SpringBoot前后端分离项目中使用Hutool生成图片验证码,并返回前端的完整步骤。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值