首页>代码>SpringBoot项目,tk.mybatis通用mapper支持,shiro权限管理,以及mybatis分页插件,文件上传实例>/springboot/src/main/java/com/test/springboot/controller/UpFileController.java
package com.test.springboot.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.*; import java.util.UUID; /** * 文件上传 */ @Controller public class UpFileController { @PostMapping(value = "/fileUpload") public String fileUpload(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest request) { if (file.isEmpty()) { System.out.println("文件为空空"); return "index"; } String fileName = file.getOriginalFilename(); // 文件名 String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后缀名 String filePath = "D://temp-rainy//"; // 上传后的路径 fileName = UUID.randomUUID() + suffixName; // 新文件名 File dest = new File(filePath + fileName); // 检测是否存在目录 if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs();//假如文件不存在即重新创建新的文件已防止异常发生 } try { file.transferTo(dest);//transferTo(dest)方法将上传文件写到服务器上指定的文件 } catch (IOException e) { e.printStackTrace(); } String filename = "/temp-rainy/" + fileName; model.addAttribute("filename", filename); return "index"; } }
最近下载更多
1529860026 LV24
2022年9月5日
彬 LV6
2022年6月25日
wwssll LV2
2022年5月9日
heweimin LV12
2022年4月9日
szf123 LV12
2021年11月11日
qazxsw_ LV10
2021年9月17日
qq70081337 LV6
2021年9月10日
build12398 LV18
2021年7月28日
融会贯通 LV3
2021年4月19日
peixizhu LV16
2020年12月26日
最近浏览更多
WBelong LV7
3月7日
janly989713 LV3
3月4日
weilaizhisheng LV21
2023年12月26日
wnnmmb LV2
2023年11月12日
edpwyg LV14
2023年10月21日
微信网友_6658069491355648 LV2
2023年10月15日
漫步的海星 LV4
2023年9月21日
yshysh123
2023年9月20日
暂无贡献等级
lyws1986 LV17
2023年5月24日
Iterman LV2
2023年2月27日