Day 29 Springboot基础7 文件上传

application.properties设置

##上传文件
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB
fileUpLoadPath=F://moban//
  • maxFileSize (单个文件大小)
    maxRequestSize(设置总上传的数据大小)---------指定multipart/form-data请求允许的最大大小
    注意MB的大小写------------只能是MB和KB两种类型
  • spring.servlet.multipart.max-file-size=-1(该文件不受限制)
  • 默认情况下,Spring Boot配置Spring MVC,每个文件的最大大小为1MB,最大值为单个请求中的10MB文件数据。
  • 官方配置:

在这里插入图片描述
https://blog.csdn.net/weixin_38389632/article/details/80112944
spring.servlet.multipart.enabled=true (是否支持多部分上传)
spring.servlet.multipart.file-size-threshold=0 (将文件写入磁盘的阈值。值可以使用后缀“MB”或“KB”分别表示兆字节或千字 节。)-------------指定文件将写入磁盘的大小阈值。 默认值为0
spring.servlet.multipart.location= (上传文件的中间位置)
spring.servlet.multipart.resolve-lazily=false (是否在文件访问或参数访问时延迟解析多部分请求)

Controller

package com.zz.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
@RestController
public class FileController {
	//@Value 获取配置文件里面的属性值,并且赋值给下面的属性
 	@Value("${fileUpLoadPath}")
    String filePath;
 	
 	@PostMapping("/testUpload3")
    public void testUploadFile(HttpServletRequest req,MultipartHttpServletRequest multiReq) throws IOException{
        MultipartFile multipartFile= multiReq.getFile("file");
        String filename=multipartFile.getOriginalFilename();
        System.out.println("文件名字:"+multipartFile.getOriginalFilename());
        File file=new File(filePath+filename);
        multipartFile.transferTo(file);
        String username=req.getParameter("username");
        System.out.println("用户名字:"+username);
        
    }


}

其中:

  • @PostMapping("/testUpload3")
    == @RequestMapping(value="/testUpload3",method=RequestMethod.POST)
    @RequestMapping 表示同时支持接收post和get请求。
    • 如果加上method=RequestMethod.POST, 表示只接收post请求
    • 有一个注解可以直接指定,只接收post请求—@PostMapping

MultipartFile

MultipartFile是spring类型,代表HTML中form data方式上传的文件,包含二进制数据+文件名称。
在这里插入图片描述
https://blog.csdn.net/a1191835397/article/details/90951345

File类

File类的源码在Java.io包下
常用方法:

  • String[] list()——查询所有文件列表(包含隐藏的),返回一个字符串类型的数组,这些字符串指定此抽象路径名表示的目录中的文件和目录
  • boolean isFile()——判断实例是否为标准file文件
  • boolean isDictionary()——判断实例是否是一个目录
  • boolean exists()——判断file实例是否存在
  • boolean isHidden()——判断当前文件是否为隐藏文件
  • String getPath()——将当前路径名转换为路径名字符串
  • String getName()——获取路径所表示的最后一个目录(实例)的名字
  • String getParent()——获取当前文件的父目录的路径,如果没有返回null
  • boolean canRead()、canWrite()、canExecute()——判断是否有读、写、执行权限
  • createNewFile()——创建文件(会抛异常,在创建前判断文件是否存在,存在则不会抛异常)
  • mkdir()——创建目录
  • delete()——删除文件
  • deleteOnExit()——延时删除, 在虚拟机终止时,请求删除此抽象路径名表示的文件或目录(Thread.sleep(5000);)
  • String getAbsolutePath()——获取文件的绝对路径名字符串
  • File getAbsoluteFile() ——获取文件的绝对路径名形式
  • String[] list(FilenameFilter filter)—— 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中满足指定过滤器的文件和目录。
  • File[] listFiles(FileFilter filter)—— 返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。
  • File[] listFiles(FilenameFilter filter)—— 返回抽象路径名数组,这些路径名表示此抽象路径名表示的目录中满足指定过滤器的文件和目录。

(原文链接:https://blog.csdn.net/qq_41384351/article/details/88656498

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

</head>
<body>
<form id="formId" action="/f/testUpload3" target="frame1" method="POST" enctype="multipart/form-data">
	<input type="text" name="username"/>
    <input type="file" name="file"/>
    <input type="button" value="提交" onclick="upload()">
</form>

<iframe name="frame1" frameborder="0" height="40"></iframe>

<script type="text/javascript">
    function upload() {
        $("#formId").submit();
    }
</script>

</body>
</html>

猜你喜欢

转载自blog.csdn.net/fggsgnhz/article/details/99670647
今日推荐