SpringBoot---文件上传

本文介绍如何在SpringBoot中实现文件上传功能,包括代码实现、配置设置及项目结构示例。

          有事后我们在做Web应用开发的时候,需要使用需要使用到文件上传和下载。Sping Boot中为我们封装好了文件上传和下载的类,我们直接拿来使用,在进行相应的配置就可以完成上传和下载功能。

 一、Spring Boot 默认使用 springMVC 包装好的解析器进行上传 
 
二、添加代码 

<form method="POST" enctype="multipart/form-data" action="/file/upload">  

        文件:<input type="file" name="roncooFile" /> 

       <input type="submit" value="上传" /> 

    </form> 

 

 

@Controller 

@RequestMapping(value = "/file") 

public class FileController { 

 

 private static final Logger logger = LoggerFactory.getLogger(FileController.class); 

 

 @RequestMapping(value = "upload") 

 @ResponseBody 

 public String upload(@RequestParam("roncooFile") MultipartFile file) { 

  if (file.isEmpty()) { 

   return "文件为空"; 

  } 

 

  // 获取文件名 

  String fileName = file.getOriginalFilename(); 

  logger.info("上传的文件名为:" + fileName); 

 

  // 获取文件的后缀名 

  String suffixName = fileName.substring(fileName.lastIndexOf(".")); 

  logger.info("上传的后缀名为:" + suffixName); 

 

  // 文件上传路径 

  String filePath = "d:/roncoo/ttt/"; 

 

  // 解决中文问题,liunx 下中文路径,图片显示问题 

  // fileName = UUID.randomUUID() + suffixName; 

   

  File dest = new File(filePath + fileName); 

 

  // 检测是否存在目录 

  if (!dest.getParentFile().exists()) {

   dest.getParentFile().mkdirs(); 

  } 

 

  try { 

   file.transferTo(dest); 

   return "上传成功"; 

  } catch (IllegalStateException e) { 

   e.printStackTrace(); 

  } catch (IOException e) { 

   e.printStackTrace(); 

  } 

  return "上传失败"; 

 } 

 

三、配置 (在resource/config)

spring.http.multipart.enabled=true #默认支持文件上传. 

spring.http.multipart.file-size-threshold=0 #支持文件写入磁盘. 

spring.http.multipart.location= # 上传文件的临时目录 

spring.http.multipart.max-file-size=1Mb # 最大支持文件大小 

spring.http.multipart.max-request-size=10Mb # 最大支持请求大小 

 

项目结构大致如下:

FileController.java 文件

package com.nyist.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping(value = "/uploadfile")
public class FileController { 
 
 private static final Logger logger = LoggerFactory.getLogger(FileController.class);
 
     @RequestMapping(value = "upload")
     @ResponseBody
     public String upload(@RequestParam("file") MultipartFile file) {
         if (file.isEmpty()) {
             return "文件为空";
         }

         // 获取文件名
         String fileName = file.getOriginalFilename();
         logger.info("上传的文件名为:" + fileName);

         // 获取文件的后缀名
         String suffixName = fileName.substring(fileName.lastIndexOf("."));
         logger.info("上传的后缀名为:" + suffixName);

         // 文件上传路径
         String filePath = "E:/上传文件/";

         // 解决中文问题,liunx 下中文路径,图片显示问题
         // fileName = UUID.randomUUID() + suffixName;

         File dest = new File(filePath + fileName);

         // 检测是否存在目录
         if (!dest.getParentFile().exists()) {
             dest.getParentFile().mkdirs();
         }

         try {
             file.transferTo(dest);
             return "上传成功";
         } catch (IllegalStateException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
         return "上传失败";
    }

} 

index.ftl文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form method="POST" enctype="multipart/form-data" action="/uploadfile/upload">
    文件:<input type="file" name="file" /><br/>
    <input type="submit" value="上传" />
</form>
</body>
</html>

application.properties

server.port=8888
#默认支持文件上传.
#Spring Boot 1.3.x或者之前
#
#multipart.maxFileSize=100Mb
#multipart.maxRequestSize=1000Mb
#
#Spring Boot 1.4.x

#spring.http.multipart.maxFileSize=100Mb
#spring.http.multipart.maxRequestSize=1000Mb
#
# Spring Boot 2.0之后
#
#spring.servlet.multipart.max-file-size=100Mb
#spring.servlet.multipart.max-request-size=1000Mb
#
spring.servlet.multipart.enabled=true
#支持文件写入磁盘.
spring.servlet.multipart.file-size-threshold=0
#上传文件保存位置
spring.servlet.multipart.location=E:/temp
# 最大支持文件大小
spring.servlet.multipart.max-file-size=100Mb
# 最大支持请求大小
spring.servlet.multipart.max-request-size=100Mb

访问 http://localhost:8888/uploadfile/upload  点击上传上传即可上传到相应路径下的文件文件中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值