国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

spring boot +Vue + element-ui實(shí)現(xiàn)圖片上傳和回顯

這篇具有很好參考價(jià)值的文章主要介紹了spring boot +Vue + element-ui實(shí)現(xiàn)圖片上傳和回顯。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

對于圖片上傳和顯示后臺采用SpringBoot實(shí)現(xiàn):

package com.example.demo.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.example.demo.domain.Books;
import com.example.demo.service.BooksService;
import com.example.demo.util.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Value("${server.port}")
    private String port;

    String path=System.getProperty("user.dir") +"/book-java/src/main/resources/upload/";
    private static final String host="http://localhost";

    @Resource
    private BooksService booksService;

    @ApiOperation("圖書列表")
    @GetMapping("/")
    public Result<List<Books>> findBooks(){
        return  new Result<>(true, 200, "OK", this.booksService.list());
    }

    @ApiOperation("新增圖書")
    @PostMapping("/add")
    public Result<?> addBook(@RequestBody Books books){
        return this.booksService.save(books)
                ? new Result<>(true, 200, "新增成功!")
                : new Result<>(false, 202, "新增失敗!");
    }

    @ApiOperation("上傳圖片")
    @PostMapping("/upload")
    public Result<?> upload(MultipartFile file){

        System.out.println(path);
        String id= IdUtil.fastSimpleUUID();
        String originalFilename = file.getOriginalFilename();
        String imgPath= path+id+"_"+originalFilename;
        System.out.println(imgPath);
        try {
            FileUtil.writeBytes(file.getBytes(),imgPath);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result<>(false, 303, "上傳失敗!");
        }
        return new Result<>(true, 200, "上傳成功", host+":"+this.port+"/books/"+id);
    }

    @GetMapping("/{flag}")
    @ApiOperation("文件下載")
    public void downFile(@PathVariable String flag, HttpServletResponse response){
        OutputStream outputStream=null;
        File file=new File(path);
        List<String>  list= Arrays.asList(file.list());
        String fileName=list.stream().filter(name->name.contains(flag)).findAny().orElse("");
        if(fileName!=null && !fileName.isEmpty()){
            try {
                response.addHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(fileName, "utf-8"));
                response.setContentType("application/octet-stream");

                byte[] arr= FileUtil.readBytes(path+fileName);
                outputStream=response.getOutputStream();
                outputStream.write(arr);
                outputStream.flush();
                outputStream.close();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

這里要特別注意的是:參數(shù)名稱必須是file,必須是post方式!

public Result<?> upload(MultipartFile file){

前端Vue:

 <el-dialog
                title="添加圖書"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose">
            <div>
                <el-form :model="book" label-width="120px">
                    <el-form-item label="圖書名稱">
                        <el-input  v-model="book.title"></el-input>
                    </el-form-item>
                    <el-form-item label="圖書作者">
                        <el-input v-model="book.author"></el-input>
                    </el-form-item>
                    <el-form-item label="圖書價(jià)格">
                        <el-input  v-model="book.price"></el-input>
                    </el-form-item>
                    <el-form-item label="庫存數(shù)量">
                        <el-input v-model="book.kucun"></el-input>
                    </el-form-item>
                    <el-form-item label="圖書封面">
                        <el-upload
                                class="upload-demo"
                                v-model="book.pic"
                                action="http://localhost:8081/books/upload" :on-success="upPic">
                            <el-button size="small" type="primary">點(diǎn)擊上傳</el-button>
                        </el-upload>
                    </el-form-item>

                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addBook">確 定</el-button>
  </span>
 </el-dialog>

圖片上傳:

  upPic(resp){
                console.log(resp.data);
                //獲取圖片名稱
                this.book.pic=resp.data;
            },

新增:

 addBook(){
                request.post('/books/add',this.book).then(resp=>{
                    if(resp.code===200){
                        this.$message.success('新增圖書成功!');
                        this.book={};
                        this.getBooks();
                        this.dialogVisible=false;

                    }else{
                        this.$message.error('新增圖書失敗!')
                    }
                })
            },

圖片的顯示:文章來源地址http://www.zghlxwxcb.cn/news/detail-644118.html

 <el-table-column
                label="封面"
        >
           <template #default="scope">
               <el-image
                       style="width: 50px; height: 50px"
                       :src="scope.row.pic"
                       :preview-src-list="[scope.row.pic]">
               </el-image>
           </template>
        </el-table-column>

到了這里,關(guān)于spring boot +Vue + element-ui實(shí)現(xiàn)圖片上傳和回顯的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 基于vue element-ui 封裝上傳圖片組件 功能:上傳,刪除,預(yù)覽,上傳圖片水印,拖拽排序,上傳進(jìn)度條等

    基于vue element-ui 封裝上傳圖片組件 功能:上傳,刪除,預(yù)覽,上傳圖片水印,拖拽排序,上傳進(jìn)度條等

    我們在開發(fā)后臺時(shí)肯定避免不了上傳圖片的功能 例如: 上傳圖片回顯 上傳完成 : 預(yù)覽查看 , 刪除等 如果是圖片列表,還可能讓你拖動圖片排序 有的后臺項(xiàng)目可能要給圖片添加水印,添加標(biāo)記 有的后臺可能要炫酷一點(diǎn) 添加進(jìn)度條功能 現(xiàn)在我們要完成上面的一系列功能,這里我

    2024年02月16日
    瀏覽(28)
  • 基于vue+element-ui實(shí)現(xiàn)上傳進(jìn)度條

    基于vue+element-ui實(shí)現(xiàn)上傳進(jìn)度條

    目錄 基于el-upload組件實(shí)現(xiàn)進(jìn)度條的編寫 后臺進(jìn)度前臺進(jìn)度條顯示 基于el-upload組件實(shí)現(xiàn)進(jìn)度條的編寫 ①編寫文件上傳時(shí)的鉤子函數(shù) ②監(jiān)聽進(jìn)度百分比 后臺進(jìn)度前臺進(jìn)度條顯示 參考文章: 后臺進(jìn)度前臺顯示進(jìn)度條_weixin_30646505的博客-CSDN博客 后端思路: ①創(chuàng)建一個(gè)類,封裝進(jìn)

    2023年04月08日
    瀏覽(22)
  • 擴(kuò)展element-ui el-upload組件,實(shí)現(xiàn)復(fù)制粘貼上傳圖片文件,帶圖片預(yù)覽功能

    擴(kuò)展element-ui el-upload組件,實(shí)現(xiàn)復(fù)制粘貼上傳圖片文件,帶圖片預(yù)覽功能

    控件改造 在窗口的 el-form 控件參數(shù)中添加 @paste.native 事件,事件綁定方法名 handlePaste 也可以在其他控件中添加事件監(jiān)聽,看個(gè)人需求。 注意: 監(jiān)聽粘貼事件時(shí),需要當(dāng)前頁面先獲取焦點(diǎn),否則無法正常監(jiān)聽, 可以在頁面加載后調(diào)用 focus() 獲取焦點(diǎn) 粘貼功能Js部分參考資料

    2023年04月08日
    瀏覽(124)
  • java上傳實(shí)現(xiàn) spring boot +element ui

    先從element ui el-upload組件開始介紹。 關(guān)于headers 在return里寫,這個(gè)即可獲得headers 目錄 1、得到文件上傳路徑,getUploadPath方法 2、FileUploadUtils里的upload方法,返回一個(gè)新的文件名 3、獲取url? 完整的FileUploadUtils? 再來看后臺controller 大致步驟分為以下: 1、得到文件上傳路徑,

    2024年02月12日
    瀏覽(26)
  • Element-ui 上傳圖片前壓縮圖片

    上傳前把圖片大小進(jìn)行一個(gè)壓縮在進(jìn)行上傳。 文章目錄 需求:項(xiàng)目當(dāng)中上傳圖片的需求點(diǎn)肯定有很多,再上傳之后,如果圖片很大的話,在加載的時(shí)候就會很慢。最近發(fā)現(xiàn)系統(tǒng)首次加載越來越慢,就開始思考怎么能降低這個(gè)加載時(shí)間,由于首頁圖片很多,所以圖片的大小就

    2024年02月06日
    瀏覽(20)
  • element-ui的el-upload組件實(shí)現(xiàn)上傳拖拽排序圖片順序(sortablejs)
  • element-ui upload圖片上傳組件使用

    element-ui upload圖片上傳組件使用

    圖片上傳前端收集 數(shù)據(jù) 再調(diào)用接口發(fā)送到后端 組件標(biāo)簽內(nèi)的參數(shù): 參數(shù) 說明 類型 可選值 默認(rèn)值 action 必選參數(shù),上傳的地址 string — — headers 設(shè)置上傳的請求頭部 object — — multiple 是否支持多選文件 boolean — — data 上傳時(shí)附帶的額外參數(shù) object — — name 上傳的文件字段

    2023年04月19日
    瀏覽(26)
  • vue+element ui實(shí)現(xiàn)圖片上傳并拖拽進(jìn)行圖片排序

    用到的技術(shù)棧: vue2 element Ui vue-dragging 第一步: 安裝 第二步: 引入 Video_23-11-13_10-17-30 end~~~ 如有錯(cuò)誤或觀點(diǎn)不一致的請?jiān)u論留言共同討論,本人前端小白一枚,根據(jù)自己實(shí)際項(xiàng)目遇到的問題進(jìn)行總結(jié)分享,謝謝大家的閱讀!

    2024年01月24日
    瀏覽(28)
  • 用element-ui中的up-load組件實(shí)現(xiàn)簡單的圖片上傳到本地然后回顯(從前端到后端)

    用element-ui中的up-load組件實(shí)現(xiàn)簡單的圖片上傳到本地然后回顯(從前端到后端)

    一:前端樣式以及效果: ?前端樣式代碼如下: 二:后端Controller層 ?這里的處理邏輯是將前端傳遞進(jìn)來的圖片交給FileUtil處理,接下來看FileUtil的代碼: 首先獲取傳遞進(jìn)來圖片的文件名后綴 然后用UUID將其拼接得到一個(gè)新的名字 將圖片存入到本地的文件夾下面 接下來返回路

    2024年02月03日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包