java web的文件上传功能

文件上传

首先将文件上传的界面form_layouts.html放到资源的form目录下,在form_layouts.html引入Thymeleaf命名空间

在controller包下新建一个FormTestController类用来处理文件上传

首先进行简单的设置,实现在main页面中点击form可以跳转到form_layouts.html中

@GetMapping("/form_layouts")
public String form_layouts(){
    return "form/form_layouts";
}

在前端main页面中的指向地址修改

<li><a th:href="@{/form_layouts}"> Form Layouts</a></li>

然后在controller类中处理文件上传

@Slf4j
@PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("username") String username,
                         @RequestPart("headerImg") MultipartFile headerImg,
                         @RequestPart("photos") MultipartFile[] photos) throws IOException {
        log.info("上传的信息:email={},username={},headerImg={},photos={}",
                email,username,headerImg.getSize(),photos.length);

        if (!headerImg.isEmpty()){
            //保存在文件服务器,OSS服务器
            String originalFilename = headerImg.getOriginalFilename();
            headerImg.transferTo(new File("/home/lnnu/dics/files/" + originalFilename));
        }

        if (photos.length > 0){
            for(MultipartFile photo : photos){
                if(!photo.isEmpty()){
                    String originalFilename = photo.getOriginalFilename();
                    photo.transferTo(new File("/home/lnnu/dics/files/" + originalFilename));
                }
            }
        }

        return "main";
    }

上传文件在前端标签中有固定的写法

<form role="form" th:action="@{/upload}" method="post" enctype="multipart/form-data"></form>

上传页面的代码

<form role="form" th:action="@{/upload}" method="post" enctype="multipart/form-data">
<div class="form-group">
                                <label for="exampleInputEmail1">邮箱</label>
                                <input type="email" name="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputPassword1">名字</label>
                                <input type="text" name="username" class="form-control" id="exampleInputPassword1" placeholder="Password">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputFile">头像</label>
                                <input type="file" name="headerImg" id="exampleInputFile">
                            </div>
                            <div class="form-group">
                                <label for="exampleInputFile">照片</label>
                                <input type="file" name="photos" multiple>
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox"> Check me out
                                </label>
                            </div>
                            <button type="submit" class="btn btn-primary">提交</button>
</form>

默认的文件上传大小有限制,可以在.properties配置文件中配置

#单个文件的大小
spring.servlet.multipart.max-file-size=10MB
#总共文件的大小
spring.servlet.multipart.max-request-size=100MB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牧码文

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值