码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot之文件上传

时间:2019-02-17 00:52:03      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:配置   requests   失败   red   lse   multi   cep   head   pos   

前端页面

<!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 "上传成功";
    }
}

 

SpringBoot之文件上传

标签:配置   requests   失败   red   lse   multi   cep   head   pos   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!