spring boot實現(xiàn)上傳圖片并在頁面上顯示及遇到的問題小結(jié)
最近在使用spring boot搭建網(wǎng)站的過程之中遇到了這樣一個問題:用戶注冊時需要上傳一個屬于自己的頭像,注冊成功之后跳轉(zhuǎn)到個人中心,在個人中心中顯示用戶信息.其中在顯示頭像的時候遇到了問題:上傳頭像的時候,我把頭像存放到了項目文件下的static文件夾中,將其地址存放到了數(shù)據(jù)庫對應(yīng)的用戶中,并且在idea中添加了熱部署,但是在注冊跳轉(zhuǎn)到個人中心后還是無法顯示頭像,只有在下一次啟動項目進(jìn)入到個人中心時才可以。
被這個問題困擾了許久,最后是這樣的解決的:在main目錄下新建了一個webapp文件夾,并且對其路徑進(jìn)行了配置。下面是一個解決方案的小demo,做的比較簡單,請見諒~~核心代碼如下:
注冊界面:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Title</title> </head> <body> <form action="/zhuce" th:action="@{/zhuce}" method="post" enctype="multipart/form-data" > <label>姓名</label><input type="text" name="name"/> <label>密碼</label><input type="password" name="password"/> <label>上傳圖片</label> <input type="file" name="file"/> <input type="submit" value="上傳"/> </form> </body> </html>
control如下:
package com.example.demo.control; import com.example.demo.dao.UserRepository; import com.example.demo.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import java.io.*; /** * Created by 18274 on 2017/8/9. */ @Controller public class Control { @Autowired UserRepository userRepository; @GetMapping(value="/zhuce") public String zhuce(){ return "zhuce"; } @PostMapping(value="/zhuce") public String tijiao(@RequestParam(value="name") String name, @RequestParam(value="password") String password, @RequestParam(value="file")MultipartFile file, Model model) { User user = new User(); user.setUsername(name); user.setPassword(password); if (!file.isEmpty()) { try { BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(new File("f:\\旗杯\\demo5\\src\\main\\webapp\\"+name+".jpg")));//保存圖片到目錄下 out.write(file.getBytes()); out.flush(); out.close(); String filename="f:\\旗杯\\demo5\\src\\main\\webapp\\"+name+".jpg"; user.setTupian(filename); userRepository.save(user);//增加用戶 } catch (FileNotFoundException e) { e.printStackTrace(); return "上傳失敗," + e.getMessage(); } catch (IOException e) { e.printStackTrace(); return "上傳失敗," + e.getMessage(); } model.addAttribute(user); return "permanager"; } else { return "上傳失敗,因為文件是空的."; } } }
個人中心:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"/> <title>Title</title> </head> <body> <p>用戶名:</p> <p th:text="${user.username}"></p> <p>圖片:</p> <img th:src="@{${user.username}+'.jpg'}"/> </body> </html>
對webapp路徑的配置
package com.example.demo.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /** * Created by 18274 on 2017/8/9. */ @Configuration public class MyWebAppConfigurer extends WebMvcConfigurerAdapter{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/src/main/webapp/**").addResourceLocations("classpath:/webapp/"); super.addResourceHandlers(registry); } }
對應(yīng)的用戶實體類:
package com.example.demo.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; /** * Created by 18274 on 2017/8/9. */ @Entity public class User { @Id @GeneratedValue private Long id; private String username; private String password; private String tupian;//圖片地址 public User(){} public Long getId() { return id; } public String getUsername() { return username; } public String getPassword() { return password; } public String getTupian() { return tupian; } public void setId(Long id) { this.id = id; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setTupian(String tupian) { this.tupian = tupian; } }
用戶實體類的接口:
package com.example.demo.dao; import com.example.demo.domain.User; import org.springframework.data.jpa.repository.JpaRepository; /** * Created by 18274 on 2017/8/9. */ public interface UserRepository extends JpaRepository<User,Long>{ }
最后運行如下:
注冊上傳頭像:
個人中心:
ps:如果在結(jié)合spring security的話,只需要從session.SPRING_SECURITY_CONTEXT.authentication.principal.XXX中取得信息即可。
附上傳的這個小demo的地址:
http://xiazai.jb51.net/201712/yuanma/demo5(jb51.net).rar
總結(jié)
以上所述是小編給大家介紹的spring boot實現(xiàn)上傳圖片并在頁面上顯示及遇到的問題小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
java批量下載將多個文件(minio中存儲)壓縮成一個zip包代碼示例
在Java應(yīng)用程序中有時我們需要從多個URL地址下載文件,并將這些文件打包成一個Zip文件進(jìn)行批量處理或傳輸,這篇文章主要給大家介紹了關(guān)于java批量下載將多個文件(minio中存儲)壓縮成一個zip包的相關(guān)資料,需要的朋友可以參考下2023-11-11Quartz定時任務(wù)管理方式(動態(tài)添加、停止、恢復(fù)、刪除定時任務(wù))
這篇文章主要介紹了Quartz定時任務(wù)管理方式(動態(tài)添加、停止、恢復(fù)、刪除定時任務(wù)),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12加速spring/springboot應(yīng)用啟動速度詳解
這篇文章主要介紹了加速spring/springboot應(yīng)用啟動速度詳解,本文通過實例代碼給大家介紹的非常詳細(xì)對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07圖解Java經(jīng)典算法冒泡排序的原理與實現(xiàn)
冒泡排序是一種簡單的排序算法,它也是一種穩(wěn)定排序算法。其實現(xiàn)原理是重復(fù)掃描待排序序列,并比較每一對相鄰的元素,當(dāng)該對元素順序不正確時進(jìn)行交換。一直重復(fù)這個過程,直到?jīng)]有任何兩個相鄰元素可以交換,就表明完成了排序2022-09-09linux系統(tǒng)下查看jdk版本、路徑及配置環(huán)境變量
在Linux系統(tǒng)中,配置JDK環(huán)境變量是非常重要的,它可以讓你在終端中直接使用Java命令,這篇文章主要給大家介紹了關(guān)于linux系統(tǒng)下查看jdk版本、路徑及配置環(huán)境變量的相關(guān)資料,需要的朋友可以參考下2024-01-01JDK生成WebService客戶端代碼以及調(diào)用方式
WebService 是一種跨編程語言和跨操作系統(tǒng)平臺的遠(yuǎn)程調(diào)用技術(shù),下面這篇文章主要給大家介紹了關(guān)于JDK生成WebService客戶端代碼以及調(diào)用方式的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08Java實現(xiàn)的圖片高質(zhì)量縮放類定義與用法示例
這篇文章主要介紹了Java實現(xiàn)的圖片高質(zhì)量縮放類定義與用法,涉及java針對圖片的運算與轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Java8如何利用Lambda快速生成map、多層嵌套map
這篇文章主要介紹了Java8如何利用Lambda快速生成map、多層嵌套map問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09