zoukankan      html  css  js  c++  java
  • 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 "上传失败,因为文件是空的.";
            }
        }
    
    }
  • 相关阅读:
    Unity3d Android SDK接入
    Unity打包IOS和Android以及之间的交互
    Gradle
    General error: 1205 Lock wait timeout exceeded; try restarting transaction的解决办法
    kubeadm init [ERROR ImagePull]: failed to pull image [k8s.gcr.io/kube-apiserver-amd64:v1.11.3]: exit status 1
    yum安装kubectl时报错
    PhpStorm激活码 2020年3月亲测可用
    Vmware 14 安装 Vmware Tools 并配置宿主机共享文件给Vmware中Centos7系统
    期权
    对冲-风险对冲
  • 原文地址:https://www.cnblogs.com/liuxm2017/p/11947707.html
Copyright © 2011-2022 走看看