`

springboot:upload

阅读更多
FileUploadConfiguration.java
===================================================
@Configuration
public class FileUploadConfiguration {

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

}
===================================================
Controller.java
===================================================
    /**
     * 文件上传具体实现方法(单文件上传)
     *
     * @param file
     * @return <form method="POST" enctype="multipart/form-data" action="/upload">
     * <p>
     * 文件:<input type="file" name="file" />
     * </p>
     * <p>
     * <input type="submit" value="上传" />
     * </p>
     * </form>
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam("file") 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 "上传失败,因为文件是空的.";
        }
    }

    /**
     * 多文件上传 主要是使用了MultipartHttpServletRequest和MultipartFile
     *
     * @param request
     * @return <form method="POST" enctype="multipart/form-data"
     * action="/upload/batch">
     * <p>
     * 文件1:<input type="file" name="file" />
     * </p>
     * <p>
     * 文件2:<input type="file" name="file" />
     * </p>
     * <p>
     * 文件3:<input type="file" name="file" />
     * </p>
     * <p>
     * <input type="submit" value="上传" />
     * </p>
     * </form>
     */
    @RequestMapping(value = "/upload/batch", method = RequestMethod.POST)
    public
    @ResponseBody
    String batchUpload(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 "You failed to upload " + i + " => " + e.getMessage();
                }
            } else {
                return "You failed to upload " + i + " because the file was empty.";
            }
        }
        return "upload successful";
    }
分享到:
评论

相关推荐

    SpringBoot File Upload

    &lt;form method="post" action="/upload" enctype="multipart/form-data"&gt; *,application/pdf"&gt; 上传 ``` 这个页面包含一个文件输入字段和一个提交按钮,用户可以选择文件并提交到服务器。 此外,为了处理...

    springboot-upload-download:使用Sping Boot REST API上传和下载文件

    springboot-上传-下载待办事项清单: 克隆此存储库: git clone https://github.com/hendisantika/springboot-upload-download.git 进入文件夹: cd springboot-upload-download 运行应用程序: mvn clean spring-...

    smart-garden-springboot:springboot后台程序

    smart-garden-springboot springboot后台程序 本地开发及打包之前,务必修改application.yml文件中upload-path属性,修改值为本地/服务器的图片上传绝对路径,注意路径内包含的文件夹必须已完成创建

    springboot-upload-excel:springboot上传excel文件,并将文件中数据保存至mysql数据库

    在本项目"springboot-upload-excel"中,我们主要探讨如何使用Spring Boot处理Excel文件的上传,并将其中的数据高效地存入MySQL数据库。这在数据分析、报表管理或数据导入等场景下非常常见。以下是关于这个项目的详细...

    uploadfile_大文件上传_springboot_webupload_源码

    本项目"uploadfile_大文件上传_springboot_webupload_源码"旨在演示如何结合WebUpload和Spring Boot实现高效的大文件上传功能。 首先,我们要理解Spring Boot中的MultipartFile接口。这是Spring MVC框架为处理上传...

    SpringBoot第 6 讲:SpringBoot+jersey跨域文件上传

    name: springboot-jersey-file-upload jersey: application-path: /api ``` 现在,我们可以创建一个Jersey资源类来处理文件上传。首先,我们需要一个控制器接口,使用`@POST`注解来表示文件上传的HTTP请求方法,...

    webuploader + springboot实现大文件的上传下载

    1. **配置MultipartFile处理**:在SpringBoot的控制器中,创建一个接收`MultipartFile`的接口,例如`@PostMapping("/upload")`,并使用`MultipartFile`的`getBytes()`方法获取文件内容。 2. **分块上传**:为了实现...

    SpringBoot+文件上传

    @PostMapping("/upload") public String handleFileUpload(@RequestParam("file") MultipartFile file) { // 文件处理逻辑... } ``` 3. **文件存储**: 接收到文件后,你需要将文件保存到服务器的某个位置。这...

    springboot_vue-simple-upload.zip

    【标题】"springboot_vue-simple-upload.zip"是一个包含SpringBoot和Vue.js的文件上传示例项目,用于演示如何在Web应用中实现文件上传功能,特别是关注于进度展示、分片上传和续传。 【描述】该压缩包由两个主要...

    springboot-upload-multiple-file-to-mysql:SpringBoot将多个文件上传到MySQL示例– Thymeleaf + Spring JPA审核

    springboot将多个文件上传到mysql 通过以下命令运行该项目: mvn clean spring-boot:run 然后打开您喜欢的浏览器,然后输入: 截屏 主页 上载页面 列出上传的页面 表结构 表格数据

    SpringBoot导入上传文件异常The temporary upload location is not valid

    错误spring boot上传文件错误The temporary upload location [/tmp/tomcat.******/work/Tomcat/localhost/ROOT] is not valid

    SpringBoot+AntDesignVue实现excel导入功能

    描述: 该文章旨在介绍如何使用 SpringBoot 和 AntDesignVue 实现 Excel 导入功能,该功能主要是使用 Ant Design Vue 中的 upload 组件来实现导入 excel 文件的功能。 标签: SpringBoot AntDesignVue excel 实现 ...

    springboot-fileUpload

    &lt;form action="/upload" method="post" enctype="multipart/form-data"&gt; 上传文件 ``` 5. **图片显示**:如果上传的是图片文件,我们还需要提供一个页面或API来显示这些图片。可以创建一个Controller方法返回...

    springboot框架+thymeleaf模板引擎+layui前端框架+数据库

    而"springboot-upload"可能是一个与SpringBoot上传功能相关的模块或示例代码,展示了如何在SpringBoot应用中处理文件上传。 综上所述,这个开源项目利用SpringBoot的便利性,结合Thymeleaf的模板渲染,Layui的前端...

    SpringBoot文件上传.zip

    "SpringBoot_Upload"可能是一个包含具体实现代码的文件,比如包含上面提到的控制器类和其他相关配置。 总之,通过SpringBoot 2.x提供的文件上传功能,我们可以轻松地在Java应用中实现单文件或多文件的上传操作,...

    fileUploadDemo-master_大文件上传_springboot_断点续传_

    本文将深入探讨如何使用Webupload与SpringBoot框架实现大文件上传及断点续传功能。 首先,SpringBoot是Java领域的一款轻量级、快速开发框架,它简化了传统的Spring应用的配置,提供了开箱即用的功能,如内置Tomcat...

    基于springboot的文件在线预览.rar

    - `/upload` 接收文件上传 - `/preview/{filename}` 返回预览的HTML或图片流 - `/download/{filename}` 提供文件下载 7. **前端实现**:前端通常使用React、Vue或Angular等现代JavaScript框架来构建用户界面,...

    毕业设计基于SSM/Springboot的商城项目

    OnlineSchoolShop-基于Spring boot/SSM商城的搭建教程 ... ... ... 管理员帐号 admin 12345678 ...图片保存路径: 可以全局搜索后替换 ...windows: D:/upload ...linux: /usr/upload ...毕业设计基于SSM/Springboot的商城项目

    文件上传下载-springboot-demo.zip

    这个"文件上传下载-springboot-demo.zip"压缩包很可能是提供了一个简单的示例项目,用于演示如何在Spring Boot应用中实现这些功能。让我们深入探讨Spring Boot中文件上传与下载的核心概念和技术。 **文件上传** 在...

Global site tag (gtag.js) - Google Analytics