springBoot文件上传

本文介绍了一个使用HTML和Java实现的文件上传功能,包括前端HTML表单的设计和后端Java控制器的实现,展示了如何处理文件上传、指定上传路径以及错误处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前端HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
    <meta charset="UTF-8">
    <title>文件上传</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="/file/upload">
    文件:<input type="file" name="file"/>
    <input type="submit" value="上传"/>
</form>
<form enctype="multipart/form-data" method="post" action="/file/uploadPathName">
    文件:<input type="file" name="file"/>
 指定路径:<input type="text" name="pathName"/>
    <input type="submit" value="上传"/>
</form>
</body>
</html>

 后端代码

@RestController
@RequestMapping("/file")
public class FileController  extends BaseController {
	private static final Logger log = LoggerFactory.getLogger(FileController.class);

    @RequestMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file, HttpServletRequest request){
    	HashMap<String, Object> map = new HashMap<>();
        try {
        	//获取项目所在目录路径
        	File path = new File("");
        	String filePath = path.getAbsolutePath()+"/static/";
            // 文件名
            String fileName = file.getOriginalFilename();
            log.info("文件名: " + fileName);

            // 文件后缀
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            log.info("文件后缀名: " + suffixName);

            //创建文件
            File dest = new File(filePath + fileName);
            file.transferTo(dest);
            map.put("success", true);
            return map;
        } catch (IOException e) {
        	map.put("error", e.getMessage());
            e.printStackTrace();
        }
        return map.put("success", false);
    }
    
    @RequestMapping("/uploadPathName")
    public Object uploadPathName(@RequestParam("file") MultipartFile file, HttpServletRequest request,String pathName){
    	HashMap<String, Object> map = new HashMap<>();
        try {
        	File path = new File(pathName);
    		if(!path.exists()) {
    			path.mkdirs();
    		}
            // 文件名
            String fileName = file.getOriginalFilename();
            log.info("文件名: " + fileName);

            // 文件后缀
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            log.info("文件后缀名: " + suffixName);
            //创建文件
            File dest = new File(pathName + fileName);
            file.transferTo(dest);
            map.put("success", true);
            return map;
        } catch (IOException e) {
        	map.put("error", e.getMessage());
            e.printStackTrace();
        }
        return map.put("success", false);
    }
}

application.yml 配置

spring:  
    servlet:
        multipart:
          max-file-size: 100MB
          max-request-size: 100MB

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值