日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java工作笔记-Spring Boot上传图片并显示

發(fā)布時(shí)間:2025/3/15 java 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java工作笔记-Spring Boot上传图片并显示 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

?

?

基本概念

代碼與實(shí)例

源碼打包下載


?

?

基本概念

這個(gè)是基因Github的一個(gè)項(xiàng)目改的,因?yàn)轫撁骊P(guān)閉了,在此找不到了,不能把連接發(fā)上來。

這里使用的是FreeMarker。

為了避免重名,使用了UUID生成隨機(jī)。

找圖片文件主要是使用ResourceLoader。

?

代碼與實(shí)例

程序運(yùn)行截圖如下:

點(diǎn)擊選擇文件,然后提交:

看看文件夾:

關(guān)鍵源碼如下:

application.properties

### FreeMarker 配置 spring.freemarker.allow-request-override=false #Enable template caching.啟用模板緩存。 spring.freemarker.cache=false spring.freemarker.check-template-location=true spring.freemarker.charset=UTF-8 spring.freemarker.content-type=text/html spring.freemarker.expose-request-attributes=false spring.freemarker.expose-session-attributes=false spring.freemarker.expose-spring-macro-helpers=false #設(shè)置面板后綴 spring.freemarker.suffix=.ftl# 設(shè)置單個(gè)文件最大內(nèi)存 multipart.maxFileSize=50Mb # 設(shè)置所有文件最大內(nèi)存 multipart.maxRequestSize=50Mb # 自定義文件上傳路徑 web.upload-path=F:/SpringTest/

TestController.java

package com.example.demo.controller;import com.example.demo.util.FileUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ResourceLoader; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile;import java.util.Map;@Controller public class TestController {private final ResourceLoader resourceLoader;public TestController(ResourceLoader resourceLoader) {this.resourceLoader = resourceLoader;}@Value("${web.upload-path}")private String path;@RequestMapping("test")private String toUpload(){return "test";}//上傳文件@RequestMapping("fileUpload")public String upload(@RequestParam("fileName") MultipartFile file, Map<String, Object> map){String localPath = "F:/SpringTest";String msg = "";StringBuffer newName = new StringBuffer();if(FileUtils.upload(file, localPath, file.getOriginalFilename(), newName)){msg = "上傳成功";}else{msg = "上傳失敗";}newName.toString();map.put("msg", msg);//map.put("fileName", file.getOriginalFilename());map.put("fileName", newName.toString());return "forward:/test";}//顯示單張圖片@RequestMapping("show")public ResponseEntity showPhotos(String fileName){if(fileName == null){return ResponseEntity.notFound().build();}try{return ResponseEntity.ok(resourceLoader.getResource("file:" + path + fileName));}catch (Exception e){return ResponseEntity.notFound().build();}} }

?

?

源碼打包下載

地址如下:

https://github.com/fengfanchen/Java/tree/master/loadPic

總結(jié)

以上是生活随笔為你收集整理的Java工作笔记-Spring Boot上传图片并显示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。