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

關(guān)于Springboot在新增和修改下上傳圖片并顯示的問題

 更新時間:2021年04月27日 11:22:22   作者:找找Bug  
這篇文章主要介紹了關(guān)于Springboot在新增和修改下上傳圖片并顯示的問題及解決方法,在這里 springboot中已經(jīng)內(nèi)嵌了上傳圖片的依賴包,因此不需要再添加額外依賴,具體實現(xiàn)代碼跟隨小編一起看看吧

解決的問題:

本篇文章除了解決上傳圖片并顯示的問題,還有就是在新增和修改下的圖片上傳如何處理。在這里新增和修改的頁面是同一頁面,修改的時候,將會把值帶過去,但是由于類型為file的input標(biāo)簽是不能給其賦值的,那么若不改變原來圖片,但是input標(biāo)簽中又沒有值,這時怎么處理呢?

一 運行環(huán)境

開發(fā)工具:IDEA

后端:Springboot+JPA

前端:thymeleaf+semantic UI

二 代碼實現(xiàn)

springboot中已經(jīng)內(nèi)嵌了上傳圖片的依賴包,因此不需要再添加額外依賴。

1 前端頁面

<form id="blog-form" action="#" th:object="${blog}" th:action="@{/admin/blogs}" method="post" enctype="multipart/form-data" class="ui form">
 
              <!--該部分內(nèi)容省略,以下為重點--> 
 
               <div class="required field">
                    <div class="ui left labeled input">
                        <label class="ui teal basic label">首圖</label>
                        <img src="" th:src="@{*{firstPicture}}" alt="" style="width: 500px !important;">
<!--                        不能給input type="file"文件賦值-->
                        <input type="file" name="picture">
                       <!--<input type="text" name="firstPicture" th:value="*{firstPicture}" placeholder="首圖引用地址">-->
                    </div>
                </div>
 
                <!--該部分內(nèi)容省略,以上為重點--> 
 
                <div class="ui right aligned container">
                    <button type="button" class="ui button" onclick="window.history.go(-1)">返回</button>
                    <button type="button" id="save-btn" class="ui secondary button">保存</button>
                    <button type="button" id="publish-btn" class="ui teal button">發(fā)布</button>
                </div>
            </form>

注意:enctype的值為multipart/form-data

2 創(chuàng)建上傳圖片類——UploadImageUtils

package com.hdq.blog_3.util;
 
 
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.IOException;
import java.util.UUID;
 
public class UploadImageUtils {
 
 
//    文件上傳
    public static String upload(MultipartFile file){
        if(file.isEmpty()){
            return "";
        }
        String fileName = file.getOriginalFilename();//獲取文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));//獲取文件后綴名
       //重命名文件
        fileName = UUID.randomUUID().toString().replaceAll("-","") + suffixName;
//        windows下
//        String filePath="E:/picture/";
        //centos下
        String filePath="/opt/findBugWeb/picture/";
        File dest = new File(filePath+fileName);
        if(!dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }
        try{
            file.transferTo(dest);
        }catch (IOException e){
            e.printStackTrace();
        }
        return filePath.substring(filePath.indexOf("/"))+fileName;
    }
}

3 配置圖片訪問路徑的類——SourceMapperConfig

該類可以指定圖片的訪問路徑。

