最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring boot實(shí)現(xiàn)上傳圖片并在頁面上顯示及遇到的問題小結(jié)

 更新時(shí)間:2017年12月05日 16:11:35   作者:浮閑  
最近在使用spring boot搭建網(wǎng)站的過程之中遇到了有點(diǎn)小問題,最終解決方案是在main目錄下新建了一個(gè)webapp文件夾,并且對(duì)其路徑進(jìn)行了配置,本文重點(diǎn)給大家介紹spring boot實(shí)現(xiàn)上傳圖片并在頁面上顯示功能,需要的朋友參考下吧

最近在使用spring boot搭建網(wǎng)站的過程之中遇到了這樣一個(gè)問題:用戶注冊(cè)時(shí)需要上傳一個(gè)屬于自己的頭像,注冊(cè)成功之后跳轉(zhuǎn)到個(gè)人中心,在個(gè)人中心中顯示用戶信息.其中在顯示頭像的時(shí)候遇到了問題:上傳頭像的時(shí)候,我把頭像存放到了項(xiàng)目文件下的static文件夾中,將其地址存放到了數(shù)據(jù)庫對(duì)應(yīng)的用戶中,并且在idea中添加了熱部署,但是在注冊(cè)跳轉(zhuǎn)到個(gè)人中心后還是無法顯示頭像,只有在下一次啟動(dòng)項(xiàng)目進(jìn)入到個(gè)人中心時(shí)才可以。

被這個(gè)問題困擾了許久,最后是這樣的解決的:在main目錄下新建了一個(gè)webapp文件夾,并且對(duì)其路徑進(jìn)行了配置。下面是一個(gè)解決方案的小demo,做的比較簡單,請(qǐng)見諒~~核心代碼如下:

注冊(cè)界面:

<!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 "上傳失敗,因?yàn)槲募强盏?";
 }
 }
}

個(gè)人中心:

<!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>

對(duì)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);
 }
}

對(duì)應(yīng)的用戶實(shí)體類:

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;
 }
}

用戶實(shí)體類的接口:

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>{
}

最后運(yùn)行如下:

注冊(cè)上傳頭像:

這里寫圖片描述 

個(gè)人中心:

這里寫圖片描述

ps:如果在結(jié)合spring security的話,只需要從session.SPRING_SECURITY_CONTEXT.authentication.principal.XXX中取得信息即可。

附上傳的這個(gè)小demo的地址:

http://xiazai.jb51.net/201712/yuanma/demo5(jb51.net).rar

總結(jié)

以上所述是小編給大家介紹的spring boot實(shí)現(xiàn)上傳圖片并在頁面上顯示及遇到的問題小結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Java?集合框架掌握?Map?和?Set?的使用(內(nèi)含哈希表源碼解讀及面試??碱})

    Java?集合框架掌握?Map?和?Set?的使用(內(nèi)含哈希表源碼解讀及面試??碱})

    這篇文章主要介紹了Java?集合框架掌握?Map?和?Set?的使用并含有內(nèi)含哈希表源碼解讀及面試??碱},?Map?和?Set?是一種適合動(dòng)態(tài)查找的集合容器或者數(shù)據(jù)結(jié)構(gòu)下面文章詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2021-12-12
  • Jar包如何導(dǎo)入本地maven倉庫

    Jar包如何導(dǎo)入本地maven倉庫

    將本地jar包導(dǎo)入本地maven倉庫,可以通過maven命令-Dfile、-DgroupId、-DartifactId、-Dversion、-Dpackaging指定jar包的詳細(xì)信息,然后執(zhí)行命令即可
    2024-11-11
  • JPA配置詳解之jpaProperties用法

    JPA配置詳解之jpaProperties用法

    這篇文章主要介紹了JPA配置詳解之jpaProperties用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • 使用注解@Validated效驗(yàn)VO參數(shù)是否合規(guī)

    使用注解@Validated效驗(yàn)VO參數(shù)是否合規(guī)

    這篇文章主要為大家介紹了使用注解@Validated效驗(yàn)VO參數(shù)是否合規(guī)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • Spring MVC創(chuàng)建項(xiàng)目踩過的bug

    Spring MVC創(chuàng)建項(xiàng)目踩過的bug

    這篇文章主要介紹了Spring MVC創(chuàng)建項(xiàng)目踩過的bug,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • java并發(fā)等待條件的實(shí)現(xiàn)原理詳解

    java并發(fā)等待條件的實(shí)現(xiàn)原理詳解

    這篇文章主要介紹了java并發(fā)等待條件的實(shí)現(xiàn)原理詳解,還是比較不錯(cuò)的,這里分享給大家,供需要的朋友參考。
    2017-11-11
  • Java struts2請(qǐng)求源碼分析案例詳解

    Java struts2請(qǐng)求源碼分析案例詳解

    這篇文章主要介紹了Java struts2請(qǐng)求源碼分析案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • JVM入門之JVM內(nèi)存結(jié)構(gòu)內(nèi)容詳解

    JVM入門之JVM內(nèi)存結(jié)構(gòu)內(nèi)容詳解

    這篇文章主要介紹了JVM入門之JVM內(nèi)存結(jié)構(gòu)內(nèi)容詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Spring?Boot中@Import三種使用方式實(shí)例詳解

    Spring?Boot中@Import三種使用方式實(shí)例詳解

    這篇文章主要介紹了Spring?Boot中@Import三種使用方式,主要有引入普通類,引入importSelector的實(shí)現(xiàn)類及引入importBeanDefinitionRegister的實(shí)現(xiàn)類,結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • Java實(shí)現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址

    Java實(shí)現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址

    這篇文章主要介紹了如何利用Java語言實(shí)現(xiàn)獲取內(nèi)網(wǎng)的所有IP地址,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定的參考價(jià)值,快跟隨小編一起學(xué)習(xí)一下吧
    2022-06-06

最新評(píng)論

扬州市| 怀柔区| 江安县| 湘阴县| 九台市| 平昌县| 岚皋县| 卢龙县| 江门市| 郓城县| 新巴尔虎右旗| 台湾省| 金沙县| 漯河市| 家居| 泗阳县| 台南县| 濮阳市| 贺兰县| 上杭县| 息烽县| 大兴区| 原平市| 石阡县| 杂多县| 南宫市| 辛集市| 龙口市| 大姚县| 吉木乃县| 寿宁县| 安溪县| 澄江县| 临漳县| 重庆市| 宣武区| 聂荣县| 西城区| 鱼台县| 乐陵市| 车险|