亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot上傳文件并配置本地資源映射來訪問文件的實(shí)例代碼

 更新時(shí)間:2021年04月23日 10:54:39   作者:weixin_44953227  
這篇文章主要介紹了SpringBoot上傳文件并配置本地資源映射來訪問文件的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.準(zhǔn)備工作

1、新建一個(gè)SpringBoot項(xiàng)目加上web依賴, 所有依賴

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>org.thymeleaf</groupId>
	<artifactId>thymeleaf-spring5</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

2、application.properties

# 上傳文件總的最大值
spring.servlet.multipart.max-request-size=10MB
#spring.servlet.multipart.max-request-size=10KB
# 單個(gè)文件的最大值
spring.servlet.multipart.max-file-size=10MB
#spring.servlet.multipart.max-file-size=10KB

# 文件上傳路徑, 當(dāng)前項(xiàng)目根目錄下的 uploadFile 目錄
pro.uploadPath=uploadFile

3、新建文件上傳的靜態(tài)文件 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>文件上傳</title>
</head>
    <body>
        <h1>文件上傳</h1>
        <form method="post" action="/file/upload" enctype="multipart/form-data">
            <input type="file" name="file"><br>
            <input type="submit" value="提交">
        </form>
    </body>
</html>

2. 配置本地資源映射路徑 addResourceHandlers

package com.pro.conf;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConf implements WebMvcConfigurer {
    @Value("${pro.uploadPath}")
    private String uploadPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(uploadPath + "/**") // 添加訪問路徑
                // file: 當(dāng)前項(xiàng)目根目錄, 映射到真實(shí)的路徑下的 uploadPath(變量)目錄下
                .addResourceLocations("file:" + uploadPath + "/");
    }
}

3.上傳代碼

package com.pro.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class UploadController {
    @Value("${pro.uploadPath}")
    private String uploadPath;

    /**
     * 獲取文件路徑
     * @param request 請(qǐng)求體
     * @param fileName 文件名稱
     * @return 文件路徑
     */
    public String getUploadPath(HttpServletRequest request, String fileName) {
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + uploadPath + "/" + fileName;
    }

    // 文件上傳, 上傳到當(dāng)前項(xiàng)目根目錄下的 uploadFile 目錄
    @RequestMapping("/file/upload")
    public Object upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
        Map<String, Object> map = new HashMap();
        map.put("msg", "上傳成功");

        if (multipartFile.isEmpty()) {
            map.put("msg", "空文件");
            return map;
        }

        // 獲取文件名
        String fileName = multipartFile.getOriginalFilename();
        if ("".equals(fileName)) {
            map.put("msg", "文件名不能為空");
            return map;
        }
        System.out.println("上傳文件原始的名字: " + fileName);

        // 使用uuid生成新文件名
        String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + fileName.substring(fileName.lastIndexOf("."), fileName.length());
        System.out.println("保存的文件的新名字: " + newFileName);

        // 獲取年月日的日期格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());

        // 生成以日期分割的文件路徑
        File readPath = new File(uploadPath + File.separator + format);
        System.out.println("存放的文件夾: " + readPath);
        System.out.println("存放文件的絕對(duì)路徑: " + readPath.getAbsolutePath());
        // 判斷文件夾是否存在
        if (!readPath.isDirectory()) {
            // 創(chuàng)建文件夾
            readPath.mkdirs();
        }

        // 文件真實(shí)的保存路徑
        File file = new File(readPath.getAbsolutePath() + File.separator + newFileName);
        try {
            multipartFile.transferTo(file);

            // 獲取存儲(chǔ)路徑
            String filePath = getUploadPath(request, format + "/" + newFileName);
            map.put("path", filePath);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("msg", "上傳失敗");
        }

        return map;
    }
}

到此這篇關(guān)于SpringBoot上傳文件并配置本地資源映射來訪問文件的文章就介紹到這了,更多相關(guān)SpringBoot上傳文件資源映射內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 最重要布局管理器GridBagLayout的使用方法

    Java 最重要布局管理器GridBagLayout的使用方法

    GridBagLayout是java里面最重要的布局管理器之一,可以做出很復(fù)雜的布局,可以說GridBagLayout是必須要學(xué)好的的,需要的朋友可以了解下
    2012-12-12
  • Spring中的攔截器HandlerInterceptor詳細(xì)解析

    Spring中的攔截器HandlerInterceptor詳細(xì)解析

    這篇文章主要介紹了Spring中的攔截器HandlerInterceptor詳細(xì)解析,HandlerInterceptor 是 Spring 框架提供的一個(gè)攔截器接口,用于在請(qǐng)求處理過程中攔截和處理請(qǐng)求,需要的朋友可以參考下
    2024-01-01
  • spring boot--從controller到DAO操作

    spring boot--從controller到DAO操作

    這篇文章主要介紹了spring boot--從controller到DAO操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java 并發(fā)編程之ThreadLocal詳解及實(shí)例

    Java 并發(fā)編程之ThreadLocal詳解及實(shí)例

    這篇文章主要介紹了Java 并發(fā)編程之ThreadLocal詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • JDK源碼白話解讀之ThreadLocal篇

    JDK源碼白話解讀之ThreadLocal篇

    其實(shí)網(wǎng)上有很多關(guān)于ThreadLocal的文章了,有不少文章也已經(jīng)寫的非常好了。但是很多同學(xué)反應(yīng)還有一些部分沒有講解的十分清楚,還是有一定的疑惑沒有想的十分清楚
    2022-02-02
  • Spring?Security自定義認(rèn)證邏輯實(shí)例詳解

    Spring?Security自定義認(rèn)證邏輯實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于Spring?Security自定義認(rèn)證邏輯的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-01-01
  • Java實(shí)現(xiàn)幾十萬條數(shù)據(jù)插入實(shí)例教程(30萬條數(shù)據(jù)插入MySQL僅需13秒)

    Java實(shí)現(xiàn)幾十萬條數(shù)據(jù)插入實(shí)例教程(30萬條數(shù)據(jù)插入MySQL僅需13秒)

    這篇文章主要給大家介紹了關(guān)于Java如何實(shí)現(xiàn)幾十萬條數(shù)據(jù)插入的相關(guān)資料,30萬條數(shù)據(jù)插入MySQL僅需13秒,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-04-04
  • Spring?Security前后分離校驗(yàn)token的實(shí)現(xiàn)方法

    Spring?Security前后分離校驗(yàn)token的實(shí)現(xiàn)方法

    這篇文章主要介紹了Spring?Security前后分離校驗(yàn)token的方法,本次token生成采取jwt的方式,通過引入jwt依賴文件配置token管理器,對(duì)Spring?Security校驗(yàn)token相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-02-02
  • spring boot security設(shè)置忽略地址不生效的解決

    spring boot security設(shè)置忽略地址不生效的解決

    這篇文章主要介紹了spring boot security設(shè)置忽略地址不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Vert.x運(yùn)行環(huán)境搭建流程圖解

    Vert.x運(yùn)行環(huán)境搭建流程圖解

    這篇文章主要介紹了Vert.x運(yùn)行環(huán)境搭建流程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11

最新評(píng)論