package com.hdq.blog_3.sourceMapper;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
//配置文件訪問路徑
@Configuration
public class SourceMapperConfig implements WebMvcConfigurer {
 
//    private String fileSavePath = "E:/picture/";
      String fileSavePath="/opt/findBugWeb/picture/";
    /**
     * 配置資源映射
     * 意思是:如果訪問的資源路徑是以“/images/”開頭的,
     * 就給我映射到本機(jī)的“E:/images/”這個文件夾內(nèi),去找你要的資源
     * 注意:E:/images/ 后面的 “/”一定要帶上
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/opt/findBugWeb/picture/**").addResourceLocations("file:"+fileSavePath);
    }
}

4 創(chuàng)建Controller類——BlogController

package com.hdq.blog_3.web.admin;
 
import com.hdq.blog_3.po.Blog;
import com.hdq.blog_3.po.User;
import com.hdq.blog_3.service.BlogService;
import com.hdq.blog_3.service.TagService;
import com.hdq.blog_3.service.TypeService;
import com.hdq.blog_3.util.UploadImageUtils;
import com.hdq.blog_3.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
 
import javax.servlet.http.HttpSession;
 
 
@Controller
@RequestMapping("/admin")
public class BlogController {
 
    private static final String INPUT="admin/blogs-input";
    private static final String LIST="admin/blogs";
    private static final String REDIRECT_LIST="redirect:/admin/blogs";
 
    @Autowired
    private BlogService blogService;
 
    @Autowired
    private TypeService typeService;
 
    @Autowired
    private TagService tagService;
 
    private void setTypeAndTag(Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
    }
 
    //新增 or 修改
    @PostMapping("/blogs")
    public String post(@RequestParam("picture") MultipartFile picture, Blog blog, RedirectAttributes attributes, HttpSession session){
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        //1.修改:picture為空,則不改變FirstPicture,否則改變FirstPicture。
        //2.新增:直接添加圖片文件
        Blog b;
        if(blog.getId() == null){
            blog.setFirstPicture(UploadImageUtils.upload(picture));//重點
            b=blogService.saveBlog(blog);
        }else{
//            isEmpty()與null的區(qū)別:前者表示內(nèi)容是否為空,后者表示對象是否實例化,在這里前端發(fā)送請求到后端時,就實例化了picture對象
            if(picture.isEmpty()){
                
           blog.setFirstPicture(blogService.getBlog(blog.getId()).getFirstPicture());//重點
            }else {
                blog.setFirstPicture(UploadImageUtils.upload(picture));//重點
            }
            b=blogService.updateBlog(blog.getId(),blog);
        }
        if(b == null){
            attributes.addFlashAttribute("message","操作失敗!");
        }else{
            attributes.addFlashAttribute("message","操作成功!");
        }
        return REDIRECT_LIST;
    }
}

注意:以上部分不重要的代碼已刪掉,只留下重要部分。

三 運行結(jié)果展示

1 初始頁面

2 新增成功頁面

a.添加圖片

b.新增成功

3 修改成功頁面

到此這篇關(guān)于關(guān)于Springboot在新增和修改下上傳圖片并顯示的問題的文章就介紹到這了,更多相關(guān)springboot新增修改上傳圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring?Security登錄表單配置示例詳解

    Spring?Security登錄表單配置示例詳解

    這篇文章主要介紹了Spring?Security登錄表單配置,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • hadoop?切片機(jī)制分析與應(yīng)用

    hadoop?切片機(jī)制分析與應(yīng)用

    切片這個詞對于做過python開發(fā)的同學(xué)一定不陌生,但是與hadoop中的切片有所區(qū)別,hadoop中的切片是為了優(yōu)化hadoop的job在處理過程中MapTask階段的性能達(dá)到最優(yōu)而言
    2022-02-02
  • Java 文件傳輸助手的實現(xiàn)(單機(jī)版)

    Java 文件傳輸助手的實現(xiàn)(單機(jī)版)

    這篇文章主要介紹了Java 文件傳輸助手的實現(xiàn)(單機(jī)版),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • SpringBoot熱部署配置過程

    SpringBoot熱部署配置過程

    這篇文章主要介紹了SpringBoot熱部署配置過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Java關(guān)鍵字之instanceof詳解

    Java關(guān)鍵字之instanceof詳解

    instanceof是Java的一個二元操作符,和==,>,<是同一類東東。由于它是由字母組成的,所以也是Java的保留關(guān)鍵字。它的作用是測試它左邊的對象是否是它右邊的類的實例,返回boolean類型的數(shù)據(jù)
    2021-11-11
  • Java正則表達(dá)式判斷字符串中是否包含中文示例

    Java正則表達(dá)式判斷字符串中是否包含中文示例

    之前一個朋友問我,如何判斷字符串中是否包含中文,其實解決的方法很簡單,但覺著有必要寫出給不知道的朋友們以參考,所以下面這篇文章主要介紹了利用Java正則表達(dá)式判斷字符串中是否包含中文的方法,需要的朋友可以參考。
    2017-03-03
  • java調(diào)用微信現(xiàn)金紅包接口的心得與體會總結(jié)

    java調(diào)用微信現(xiàn)金紅包接口的心得與體會總結(jié)

    這篇文章主要介紹了java調(diào)用微信現(xiàn)金紅包接口的心得與體會總結(jié),有需要的朋友可以了解一下。
    2016-11-11
  • MyEclipse開發(fā)一個webservice接口

    MyEclipse開發(fā)一個webservice接口

    這篇文章主要為大家詳細(xì)介紹了MyEclipse開發(fā)一個webservice接口,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Java中super關(guān)鍵字介紹以及super()的使用

    Java中super關(guān)鍵字介紹以及super()的使用

    這幾天看到類在繼承時會用到this和super,這里就做了一點總結(jié),下面這篇文章主要給大家介紹了關(guān)于Java中super關(guān)鍵字介紹以及super()使用的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • java實現(xiàn)簡單驗證碼生成

    java實現(xiàn)簡單驗證碼生成

    這篇文章主要介紹了java實現(xiàn)簡單驗證碼生成,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10

最新評論

祥云县| 晴隆县| 双桥区| 平果县| 靖远县| 福贡县| 霍山县| 永善县| 通州市| 雷山县| 梁河县| 长顺县| 应城市| 儋州市| 阿瓦提县| 邵阳县| 拉孜县| 岳池县| 莫力| 静宁县| 策勒县| 红桥区| 油尖旺区| 麦盖提县| 上栗县| 涟水县| 北流市| 淮南市| 奈曼旗| 乌兰浩特市| 延寿县| 丰原市| 江山市| 阿合奇县| 鹤壁市| 鄄城县| 宁武县| 贵州省| 利辛县| 洪江市| 蛟河市|