383、Java中级38 -【Spring Boot - 上传文件】 2020.09.25

56 篇文章 1 订阅
17 篇文章 0 订阅

1、 uploadPage.jsp

在jsp目录下新建uploadPage.jsp,需要几点:

  1. method=“post” 是必须的
  2. enctype=“multipart/form-data” 是必须的,表示提交二进制文件
  3. name=“file” 是必须的,和后续服务端对应
  4. accept=“image/*” 表示只选择图片
    在这里插入图片描述
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<form action="upload" method="post" enctype="multipart/form-data">
  选择图片:<input type="file" name="file" accept="image/*" /> <br>
  <input type="submit" value="上传">
</form>
  • UploadController.java

因为uploadPage.jsp 在WEB-INF下,不能直接从浏览器访问,所以要在这里加一个uploadPage跳转,这样就可以通过

http://127.0.0.1:8080/uploadPage

访问到uploadPage.jsp了

package com.how2java.springboot.web;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
  
@Controller
public class UploadController {
  
    @RequestMapping("/uploadPage")
    public String uploadPage() {
        return "uploadPage";
    }
 
}

2、UploadController.java

为UploadController.java 新增upload用来接受上传
1. 接受上传的文件
 
@RequestParam("file") MultipartFile file
 

2. 根据时间戳创建新的文件名,这样即便是第二次上传相同名称的文件,也不会把第一次的文件覆盖了
 
String fileName = System.currentTimeMillis()+file.getOriginalFilename();
 

3. 通过req.getServletContext().getRealPath("") 获取当前项目的真实路径,然后拼接前面的文件名
 
String destFileName=req.getServletContext().getRealPath("")+"uploaded"+File.separator+fileName;
 

4. 第一次运行的时候,这个文件所在的目录往往是不存在的,这里需要创建一下目录
 
File destFile = new File(destFileName);
destFile.getParentFile().mkdirs();
 

5. 把浏览器上传的文件复制到希望的位置
 
file.transferTo(destFile);
 

6. 把文件名放在model里,以便后续显示用
 
m.addAttribute("fileName",fileName);
package com.how2java.springboot.web;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
  
@Controller
public class UploadController {
  
    @RequestMapping("/uploadPage")
    public String uploadPage() {
        return "uploadPage";
    }
     
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload(HttpServletRequest req, @RequestParam("file") MultipartFile file,Model m) {
            try {
                String fileName = System.currentTimeMillis()+file.getOriginalFilename();
                String destFileName=req.getServletContext().getRealPath("")+"uploaded"+File.separator+fileName;
                 
                File destFile = new File(destFileName);
                destFile.getParentFile().mkdirs();
                file.transferTo(destFile);
                 
                m.addAttribute("fileName",fileName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }
             
            return "showImg";
    }   
}

3、application.properties

设置上传文件的大小,默认是1m,太小了,文件稍微大一点就会出错

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb

4、测试

访问测试地址:

http://127.0.0.1:8080/uploadPage

在这里插入图片描述

5、参考链接

[01] How2j - Spring Boot - 上传文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值