• SpringBoot上传文件


    1.pom文件

    <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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.company</groupId>
        <artifactId>uploaddemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>uploaddemo</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
            <relativePath />
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>

    2.Springboot项目 

    2.1 application.properties

    server.port=8000
    spring.http.multipart.maxFileSize=10Mb  
    spring.http.multipart.maxRequestSize=10Mb

    2.2 App.java

    package com.company.uploaddemo;
    
    import org.springframework.boot.Banner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    /**
     * Hello world!
     *
     */
    @SpringBootApplication
    public class App {
        public static void main(String[] args) {
            SpringApplication app = new SpringApplication(App.class);
            // 关闭banner
            app.setBannerMode(Banner.Mode.OFF);
            app.run(args);
        }
    }

    2.3 Controller

    package com.company.uploaddemo;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.springframework.util.FileCopyUtils;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    
    @RestController
    public class FileUploadController {
    
        /**
         * 上传文件 测试方法: 测试接口:http://localhost:8000/upload 使用PostMan测试
         *
         * @param file 待上传文件
         * @return
         */
        @RequestMapping(value = "/upload", method = RequestMethod.POST)
        @ResponseBody
        public String handleFileUpload(@RequestParam(value = "file", required = true) MultipartFile file) {
            File fileToSave = null;
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    // fileToSave = new File(file.getOriginalFilename());
                    fileToSave = new File("C:\" + file.getOriginalFilename());
                    // 自定义存储路径
                    FileCopyUtils.copy(bytes, fileToSave);
                } catch (IOException e) {
                    e.printStackTrace();
                    return "上传失败," + e.getMessage();
                }
                return "上传成功" + fileToSave.getAbsolutePath();
            } else {
                return "上传失败,因为文件是空的.";
            }
        }
    
    }
  • 相关阅读:
    POJ 3660 Cow Contest——flody求传递闭包
    最小生成树算法
    最短路练习
    UVa 11491 Erasing and Winning
    uva 1610 聚会游戏
    C#操作Excel
    C#操作Excel文件(转)
    sqlserver行列转换问题(网上搜集)
    ASP.NET常用技术之Cookie
    ASP.NET常用技术之加密解密
  • 原文地址:https://www.cnblogs.com/liuxm2017/p/11947707.html
Copyright © 2020-2023  润新知