SpringBoot之文件上传

前端页面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传!</title>
</head>
<body>
    <form method="POST" enctype="multipart/form-data" action="/upload">
        <p>
            文件:<input type="file" name="file1" />
        </p>
        <p>
            <input type="submit" value="上传" />
        </p>
    </form>
    <form method="POST" enctype="multipart/form-data" action="/uploadBatch">
        <p>
            文件1:<input type="file" name="file" />
        </p>
        <p>
            文件2:<input type="file" name="file" />
        </p>
        <p>
            <input type="submit" value="上传" />
        </p>
    </form>
</body>
</html>

上传配置UploadConfig.java

package com.zns.config;

import javax.servlet.MultipartConfigElement;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class UploadConfig {
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 设置文件大小限制 ,超了,页面会抛出异常信息,这时候就需要进行异常信息的处理了;
        factory.setMaxFileSize("1024KB"); // KB,MB
        // 设置总上传数据总大小
        factory.setMaxRequestSize("1024KB");
        // Sets the directory location where files will be stored.
        // factory.setLocation("路径地址");
        return factory.createMultipartConfig();
    }
}

UploadController.java

package com.zns.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
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.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
public class UploadController {
    // 单文件上传
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam("file1") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                // 此处是上传到了根目录
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(file.getOriginalFilename())));
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }
            return "上传成功";
        } else {
            return "上传失败,因为文件是空的.";
        }
    }

    // 多文件上传
    @RequestMapping(value = "/uploadBatch", method = RequestMethod.POST)
    @ResponseBody
    public String uploadBatch(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        MultipartFile file = null;
        BufferedOutputStream stream = null;
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    stream = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
                    stream.write(bytes);
                    stream.close();
                } catch (Exception e) {
                    stream = null;
                    return "第" + i + "个文件上传失败:" + e.getMessage();
                }
            } else {
                return "上传失败,第" + i + "个文件为空!";
            }
        }
        return "上传成功";
    }
}

原文地址:https://www.cnblogs.com/zengnansheng/p/10389798.html

时间: 2024-08-30 09:52:55

SpringBoot之文件上传的相关文章

SpringBoot图文教程4—SpringBoot 实现文件上传下载

有天上飞的概念,就要有落地的实现 概念+代码实现是本文的特点,教程将涵盖完整的图文教程,代码案例 文章结尾配套自测面试题,学完技术自我测试更扎实 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 大哥大姐新年好,点赞转发不要少 SpringBoot 图文系列教程技术大纲 鹿老师的Java笔记 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+案例 思维导图」「基础篇上」 SpringBoot图文教程2—日志的使用「logback」「log4j」 Sp

springBoot的文件上传功能

知识点: 后台:将上传的图片写入指定服务器路径,保存起来,返回上传后的图片路径(在springBoot中,参考博客:http://blog.csdn.net/change_on/article/details/59529034) 前端:在Vue.js前端框架中,使用Vue_Core_Image_Upload插件,上传图片 (github地址:https://github.com/Vanthink-UED/vue-core-image-upload) 后台: 1)在Controller中写一个方法

springboot+web文件上传和下载

一.首先安装mysql数据库,开启web服务器. 二.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:schemaLocatio

(十)SpringBoot的文件上传

一:添加commons-fileupload依赖 打开pom文件添加 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> 二:添加系统变量 打开core→constant文件,添加文件保存路径 //

Springboot Feign文件上传

服务提供者 @SpringBootApplication public class FeignUploadFirstApplication { @RestController public class UploadController { @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public Str

springboot实现简单的文件上传

承接上一篇,这里记录一下简单的springboot文件上传的方式 首先,springboot简单文件上传不需要添加额外的jar包和配置 这里贴一下后端controller层的实现代码 补一份前台的HTML代码 补充,这里实现的是简单的单文件上传,没有指定存储路径和访问路径 windows下的话,图片默认的是保存在临时目录下的,示例如:C:\Users\用户名\AppData\Local\Temp\tomcat.8163613599402220500.8080\work\Tomcat\localh

spring-boot 参数长度、文件上传大小限制问题

spring boot 设置tomcat post参数限制 1.外置tomcat: 这个简单,直接在server.xml里面修改这句话: <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" maxPostSize="0"/> 没错就是修改这里的maxPostSize的值,

spring boot工程文件上传出现net::ERR_CONNECTION_ABORTED,文件上传失败

SpringBoot做文件上传时出现了The field file exceeds its maximum permitted size of 1048576 bytes.错误,显示文件的大小超出了允许的范围.查看了官方文档,原来Spring Boot工程嵌入的tomcat限制了请求的文件大小,这一点在Spring Boot的官方文档中有说明,原文如下 65.5 Handling Multipart File UploadsSpring Boot embraces the Servlet 3 j

springBoot(3)---目录结构,文件上传

目录结构,文件上传 一.目录结构 1.目录讲解 src/main/java:存放代码      src/main/resources                   static: 存放静态文件,比如 css.js.image, (访问方式 http://localhost:8080/js/main.js)                   templates:存放静态页面jsp,html,tpl                   config:存放配置文件,application.pr