最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot圖片上傳和訪問(wèn)路徑映射

 更新時(shí)間:2021年08月20日 11:22:32   作者:Snow、楊  
這篇文章主要為大家詳細(xì)介紹了SpringBoot圖片上傳和訪問(wèn)路徑映射,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

簡(jiǎn)介

做移動(dòng)端對(duì)接,框架用的SpringBoot,接口RESTful,實(shí)現(xiàn)一個(gè)圖片上傳功能,圖片上傳是個(gè)經(jīng)典的應(yīng)用場(chǎng)景了,完成后,做個(gè)筆記記錄一下,希望能幫到攻城獅們

開(kāi)發(fā)步驟

1、先貼圖片上傳工具類

package com.prereadweb.utils;
 
import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;
 
/**
 * @Description: 文件工具類
 * @author: Yangxf
 * @date: 2019/4/17 16:06
 */
public class FileTool {
 
  /**
   * @Function: 圖片上傳
   * @author:  YangXueFeng
   * @Date:   2019/4/18 14:13
   */
  public static void uploadFiles(byte[] file, String filePath, String fileName) throws Exception {
    File targetFile = new File(filePath);
    if (!targetFile.exists()) {
      targetFile.mkdirs();
    }
    FileOutputStream out = new FileOutputStream(filePath + fileName);
    out.write(file);
    out.flush();
    out.close();
  }
 
  /**
   * @Function: 創(chuàng)建新的文件名
   * @author:  YangXueFeng
   * @Date:   2019/4/17 17:57
   */
  public static String renameToUUID(String fileName) {
    return UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
  }
}

2、contoller層

/**
   * @Function: 用戶圖片上傳
   * @author:  Yangxf
   * @Date:   2019/4/17 15:42
   */
  @PostMapping("/postfile")
  public Object fileUpload(@RequestParam(value = "userImg", required = false) MultipartFile file, @RequestParam(value = "userId", required = false) Long userId) {
    return personalService.fileUpload(file, userId);
  }

此處提一下@RequestParam注解

value:前臺(tái)所傳參數(shù)的名稱

required:它有兩個(gè)參數(shù),true/false,默認(rèn)是true,如果設(shè)置的是true的,客戶端如果傳值為空的話,訪問(wèn)此接口會(huì)報(bào)500異常,如果是false的話,客戶端傳值為空,會(huì)默認(rèn)給參數(shù)賦值null

3、service層

/**
   * @Function: 用戶頭像上傳
   * @author:  YangXf
   * @Date:   2019/4/17 15:41
   * @param:  file 圖片
   * @param:  userId 用戶ID
   * @return: map
   */
  @Override
  public Map<String, Object> fileUpload(MultipartFile file, Long userId) {
    Map<String, Object> map = new HashMap<>();
    if (Util.isEmpty(file)) {
      System.out.println("文件為空空");
      map.put("code", UserStatusEnum.EMPTY.intKey());
      map.put("msg", UserStatusEnum.EMPTY.value());
      return map;
    }
    UserEntity user = userMapper.fetchUser(userId);
    if(Util.isEmpty(user)){
      map.put("code", UserStatusEnum.USER_NOT_EXISTENCE.intKey());
      map.put("msg", UserStatusEnum.USER_NOT_EXISTENCE.value());
      return map;
    }
 
    String fileName = file.getOriginalFilename();
    fileName = FileTool.renameToUUID(fileName);
    try {
      FileTool.uploadFiles(file.getBytes(), uploadConfig.getUploadPath(), fileName);
    } catch (Exception e) {
    }
    if (Util.isEmpty(fileName)) {
      map.put("code", UserStatusEnum.USER_NOT_EXISTENCE.intKey());
      map.put("msg", UserStatusEnum.USER_NOT_EXISTENCE.value());
      return map;
    }
 
    Map<String, Object> returnMap = new HashMap<>();
    String url = "/static/" + fileName;
    updateUrl(userId, url);
    returnMap.put("imageUrl", url);
    map.put("code", UserStatusEnum.SUCCESS.intKey());
    map.put("msg", UserStatusEnum.SUCCESS.value());
    map.put("data", returnMap);
    return map;
  }

4、設(shè)置圖片訪問(wèn)路徑映射

preread: 
   #文件上傳目錄(注意Linux和Windows上的目錄結(jié)構(gòu)不同) 
   uploadPath: E:/image/

5、配置文件上傳路徑

package com.prereadweb.config.upload;
 
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
/**
 * @Description: 文件上傳路徑配置
 * @author: Yangxf
 * @date: 2019/4/17 17:50
 */
@Component
@ConfigurationProperties(prefix="preread")
public class PreReadUploadConfig {
 
  //上傳路徑
  private String uploadPath;
 
  public String getUploadPath() {
    return uploadPath;
  }
 
  public void setUploadPath(String uploadPath) {
    this.uploadPath = uploadPath;
  }
}

