Spring boot 访问template文件夹下的html

第一步:

创建html文件,格式:

<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
</head>
<body>
	 点击切换语言:
    <a href="?lang=zh_CN">简体中文</a> &nbsp;&nbsp;
    <a href="?lang=en_US">English(US)</a><br/>
    <p>
        <!-- <h2 th:text="#{welcome}"></h2> -->
    </p>
</body>
</html>

xmlns不能少,否则会显示不到该界面

第二步:

Controller类必须使用@Controller注解,不能是RestController

第三步:

控制器中return的index.html 只需要index即可,不要写.html

@org.springframework.stereotype.Controller
@RequestMapping("user")
public class Controller
{

	@Autowired
	private UserService userService;

	@RequestMapping("list")
	public String queryUserAll(String name,Model model){
		String content = userService.queryUserByName(name).toString();
		
		return "index";
	}
}

第四步/最终

application.properties中可以不用配置#spring.thymeleaf.prefix=classpath:/templates/  默认就是该文件夹下的内容

访问controller的路径即可,无需项目名字

很好,我能够为您提供您请求的信息。下面是使用spring boot和vue实现分片上传的代码示例: 后端代码: ``` //文件上传 @RequestMapping(value = "/upload", method = RequestMethod.POST) public String upload(HttpServletRequest request) throws Exception { //获取上传的文件 MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file"); //获取保存文件的路径 String path = request.getSession().getServletContext().getRealPath("/uploads/"); //创建文件夹 File uploadDir = new File(path); if (!uploadDir.exists()) { uploadDir.mkdir(); } //分片的序列号 String chunk = request.getParameter("chunk"); //分片上传的总数 String chunks = request.getParameter("chunks"); //分片文件的存储目录 String chunkPath = path + "/" + chunk; File chunkDir = new File(chunkPath); if (!chunkDir.exists()) { chunkDir.mkdir(); } //生成随机的文件名 String fileName = UUID.randomUUID().toString().replace("-", "") + "_" + file.getOriginalFilename(); //保存文件到分片目录 file.transferTo(new File(chunkPath + "/" + fileName)); //将当前分片的序列号和总数保存到redis redisTemplate.opsForValue().set(file.getOriginalFilename() + "_chunk_" + chunk, chunks); //如果这是最后一个分片,将所有分片合并成一个文件 if (Integer.parseInt(chunk) == Integer.parseInt(chunks) - 1) { //创建新的文件 File newFile = new File(path + "/" + fileName); //获取分片目录下的所有文件列表 File[] files = chunkDir.listFiles(); //通过文件名的长度排序,确保分片之后的顺序正确 Arrays.sort(files, Comparator.comparingInt((File f) -> f.getName().length())); //将所有分片合并到新文件中 for (File f : files) { FileOutputStream fos = new FileOutputStream(newFile, true); FileInputStream fis = new FileInputStream(f); byte[] bytes = new byte[1024]; int len = 0; while ((len = fis.read(bytes)) != -1) { fos.write(bytes, 0, len); } fis.close(); fos.close(); f.delete(); //删除已经合并的分片 } //删除分片目录 chunkDir.delete(); //从redis中删除保存的分片信息 redisTemplate.delete(file.getOriginalFilename() + "_chunk_*"); } return fileName; } ``` 前端代码: ``` <template> <div> <input type="file" @change="handleFileChange"> <button @click="handleUpload">上传</button> </div> </template> <script> export default { data() { return { file: null } }, methods: { handleFileChange(e) { this.file = e.target.files[0]; }, handleUpload() { let chunkSize = 1 * 1024 * 1024; //每个分片的大小为1MB let fileSize = this.file.size; let chunks = Math.ceil(fileSize / chunkSize); //需要上传的总分片数 let currentChunk = 0; //当前上传的分片序号 let fileReader = new FileReader(); //文件读取器 fileReader.onload = (e) => { let blob = new Blob([e.target.result]); //将数据转换为二进制数据 let xhr = new XMLHttpRequest(); //创建AJAX向后端发送请求 xhr.open('POST', 'http://localhost:8080/upload', true); xhr.setRequestHeader("Content-type", "multipart/form-data"); //设置请求头 xhr.onreadystatechange = () => { if (xhr.readyState === 4 && xhr.status === 200) { console.log(xhr.responseText); } } let formData = new FormData(); //创建表单数据对象,用于向后端发送数据 formData.append("file", blob, this.file.name); //添加文件数据 formData.append("chunk", currentChunk); //添加当前分片的序号 formData.append("chunks", chunks); //添加分片的总数 xhr.send(formData); //发送请求 currentChunk++; //分片序号加1 if (currentChunk < chunks) { //如果还有分片需要上传,继续上传下一分片 loadNext(); } } let loadNext = () => { let start = currentChunk * chunkSize; let end = Math.min(start + chunkSize, fileSize); fileReader.readAsArrayBuffer(this.file.slice(start, end)); //读取下一分片的数据 } loadNext(); //开始上传第一分片 } } } </script> ``` 希望这段代码能够对您有所帮助!
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值