Spring boot實(shí)現(xiàn)上傳文件到本地服務(wù)器
本文實(shí)例為大家分享了Spring boot實(shí)現(xiàn)上傳文件到本地服務(wù)器的具體代碼,供大家參考,具體內(nèi)容如下
引入配置
UploadFileConfig類
@Configuration public class UploadFileConfig { ? ? ? ?@Value("${file.uploadFolder}") ? ? ? ? private String uploadFolder; ? ? ? ? ? @Bean ? ? ? ? MultipartConfigElement multipartConfigElement() { ? ? ? ? ? ? MultipartConfigFactory factory = new MultipartConfigFactory(); ? ? ? ? ? ? factory.setLocation(uploadFolder); ? ? ? ? ? ? //文件最大 ? ? ? ? ? ? factory.setMaxFileSize("20MB"); ? ? ? ? ? ? return factory.createMultipartConfig(); ? ? ? ? } }
UploadFilePathConfig類
@Configuration public class UploadFilePathConfig extends WebMvcConfigurerAdapter { ? ? @Value("${file.staticAccessPath}") ? ? private String staticAccessPath; ? ? @Value("${file.uploadFolder}") ? ? private String uploadFolder; ? ? @Override ? ? public void addResourceHandlers(ResourceHandlerRegistry registry) { ? ? ? ? registry.addResourceHandler(staticAccessPath).addResourceLocations("file:" + uploadFolder); ? ? } }
在application.properties加上傳的配置項(xiàng)
#文件服務(wù)本地rest請(qǐng)求對(duì)外地址 file.staticAccessPath=/upload/** #文件上傳目錄(注意Linux和Windows上目錄結(jié)構(gòu)不同)需要改成你自己的實(shí)際目錄 file.uploadFolder=/Users/leichunhong/Documents/hlp/zx/ #上傳文件本地域名 改寫成你后端服務(wù)的地址和端口 file.upload.nama=http://127.0.0.1:9090 #springBOOt上傳文件大小 # 單個(gè)文件的最大值 spring.servlet.multipart.max-file-size = 10MB # 上傳文件總的最大值 spring.servlet.multipart.max-request-size=100MB
編寫上傳controller
@RestController @RequestMapping("/test") public class UploadController { ? ? ? @Value("${file.uploadFolder}") ? ? private String uploadFolder; ? ? ? @Value("${file.staticAccessPath}") ? ? private String staticAccessPath; ? ? ? @Value("${file.upload.nama}") ? ? private String reurl; ? ? ? @RequestMapping(value = "/upload", method = {RequestMethod.POST, RequestMethod.GET}) ? ? public List<String> upload(HttpServletRequest request) throws MultipartException { ? ? ? ? String path = ""; ? ? ? ? List<String> ksfs = new ArrayList<>(); ? ? ? ? //1 需傳imgUrl 其他值就是傳file文件上傳 ? ? ? ? try { ? ? ? ? ? ? //將當(dāng)前上下文初始化給 ?CommonsMutipartResolver (多部分解析器) ? ? ? ? ? ? CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver( ? ? ? ? ? ? ? ? ? ? request.getSession().getServletContext()); ? ? ? ? ? ? ? //檢查form中是否有enctype="multipart/form-data" ? ? ? ? ? ? if (multipartResolver.isMultipart(request)) { ? ? ? ? ? ? ? ? //將request變成多部分request ? ? ? ? ? ? ? ? MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; ? ? ? ? ? ? ? ? //獲取multiRequest 中所有的文件名 ? ? ? ? ? ? ? ? Iterator<String> iter = multiRequest.getFileNames(); ? ? ? ? ? ? ? ? ? while (iter.hasNext()) { ? ? ? ? ? ? ? ? ? ? //多個(gè)文件上傳 ? ? ? ? ? ? ? ? ? ? List<MultipartFile> fileList = multiRequest.getFiles(iter.next()); ? ? ? ? ? ? ? ? ? ? for (MultipartFile multipartFile : fileList) { ? ? ? ? ? ? ? ? ? ? ? ? String realPath = uploadFolder; ? ? ? ? ? ? ? ? ? ? ? ? String trueFileName = System.currentTimeMillis() + "." + FilenameUtils.getExtension(multipartFile.getOriginalFilename()); ? ? ? ? ? ? ? ? ? ? ? ? // 設(shè)置存放圖片文件的路徑 ? ? ? ? ? ? ? ? ? ? ? ? path = realPath + trueFileName; ? ? ? ? ? ? ? ? ? ? ? ? File file = new File(path); ? ? ? ? ? ? ? ? ? ? ? ? multipartFile.transferTo(file); ? ? ? ? ? ? ? ? ? ? ? ? path = reurl + "/upload/" + trueFileName; ? ? ? ? ? ? ? ? ? ? ? ? ksfs.add(path); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? //單個(gè)文件 ? ? ? ? ? ? ? ? ? ? ? ?/* MultipartFile multipartFile=multiRequest.getFile(iter.next().toString()); ? ? ? ? ? ? ? ? ? ? ? ? if (multipartFile != null) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? excelFile = File.createTempFile(prefix, ".jpg"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? multipartFile.transferTo(excelFile); ? ? ? ? ? ? ? ? ? ? ? ? ? ? path = KSFileSave.uploadFile(prefix, excelFile, "jpg"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ksfs.add(path); ? ? ? ? ? ? ? ? ? ? ? ?}*/ ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } finally { ? ? ? ? } ? ? ? ? return ksfs; ? ? } ? }
調(diào)用接口測(cè)試,用如下的html頁面
<form method="POST" enctype="multipart/form-data" action="http:127.0.0.1:9090/test/upload"> ? ? ? ? <table> ? ? ? ? ? ? <tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr> ? ? ? ? ? ? <tr><td></td><td><input type="submit" value="Upload" /></td></tr> ? ? ? ? </table> </form>
點(diǎn)擊選擇文件上傳驗(yàn)證返回結(jié)果
查看上傳目錄
點(diǎn)擊返回接口訪問
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ArrayList在for循環(huán)中使用remove方法移除元素方法介紹
這篇文章主要介紹了ArrayList在for循環(huán)中使用remove方法移除元素的內(nèi)容,介紹了具體代碼實(shí)現(xiàn),需要的朋友可以參考下。2017-09-09Java可重入鎖的實(shí)現(xiàn)原理與應(yīng)用場(chǎng)景
今天小編就為大家分享一篇關(guān)于Java可重入鎖的實(shí)現(xiàn)原理與應(yīng)用場(chǎng)景,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01SpringBoot基礎(chǔ)教程之集成郵件服務(wù)
這篇文章主要給大家介紹了關(guān)于SpringBoot基礎(chǔ)教程之集成郵件服務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用SpringBoot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07java.sql.SQLRecoverableException關(guān)閉的連接異常問題及解決辦法
當(dāng)數(shù)據(jù)庫連接池中的連接被創(chuàng)建而長(zhǎng)時(shí)間不使用的情況下,該連接會(huì)自動(dòng)回收并失效,就導(dǎo)致客戶端程序報(bào)“ java.sql.SQLException: Io 異常: Connection reset” 或“java.sql.SQLException 關(guān)閉的連接”異常問題,下面給大家分享解決方案,一起看看吧2024-03-03Springboot教程之如何設(shè)置springboot熱重啟
這篇文章主要介紹了Springboot教程之如何設(shè)置springboot熱重啟,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07springboot使用@Slf4j進(jìn)行日志的記錄步驟詳解
這篇文章主要介紹了springboot使用@Slf4j進(jìn)行日志的記錄,使用@Slf4j的注解進(jìn)行日志記錄非常方便,本文給大家分享操作步驟,需要的朋友可以參考下2023-08-08java8 List<Object>去掉重復(fù)對(duì)象的幾種方法
本文主要介紹了java8 List<Object>去掉重復(fù)對(duì)象的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04