springboot整合web(二)-----------文件上传

新建springboot项目

https://blog.csdn.net/qq_43560721/article/details/104653470

项目结构

创建Controller

package cn.xxs.springbootfileupload.controller;
import java.io.File;
import java.io.IOException;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController//当前类下的方法返回值自动做json格式的转换
public class FileUploadController {
    /**
     * 文件上传
     * @throws IOException
     * @throws IllegalStateException
     */
    @RequestMapping("/fileUpload")
    public String fileUpload(MultipartFile file) throws IllegalStateException, IOException{
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("E:/"+file.getOriginalFilename()));
        return "success";
    }
}

编写启动类

package cn.xxs.springbootfileupload;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootfileuploadApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootfileuploadApplication.class, args);
    }

}

在application.properties文件设置上传文件的参数及访问路径权限设置


spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=200MB
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static

下边两个可以不设置,只是有的版本可能不设置会出现以下404,我的是这样子的

 

设置上传页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/fileUpload" method="post" enctype="multipart/form-data">
	<input type="file" name="file"/>
	<br>
	<input type="submit" value="上传">
</form>
</body>
</html>

访问项目

浏览器输入http://localhost:8080/static/upload.html

 

在这里我犯了个错误

偷懒直接如图访问

跳进了浏览器,不注意看就会往下走(注意看就会发现端口号不是8080)

会导致

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值