pom文件project标签报错_Spring Boot 2.x 实现文件上传

v2-68c8801c4be643df645b8a800b2b18a7_1440w.jpg?source=172ae18b

Spring Boot 2.x 实现文件上传

简述

此教程是一个日常上传文件的脚手架,用到的很多,就写一个简单的Demo,为了日后更加方便的CV编程,有紧急需求的可直接去源码CV

源码地址

https://github.com/Gleans/SpringBootLearn/tree/master/springboot-upfile

新建项目

v2-e3fbb250ba174a31a1c59716392f47e3_b.jpg

v2-6f1b3de19183591b12c0ad84467eea6d_b.jpg

目录

v2-6952cc73c8b0a88cf155c38e9a547880_b.jpg

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot-upfile</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-upfile</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--  Thymeleaf组件 动态页面-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

UploadServiceImpl.java

package com.example.springbootupfile.service.impl;

import com.example.springbootupfile.service.UploadService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;

@Service
public class UploadServiceImpl implements UploadService {

    @Value("${file.path}")
    private String path;

    @Override
    public Map<Object, Object> uploadFile(MultipartFile file) throws IOException {
        Map<Object, Object> resultMap = new HashMap<>(4);

        if (file.isEmpty()) {
            resultMap.put("code", -1);
            resultMap.put("msg", "文件不能为空");
        }

        // 得到上传时的文件du名
        String fileName = file.getOriginalFilename();
        // 获取文件输入流
        InputStream is = file.getInputStream();

        /*
         * 使用 Files.copy 命令将文件从输入流源复制到目标目录
         * StandardCopyOption.REPLACE_EXISTING 覆盖已有的目标路径
         */
        Files.copy(is, Paths.get(path + fileName), StandardCopyOption.REPLACE_EXISTING);

        resultMap.put("code", 0);
        resultMap.put("msg", "success");
        return resultMap;
    }
}

自行创建一个 UploadService 接口

简单的前端上传页面

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>上传文件</h1>

<form action="/upload" method="post" enctype="multipart/form-data">
    <label>选择文件</label>
    <input type="file" name="file">
    <button type="submit">上传</button>
</form>
</body>
</html>

templates/result.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>返回页面</title>
</head>
<body>
<h1>返回结果</h1>

<p>返回代码:<span th:text="${code}"></span></p>
<p>message:<span th:text="${msg}"></span></p>
</body>
</html>

配置文件

配置上传的路径
file.path=D:baidu

启动类

SpringbootUpfileApplication.java

package com.example.springbootupfile;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootUpfileApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootUpfileApplication.class, args);
    }

}

启动项目

上传页面

v2-b79fbeadeb116048ee8741a3cd8b6625_b.jpg

返回页面

v2-a09ef2a556ea43a95dcbb5989fb12d7e_b.jpg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值