• SpringBoot 上传附件


    一:用idea 创建 springboot 项目:

    详情请参考:《使用IDEA创建一个springboot项目

    二:具体代码内容:

    1:代码结构

    image


    FileUploadController


    package com.alancode.springboot.controller;
    /**
     * @author Alan_liu
     * @Create 2021/2/16 15:51
     */
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @program: SpringBootAddFileUpload
     *@description: SpringBoot 文件上传
     *@author: Alan_Liu
     *@create: 2021-02-16 15:51
     */
    @RestController   /***使用该注解:表示该使用的java类下的所有的方法返回值都默认做jason转换;其作用相当于@Controller+@ResponseBody ****/
    public class FileUploadController {
    
    	 /**
    	  *处理附件上传
    	  *
    	  *@Param: `
    	  *@return
    	  *@throws
    	  *@author Alan_Liu
    	  *@date   2021/2/16 15:56
    	  *
    	  **/
    	 @RequestMapping("/fileUploadController")
    	 public Map<String,Object> fileUpload(MultipartFile filename) throws IOException {
    		 System.out.println(filename.getOriginalFilename());
    	     filename.transferTo(new File("F:/SoftCode/Idea202103_WorkSpace/SpringBootAddFileUpload/src/main/resources/static/UpLoadFile/"+filename.getOriginalFilename()));
             Map<String,Object> map=new HashMap<String,Object>();
             map.put("msg","ok");
             return map;
    	 }
    
    
    
    
    }


    DemoApplication


    package com.alancode.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.MultipartConfigFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.util.unit.DataSize;
    
    import javax.servlet.MultipartConfigElement;
    
    /**
     * @author Alan_Liu
     */
    @SpringBootApplication
    public class DemoApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(DemoApplication.class, args);
    	}
    
    	 //@Configuration
    	@Bean
    	public MultipartConfigElement multipartConfigElement() {
    		MultipartConfigFactory factory = new MultipartConfigFactory();
    		//单个文件最大
    		factory.setMaxFileSize(DataSize.parse("1024MB")); //KB,MB
    		//设置总上传数据总大小
    		factory.setMaxRequestSize(DataSize.parse("1024MB"));
    		return factory.createMultipartConfig();
    	}
    }
    


    ServletInitializer


    package com.alancode.springboot;
    
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    
    public class ServletInitializer extends SpringBootServletInitializer {
    
    	@Override
    	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    		return application.sources(DemoApplication.class);
    	}
    
    }
    


    Upload.html


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>springboot 文件上传</title>
    </head>
    <body>
    <form   action="fileUploadController" method="post" enctype="multipart/form-data" value="107341824000">>
        上传文件:<input type="file" name="filename" /><input type="submit">
    
    </form>
    </body>
    </html>


    application.properties

    spring.http.multipart.maxFileSize = -1
    spring.http.multipart.maxRequestSize=-1
    


    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.4.2</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.alanCode.springBoot</groupId>
        <artifactId>SpringBootAddFileUpload</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
        <name>demo</name>
        <description>springboot 上传附件</description>
        <properties>
            <java.version>1.8</java.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    


    启动项目:

    image


    上传附件:


    上传附件路径:

    image


    访问静态页面

    image

    image


    image


    image

    image


    image

    为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
    学问:纸上得来终觉浅,绝知此事要躬行
    为事:工欲善其事,必先利其器。
    态度:道阻且长,行则将至;行而不辍,未来可期
    转载请标注出处!
  • 相关阅读:
    配置WebLogic的详细步骤
    安装WebLogic的详细步骤
    syswrite 函数
    利用perl 改写awk统计
    第一章 输入和输出基础
    CIO必看:跨国集团采购部报表系统的建设经验分享
    价值5000元的web报表分享
    价值5000元的web报表分享
    分页语句 where 条件列和Order by 列 引导列的问题
    Java中final修饰参数的作用
  • 原文地址:https://www.cnblogs.com/ios9/p/14407066.html
Copyright © 2020-2023  润新知