springboot中實現檔案上傳的具體步驟

2021-10-03 12:06:13 字數 3825 閱讀 6613

首先是shiro許可權管理,要配置檔案上傳***

//upload檔案上傳

filterrulemap.put("/upload/**", 「anon」);

@configuration

public

class

shiroconfig/*

** 注入 securitymanager

** @return the security manager

*/@bean

public securitymanager securitymanager()

@bean

public customrealm customrealm()

/** * 開啟shiro aop註解支援. 使用**方式; 所以需要開啟**支援;

** @param securitymanager 安全管理器

* @return 授權advisor

*/@bean

public authorizationattributesourceadvisor authorizationattributesourceadvisor

(securitymanager securitymanager)

}

緊接著就是檔案管理配置類,將訪問的url和檔案路徑寫上去

registry.addresourcehandler("/driver/**").addresourcelocations(「file:」+filepath+"/upload/driver/");

@configuration

public

class

fileconfig

extends

webmvcconfigureradapter")

private string filepath;

@override

public

void

addresourcehandlers

(resourcehandlerregistry registry)

}

接下來便是**實現了,先通過檔案上傳通用類獲取url,儲存到資料庫中

@restcontroller

("/filelibrary"

)@api

(tags =

"檔案管理"

)public

class

filelibrarycontroller

extends

basecontroller

("/editdriver"

)@apioperation

("司機證件修改"

)public object editdriver

(filelibrary filelibrary, multipartfile file)

else

string url = filelibraryservice.

uploadfile

(file, fileprefix, filepathkeyword, urlpathkeyword)

; filelibrary.

setformername

(filename)

; filelibrary.

seturl

(url)

;return filelibraryservice.

updatebyid

(filelibrary)

? resultjson.ok(

):resultjson.

failure

(resultcode.bad_request);}

}

service介面

public

inte***ce

ifilelibraryservice

extends

iservice

檔案上傳通用實現類,用於獲取檔案的url和刪除檔案

@service

public

class

filelibraryserviceimpl

extends

serviceimpl

implements

ifilelibraryservice")

private string filepath;

/** * 上傳檔案

* 例 資料夾位址 /filepath/filepathkeyword/fileprefix_123.jpg

* url訪問路徑 /filepath/urlpathkeyword/fileprefix_123.jpg

* @param file

* @param fileprefix 檔名字首

* @param filepathkeyword 資料夾名(可多個資料夾) 前後不要加下劃線

* @param urlpathkeyword url指向名 前後不要加下劃線

* @return

* @throws exception

*/@override

public string uploadfile

(multipartfile file, string fileprefix, string filepathkeyword, string urlpathkeyword)

//檔案儲存路徑

file files =

newfile

(filepath +file.separator+ filepathkeyword + file.separator);if

(!files.

exists()

)//檔名: 字首 + 日期時間 + 隨機數 + 檔案型別

string targetname = fileprefix +

"_"+ system.

currenttimemillis()

+(int)

((math.

random()

*9+1

)*100000

)+ suffix;

string path = files + file.separator + targetname;

trycatch

(ioexception e)

string url =

(file.separator + urlpathkeyword+ file.separator + targetname)

;return url;

}/**

* 刪除檔案

* @param fileurl

* @param filepathkeyword

* @return

*/@override

public string deletefile

(string fileurl, string filepathkeyword)

}catch

(exception e)

}return

"刪除失敗";}

}

public

inte***ce

extends

檔案上傳的實體,與資料庫中相應的表字段對應

@data

public

class

filelibrary

implements

serializable

}

SpringBoot實現檔案上傳

org.springframework.boot groupid spring boot starter web artifactid dependency thymeleaf 模板依賴 org.springframework.boot groupid spring boot starter thy...

實現springboot多檔案上傳的最佳方案

spingmvc支援檔案上傳,我們通過apach 的 commons fileupload 包的commonsmultipartresolver 去實現了 spingmvc的multipartresolver 本文章的示例是個簡單的多檔案上傳,根據不同的業務自行修改。commons fileuplo...

springboot實現簡單的檔案上傳與回顯

前端頁面 input的file型別可以將上傳檔案的絕對路徑返回給後台。doctype html lang en xmlns th charset utf 8 src script href rel stylesheet titletitle head class container class co...