6、配置映射路徑

package com.prereadweb.config.upload;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 
/**
 * @Description: 自定義資源映射
 * <p>
 *   設(shè)置虛擬路徑,訪問(wèn)絕對(duì)路徑下資源
 * </p>
 * @author: Yangxf
 * @date: 2019/4/17 18:17
 */
@ComponentScan
@Configuration
public class WebConfigurer extends WebMvcConfigurerAdapter {
 
  @Autowired
  PreReadUploadConfig uploadConfig;
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/static/**").addResourceLocations("file:///"+uploadConfig.getUploadPath());
  }
}

7、此處需要導(dǎo)入一個(gè)jar報(bào)

<!-- 配置 -->
 <dependency>
  <groupId> org.springframework.boot </groupId>
  <artifactId> spring-boot-configuration-processor </artifactId>
  <optional> true </optional>
</dependency>

8、postman測(cè)試接口

9、此時(shí)配置完成

圖片的存儲(chǔ)路徑在:E:/image/

訪問(wèn)路徑:http://127.0.0.1:8080/static/0fa7481d-4fec-42c3-a716-514feff73707.jpg

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決spring 處理request.getInputStream()輸入流只能讀取一次問(wèn)題

    解決spring 處理request.getInputStream()輸入流只能讀取一次問(wèn)題

    這篇文章主要介紹了解決spring 處理request.getInputStream()輸入流只能讀取一次問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • springsecurity 基本使用詳解

    springsecurity 基本使用詳解

    這篇文章主要介紹了springsecurity 基本使用,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • SpringBoot如何通過(guò)Feign調(diào)用傳遞Header中參數(shù)

    SpringBoot如何通過(guò)Feign調(diào)用傳遞Header中參數(shù)

    這篇文章主要介紹了SpringBoot通過(guò)Feign調(diào)用傳遞Header中參數(shù),本文給大家分享兩種解決方案給大家詳細(xì)講解,需要的朋友可以參考下
    2023-04-04
  • JDK1.8源碼下載及idea2021導(dǎo)入jdk1.8源碼的詳細(xì)步驟

    JDK1.8源碼下載及idea2021導(dǎo)入jdk1.8源碼的詳細(xì)步驟

    這篇文章主要介紹了JDK1.8源碼下載及idea2021導(dǎo)入jdk1.8源碼的詳細(xì)步驟,在文章開(kāi)頭就給大家分享了JDK1.8源碼下載地址和下載步驟,告訴大家idea2021.1.3導(dǎo)入JDK1.8源碼步驟,需要的朋友可以參考下
    2022-11-11
  • MongoDB 整合SpringBoot舉例介紹

    MongoDB 整合SpringBoot舉例介紹

    這篇文章主要介紹了MongoDB 整合SpringBoot的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2025-05-05
  • maven打包不打lib目錄里面的jar包的解決辦法

    maven打包不打lib目錄里面的jar包的解決辦法

    本文主要介紹了maven打包不打lib目錄里面的jar包的解決辦法,解決打包時(shí)第三方j(luò)ar未被包含導(dǎo)致ClassNotFound問(wèn)題,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-05-05
  • 詳解Java的Struts框架中注釋的用法

    詳解Java的Struts框架中注釋的用法

    這篇文章主要介紹了詳解Java的Struts框架中注釋的用法,Struts是Java的SSH三大web開(kāi)發(fā)框架之一,需要的朋友可以參考下
    2015-12-12
  • Java CompletableFuture使用方式

    Java CompletableFuture使用方式

    這篇文章主要介紹了Java CompletableFuture使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Java基于ServletContextListener實(shí)現(xiàn)UDP監(jiān)聽(tīng)

    Java基于ServletContextListener實(shí)現(xiàn)UDP監(jiān)聽(tīng)

    這篇文章主要介紹了Java基于ServletContextListener實(shí)現(xiàn)UDP監(jiān)聽(tīng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Jersey框架的統(tǒng)一異常處理機(jī)制分析

    Jersey框架的統(tǒng)一異常處理機(jī)制分析

    初學(xué)者往往不清楚java的異常為什么會(huì)設(shè)計(jì)成這個(gè)樣子,他們通常會(huì)對(duì)異常只進(jìn)行簡(jiǎn)單的處理
    2016-07-07

最新評(píng)論

广汉市| 东乌| 仙游县| 澄城县| 商水县| 兰州市| 崇阳县| 新化县| 铅山县| 古蔺县| 射洪县| 波密县| 新昌县| 嘉兴市| 京山县| 景泰县| 永年县| 棋牌| 牟定县| 宣武区| 临泽县| 花垣县| 灵台县| 奇台县| 仲巴县| 本溪市| 佛冈县| 会东县| 喀喇沁旗| 额济纳旗| 重庆市| 巩留县| 新建县| 蓬安县| 买车| 故城县| 常宁市| 清苑县| 高雄县| 眉山市| 临泽县|