文章摘要
这篇文章介绍了如何使用Java后端的`@PostMapping`接口实现文件上传功能。文章详细描述了上传路径的保存设置,包括检查上传路径是否存在并创建,以及将文件通过`CommonsMultipartFile`方法直接写入指定路径。此外,文章还记录了上传路径的信息,并通过日志输出展示了上传地址和文件转移的详细信息。文章内容简明扼要地介绍了文件上传的核心逻辑和技术细节。
@PostMapping(“/upload”)
public void fileUpload2(@RequestParam(“file”) CommonsMultipartFile file, HttpServletRequest request) throws IOException {
System.out.println(“走了”);
//上传路径保存设置
String path=request.getServletContext().getRealPath(“/upload”);
File realPath=new File(path);
if (!realPath.exists()) {
realPath.mkdir();
}
//上传文件地址
System.out.println(“上传文件保存地址:” + realPath);
@PostMapping(“/upload”)
public void fileUpload2(@RequestParam(“file”) CommonsMultipartFile file, HttpServletRequest request) throws IOException {
System.out.println(“走了”);
//上传路径保存设置
String path=request.getServletContext().getRealPath(“/upload”);
File realPath=new File(path);
if (!realPath.exists()) {
realPath.mkdir();
}
//上传文件地址
System.out.println(“上传文件保存地址:” + realPath);
//通过CommonsMultipartFile的方法直接写文件(注意这个时候)
file.transferTo(new File(realPath + “/” + file.getOriginalFilename()));
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。