SpringBoot實現(xiàn)文件上傳并返回url鏈接的示例代碼
檢查依賴
確保pom.xml包含了Spring Boot Web的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
創(chuàng)建Controller
創(chuàng)建公用上傳文件控制器
package com.example.ruijisboot.common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@RestController
public class FileUploadController {
@Autowired
private ServletContext servletContext;
@PostMapping("/upload")
public R handleFileUpload(MultipartFile file) {
System.out.println(file);
if (file.isEmpty()) {
// return "Please select a file to upload.";
return R.error("Please select a file to upload.");
}
try {
// 構(gòu)建上傳目錄的路徑
String uploadDir = servletContext.getRealPath("/upload/test/");
// 確保目錄存在
File dirPath = new File(uploadDir);
if (!dirPath.exists()) {
dirPath.mkdirs();
}
// 構(gòu)建上傳文件的完整路徑
Path path = Paths.get(uploadDir).resolve(file.getOriginalFilename());
System.out.println(path);
// 保存文件
Files.write(path, file.getBytes());
// 構(gòu)建文件在Web應(yīng)用中的URL
String fileUrl = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/upload/test/")
.path(file.getOriginalFilename())
.toUriString();
// return "File uploaded successfully! You can download it from: " + fileUrl;
return R.success(fileUrl,"成功");
} catch (IOException e) {
e.printStackTrace();
// return "File upload failed!";
return R.error("File upload failed!");
}
}
}
這里R為我本地封裝的統(tǒng)一返回格式的類
配置屬性
在application.properties或application.yml中,你可以配置一些與文件上傳相關(guān)的屬性,比如文件大小限制等。
# application.properties 示例 spring.servlet.multipart.max-file-size=128KB spring.servlet.multipart.max-request-size=10MB
驗證
請求 /upload 路徑

文件會默認放在系統(tǒng)的臨時文件目錄
到此這篇關(guān)于SpringBoot實現(xiàn)文件上傳并返回url鏈接的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot文件上傳并返回url內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決java.lang.ClassNotFoundException: com.mysql.cj.jdbc.D
這篇文章主要介紹了解決java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
SpringBoot3集成Calcite多數(shù)據(jù)源查詢的實戰(zhàn)示例小結(jié)
本文介紹了Spring Boot 3集成Apache Calcite實現(xiàn)多數(shù)據(jù)源查詢的實戰(zhàn)指南,本文通過實例代碼給大家講解的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2026-02-02
SpringBoot實現(xiàn)定時任務(wù)的三種方式小結(jié)
這篇文章主要介紹了SpringBoot實現(xiàn)定時任務(wù)的三種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
使用springboot對外部靜態(tài)資源文件的處理操作
這篇文章主要介紹了使用springboot對外部靜態(tài)資源文件的處理操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
詳解自動注冊Gateway網(wǎng)關(guān)路由配置
這篇文章主要為大家介紹了自動注冊Gateway網(wǎng)關(guān)路由配置的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
Spring Core動態(tài)代理的實現(xiàn)代碼
通過JDK的Proxy方式或者CGLIB方式生成代理對象的時候,相關(guān)的攔截器已經(jīng)配置到代理對象中去了,接下來通過本文給大家介紹Spring Core動態(tài)代理的相關(guān)知識,需要的朋友可以參考下2021-10-10

