SpringBoot上传文件、图片以及视频。附加载图片或者视频出错的

1、创建一个最基础的SpringBoot项目,不要忘记了加上thymeleaf的依赖

    <properties>
        <java.version>1.8java.version>
        
        <thymeleaf.version>3.0.11.RELEASEthymeleaf.version>
        <thymeleaf-layout-dialect.version>2.2.2thymeleaf-layout-dialect.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

2、为了能够正常访问templates文件夹下的网页,加上config设置

package com.example.demo925002.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author xjj
 */
@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/upload.html").setViewName("/upload");
            }
        };
        return webMvcConfigurer;
    }
}

3、编写upload.html页面


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
    <form method="post" action="/upload" enctype="multipart/form-data">
        
        <input type="file" name="uploadFile" multiple>
        <input type="submit" value="上传">
    form>
body>
html>

4、编写controller

package com.example.demo925002.controller;

import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * @author xjj
 */
@Controller
public class MyController {
    @RequestMapping("/uploadFile")
    @ResponseBody
    public String uploadFile(HttpServletRequest httpServletRequest){
        /* 1、设置文件到本地的文件夹位置 */
        String realPath=null;
        try {
            /* 这里获得的路径是项目的target/classes/upload*/
            realPath= ResourceUtils.getURL("classpath:").getPath()+"/upload";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        /* 如果文件夹不存在,则创建该文件夹 */
        File dir=new File(realPath);
        if(!dir.isDirectory()){
            dir.mkdirs();
        }
        /* 2、获取上传的文件名为uploadFile的list,这个文件名是upload页面上input的name */
        List<MultipartFile> uploadFiles=((MultipartHttpServletRequest)httpServletRequest).getFiles("uploadFile");
        /* 3、开始将文件移动到目标路径下 */
        try {
            for(MultipartFile uploadFile:uploadFiles) {
                String filename = uploadFile.getOriginalFilename();
                File fileServer = new File(dir, filename);
                uploadFile.transferTo(fileServer);
            }
            return "ok";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "fail";
    }
}

5、当想要播放视频或者查看图片时,出现获取资源失败的情况,在Myconfig文件中添加addResourceHandlers

package com.example.demo925002.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author xjj
 */
@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Bean
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/upload.html").setViewName("/upload");
            }
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                /* 其中的路径写的是文件保存的路径,可以直接将file:/后面的内容全部进行替换 */
                registry.addResourceHandler("/Path/**").addResourceLocations("file:/D:/Idea/demo925002/target/classes/upload/");
            }
        };
        return webMvcConfigurer;
    }
}

6、修改前端显示图片或者视频


<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
    <video src="/Path/VID_20190925_134245.mp4" controls="controls">

    video>
    <img src="/Path/3f14fb50352ac65ce75c16d4f5f2b21192138a4b.gif">
body>
html>
>
    form>
    <video src="/Path/VID_20190925_134245.mp4" controls="controls">

    video>
    <img src="/Path/3f14fb50352ac65ce75c16d4f5f2b21192138a4b.gif">
body>
html>

你可能感兴趣的:(#,SpringBoot)