首页>代码>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";
}
}
最近下载更多
微信网友_6377331253415936 LV3
2024年11月5日
微信网友_7134912998903808 LV15
2024年8月29日
1529860026 LV24
2022年9月5日
彬 LV6
2022年6月25日
wwssll LV2
2022年5月9日
heweimin LV13
2022年4月9日
szf123 LV12
2021年11月11日
qazxsw_ LV10
2021年9月17日
qq70081337 LV6
2021年9月10日
build12398 LV18
2021年7月28日
最近浏览更多
citybird LV4
2024年11月20日
微信网友_6377331253415936 LV3
2024年11月5日
bluerstar LV1
2024年10月23日
微信网友_7134912998903808 LV15
2024年8月29日
ma406805131 LV19
2024年6月15日
lllajen
2024年6月9日
暂无贡献等级
WBelong LV8
2024年3月7日
janly989713 LV3
2024年3月4日
weilaizhisheng LV21
2023年12月26日
wnnmmb LV2
2023年11月12日

