SpringBoot 文件或圖片上傳與下載功能的實(shí)現(xiàn)
導(dǎo)入依賴(pom.xml)
<!-- 上傳下載需要設(shè)計(jì)到的jar包 --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency> <!--servlet-api導(dǎo)入高版本的--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> </dependency> <!-- 圖片處理類 --> <dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
全局配置 application.properties
# 上傳文件大小 spring.servlet.multipart.max-file-size=5MB spring.servlet.multipart.max-request-size=5MB
創(chuàng)建 WebMvcConfig 配置類 靜態(tài)資源映射
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
ApplicationHome h = new ApplicationHome(getClass());
File jarF = h.getSource();
String dirPath = jarF.getParentFile().toString()+"/upload/";
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("win")) { //如果是Windows系統(tǒng)
registry.addResourceHandler("/upload/**").addResourceLocations("file:"+dirPath);
} else {
registry.addResourceHandler("/upload/**").addResourceLocations("file:"+dirPath);
}
}
}
文件或圖片上傳
控制層
// 上傳文件
@ResponseBody
@RequestMapping("/upload")
public String fileUpload(@RequestParam("files") MultipartFile files) throws IOException {
// // win系統(tǒng) 上傳路徑保存設(shè)置
// // 獲取項(xiàng)目路徑
// File projectPath = new File(ResourceUtils.getURL("classpath:").getPath());
// // 絕對(duì)路徑=項(xiàng)目路徑+自定義路徑
// File pathFile = new File(projectPath.getAbsolutePath(), "static/upload/");
// if (!pathFile.exists()) {
// pathFile.mkdirs();
// }
// //上傳文件地址
// UUID uuid = UUID.randomUUID();
// File serverFile = new File(pathFile, uuid + "_" + files.getOriginalFilename());
// files.transferTo(serverFile);
//
// String imgPath = ("/upload/" + uuid + "_" + files.getOriginalFilename()).replace("\\", "/");
//
// return imgPath;
// Linux服務(wù)器 上傳路徑保存設(shè)置
// 項(xiàng)目路徑 /home/www/
File pathFile = new File("/home/www/upload/");
if (!pathFile.exists()) {
pathFile.mkdirs();
}
//上傳文件地址
UUID uuid = UUID.randomUUID();
File serverFile = new File(pathFile, uuid + "_" + files.getOriginalFilename());
files.transferTo(serverFile);
String imgPath = ("/upload/" + uuid + "_" + files.getOriginalFilename()).replace("\\", "/");
return imgPath;
}
HTML頁面
Ajax 無刷新上傳
<form action="" class="layui-form" enctype="multipart/form-data" method="post"> <input type="hidden" name="blogImg" id="imgPath" value=""> <div class="form-group"> <label>圖片上傳</label> <input type='file' style='margin: 5px;' name='files' required><br> <button type="button" class="layui-btn" id="img_upload">上傳圖片</button> </div> <input type="submit"> </form>
JS
//普通圖片上傳
$('#img_upload').click(function () {
var formData = new FormData();
//獲取選擇的文件
$.each($('input[name="files"]'),function (index,item) {
formData.append("files",item.files[0])
});
//發(fā)送異步請(qǐng)求
$.ajax({
method:'post',
url: '[[@{/user/upload}]]', // 文件上傳接口
data:formData,
processData: false,
contentType:false,
success:function (data) {
//成功返回觸發(fā)的方法
$('#imgPath').val(data);
alert("上傳成功");
},
//請(qǐng)求失敗觸發(fā)的方法
error:function () {
alert("上傳失敗");
}
});
});
文件或圖片下載
控制層
@RequestMapping(value="/download")
public String downloads(HttpServletResponse response ,HttpServletRequest request) throws Exception{
//要下載的圖片地址
String path = request.getServletContext().getRealPath("/upload");
String fileName = "基礎(chǔ)語法.jpg";
//1、設(shè)置response 響應(yīng)頭
response.reset(); //設(shè)置頁面不緩存,清空buffer
response.setCharacterEncoding("UTF-8"); //字符編碼
response.setContentType("multipart/form-data"); //二進(jìn)制傳輸數(shù)據(jù)
//設(shè)置響應(yīng)頭
response.setHeader("Content-Disposition",
"attachment;fileName="+URLEncoder.encode(fileName, "UTF-8"));
File file = new File(path,fileName);
//2、 讀取文件--輸入流
InputStream input=new FileInputStream(file);
//3、 寫出文件--輸出流
OutputStream out = response.getOutputStream();
byte[] buff =new byte[1024];
int index=0;
//4、執(zhí)行 寫出操作
while((index= input.read(buff))!= -1){
out.write(buff, 0, index);
out.flush();
}
out.close();
input.close();
return null;
}
HTML頁面
<a href="/download" rel="external nofollow" >點(diǎn)擊下載</a>
SpringBoot 文件或圖片上傳與下載就可以了
到此這篇關(guān)于SpringBoot 文件或圖片上傳與下載功能的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot 文件上傳與下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot中的文件上傳與下載詳解
- Java實(shí)現(xiàn)大文件的分片上傳與下載(springboot+vue3)
- SpringBoot文件上傳與下載功能實(shí)現(xiàn)詳解
- Axios+Spring?Boot實(shí)現(xiàn)文件上傳和下載
- Spring?Boot實(shí)現(xiàn)文件上傳下載
- SpringBoot上傳和下載文件的原理解析
- springboot+vue實(shí)現(xiàn)文件上傳下載
- 詳解SpringBoot下文件上傳與下載的實(shí)現(xiàn)
- Spring Boot 文件上傳與下載的示例代碼
- SpringBoot 文件上傳和下載的實(shí)現(xiàn)源碼
- springboot 中文件上傳下載實(shí)例代碼
- SpringBoot實(shí)現(xiàn)文件上傳下載功能小結(jié)
- SpringBoot+ruoyi框架文件上傳和下載的實(shí)現(xiàn)
相關(guān)文章
Java Scanner輸入兩個(gè)數(shù)組的方法
今天小編就為大家分享一篇Java Scanner輸入兩個(gè)數(shù)組的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
SpringBoot配置Email發(fā)送功能實(shí)例
本篇文章主要介紹了SpringBoot配置Email發(fā)送功能實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Spring AOP有多少個(gè)通知以及它們的執(zhí)行順序介紹
這篇文章主要介紹了Spring AOP有多少個(gè)通知以及它們的執(zhí)行順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
java原生序列化和Kryo序列化性能實(shí)例對(duì)比分析
這篇文章主要介紹了java原生序列化和Kryo序列化性能實(shí)例對(duì)比分析,涉及Java和kryo序列化和反序列化相關(guān)實(shí)例,小編覺得很不錯(cuò),這里分享給大家,希望給大家一個(gè)參考。2017-10-10
java?MongoDB實(shí)現(xiàn)列表分頁查詢的示例代碼
本文主要介紹了java?MongoDB實(shí)現(xiàn)列表分頁查詢的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
JAVA使用爬蟲抓取網(wǎng)站網(wǎng)頁內(nèi)容的方法
這篇文章主要介紹了JAVA使用爬蟲抓取網(wǎng)站網(wǎng)頁內(nèi)容的方法,實(shí)例分析了java爬蟲的兩種實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

