解决使用Spring Boot、Multipartfile上传文件路径错误问题

本文主要是介绍解决使用Spring Boot、Multipartfile上传文件路径错误问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.问题描述

  • 关键字: SpringMVC 4.2.4 、 Spring Boot 1.3.1 、Servlet 3.0 、文件上传
  • 报错信息: java.io.IOException: java.io.FileNotFoundException: /tmp/tomcat.273391201583741210.8080/work/Tomcat/localhost/ROOT/tmp/source/IMG_20160129_132623.jpg (No such file or directory)
  • 问题源码: transferTo方法报错
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    // 前端传入mulFileSource
    // 创建压缩前源文件
    File fileSourcePath = new File("tmp/source/");
    File fileSource = new File(fileSourcePath, mulFileSource.getOriginalFilename());
    if (!fileSourcePath.exists()) {
        fileSourcePath.mkdirs();
    }
    // 将接收得图片暂存到临时文件中
    mulFileSource.transferTo(fileSource);
    

2.问题分析

  • 首先,看源码中文件定义,相对路径,预期路径应该是项目路径/tmp/source/,但是报错确是一个系统临时文件路径(tomcat的)。
  • 其次,由于是transferTo方法报错,因此应该是该方法写入文件时报错,因此,我们跟入方法源码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class StandardMultipartHttpServletRequest extends AbstractMultipartHttpServletRequest {

//中间代码省略

/**
 * Spring MultipartFile adapter, wrapping a Servlet 3.0 Part object.
 */
@SuppressWarnings("serial")
private static class StandardMultipartFile implements MultipartFile, Serializable {

//中间代码省略

@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
this.part.write(dest.getPath());
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package org.apache.catalina.core;
/**
 * Adaptor to allow {@link FileItem} objects generated by the package renamed
 * commons-upload to be used by the Servlet 3.0 upload API that expects
 * {@link Part}s.
 */
public class ApplicationPart implements Part {

//中间代码省略

    @Override
    public void write(String fileName) throws IOException {
        File file = new File(fileName);
        if (!file.isAbsolute()) {
            file = new File(location, fileName);
        }
        try {
            fileItem.write(file);
        } catch (Exception e) {
            throw new IOException(e);
        }
    }
}
  • 源码一目了然,使用Servlet3.0的支持的上传文件功能时,如果我们没有使用绝对路径的话,transferTo方法会在相对路径前添加一个location路径,即:file = new File(location, fileName);。当然,这也影响了SpringMVC的Multipartfile的使用。
  • 由于我们创建的File在项目路径/tmp/source/,而transferTo方法预期写入的文件路径为/tmp/tomcat.273391201583741210.8080/work/Tomcat/localhost/ROOT/tmp/source/,我们并没有创建该目录,因此会抛出异常。

3.问题解决方案

  1. 使用绝对路径
  2. 修改location的值
    这个location可以理解为临时文件目录,我们可以通过配置location的值,使其指向我们的项目路径,这样就解决了我们遇到的问题。
    在Spring Boot下配置location,可以在main()方法所在文件中添加如下代码:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    /**
     * 文件上传临时路径
     */
     @Bean
     MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setLocation("/app/pttms/tmp");
        return factory.createMultipartConfig();
    }
    

  3. Home  >   Spring 4

    Spring 4 MVC Single and Multiple File Upload Example with Tomcat

    By Arvind Rai,  August 09, 2014
    In this page we will learn how to upload a file in Spring 4 MVC. We are presenting the demo for single and multiple file upload. We are using XML less configurations. MultipartConfigElement Bean needs to be configured for file upload. In controller, method argument should be MultipartFile class for uploading the file. The JSP form, enctype must be set for multipart form data. Check the demo now.

    Software Used

    To run the file upload demo in Spring 4, get ready with below software. 
    1. JDK 7 
    2. Eclipse 
    3. Tomcat 7 
    4. Gradle 2.0 for Spring Boot

    Project Structure in Eclipse

    For better understanding, get the file location in eclipse how to put our classes and JSPs.
    Spring 4 MVC  Single and Multiple File Upload  Example  with Tomcat

    Controller Class: Use of MultipartFile

    In our example we are presenting demo for single and multiple file upload. So we have created two different methods for uploading the file. In single upload, the method should have the parameter as below with other parameters.
    @RequestParam("file") MultipartFile file 
    And for multiple file upload , the parameter should be as below
    @RequestParam("file") MultipartFile[]  file 
    Get the name of file from MultipartFile object and save it to your desired location. Find the controller. 
    FileUploadController.java
    package com.concretepage;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    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;
    @Controller
    public class FileUploadController {@RequestMapping(value="/singleUpload")public String singleUpload(){return "singleUpload";}@RequestMapping(value="/singleSave", method=RequestMethod.POST )public @ResponseBody String singleSave(@RequestParam("file") MultipartFile file, @RequestParam("desc") String desc ){System.out.println("File Description:"+desc);String fileName = null;if (!file.isEmpty()) {try {fileName = file.getOriginalFilename();byte[] bytes = file.getBytes();BufferedOutputStream buffStream = new BufferedOutputStream(new FileOutputStream(new File("F:/cp/" + fileName)));buffStream.write(bytes);buffStream.close();return "You have successfully uploaded " + fileName;} catch (Exception e) {return "You failed to upload " + fileName + ": " + e.getMessage();}} else {return "Unable to upload. File is empty.";}}@RequestMapping(value="/multipleUpload")public String multiUpload(){return "multipleUpload";}@RequestMapping(value="/multipleSave", method=RequestMethod.POST )public @ResponseBody String multipleSave(@RequestParam("file") MultipartFile[] files){String fileName = null;String msg = "";if (files != null && files.length >0) {for(int i =0 ;i< files.length; i++){try {fileName = files[i].getOriginalFilename();byte[] bytes = files[i].getBytes();BufferedOutputStream buffStream = new BufferedOutputStream(new FileOutputStream(new File("F:/cp/" + fileName)));buffStream.write(bytes);buffStream.close();msg += "You have successfully uploaded " + fileName +"<br/>";} catch (Exception e) {return "You failed to upload " + fileName + ": " + e.getMessage() +"<br/>";}}return msg;} else {return "Unable to upload. File is empty.";}}
    } 

    Configuration Class: Use of MultipartConfigElement Bean

    In the configuration class, we need to use bean for MultipartConfigElement and UrlBasedViewResolver. MultipartConfigElement supports the file upload where we can set max file size, max request size etc. MultipartConfigElement needs to be configured with Dispatcher servlet using WebApplicationInitializer . UrlBasedViewResolver defines the JSP location and file extension pattern of output. 
    AppConfig.java
    package com.concretepage;
    import javax.servlet.MultipartConfigElement;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.context.embedded.MultipartConfigFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.view.JstlView;
    import org.springframework.web.servlet.view.UrlBasedViewResolver;
    @Configuration
    @ComponentScan
    @EnableWebMvc
    @EnableAutoConfiguration
    public class AppConfig {@Beanpublic MultipartConfigElement multipartConfigElement() {MultipartConfigFactory factory = new MultipartConfigFactory();factory.setMaxFileSize("128KB");factory.setMaxRequestSize("128KB");return factory.createMultipartConfig();}@Bean  public UrlBasedViewResolver setupViewResolver() {  UrlBasedViewResolver resolver = new UrlBasedViewResolver();  resolver.setPrefix("/views/");  resolver.setSuffix(".jsp");  resolver.setViewClass(JstlView.class);return resolver;  }
    } 

    WebApplicationInitializer Class: Use of Dynamic.setMultipartConfig()

    WebApplicationInitializer is used when our application is not using web.xml. This supports all functionality which web.xml does. To supports file upload our dispatcher must be set for multipart config. 
    WebAppInitializer.java
    package com.concretepage;
    import javax.servlet.MultipartConfigElement;
    import javax.servlet.ServletContext;  
    import javax.servlet.ServletException;  
    import javax.servlet.ServletRegistration.Dynamic;  
    import org.springframework.web.WebApplicationInitializer;  
    import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;  
    import org.springframework.web.servlet.DispatcherServlet;  
    public class WebAppInitializer implements WebApplicationInitializer {public void onStartup(ServletContext servletContext) throws ServletException {  AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();  ctx.register(AppConfig.class);  ctx.setServletContext(servletContext); ctx.refresh();Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));  dynamic.addMapping("/");  dynamic.setLoadOnStartup(1);dynamic.setMultipartConfig(ctx.getBean(MultipartConfigElement.class));}  
    }  

    View: Use of enctype="multipart/form-data" and type="file"

    The JSPs are being used as views. To support file upload, form must be set with enctype for multipart form data and there should a file input text. We have two JSP. Find the JSP for single file upload. 
    singleUpload.jsp
    <html>
    <body>
    <h1>Single File Upload</h1><form method="post" enctype="multipart/form-data" action="singleSave">Upload File: <input type="file" name="file"><br /><br />Description: <input type="text" name="desc"/><br/><br/><input type="submit" value="Upload"> </form>
    </body>
    </html> 
    Find the JSP for multiple file Upload. We need to take care that each input type of file type must have same name, so that it could be accessed as an array. 
    multipleUpload.jsp
    <html>
    <body><h1> Multiple File Upload </h1><form method="post" enctype="multipart/form-data" action="multipleSave">Upload File 1: <input type="file" name="file"> <br/>Upload File 2: <input type="file" name="file"> <br/>Upload File 3: <input type="file" name="file"> <br/>Upload File 4: <input type="file" name="file"> <br/><br /><br /><input type="submit" value="Upload"> </form>
    </body>
    </html> 

    Spring Boot Jar Dependency using Gradle

    Find the gradle script for JAR dependency and creating WAR file of the project. Spring boot web and security are being used. 
    build.gradle
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'war'
    archivesBaseName = 'CP'
    repositories {mavenCentral()
    }
    dependencies {compile 'org.springframework.boot:spring-boot-starter-web:1.1.4.RELEASE'compile 'org.springframework.boot:spring-boot-starter-security:1.1.4.RELEASE'compile 'javax.servlet:jstl:1.2'compile 'commons-fileupload:commons-fileupload:1.3.1'
    }  

    Deploy in Tomcat 7 To Check Output

    Go to build directory in your project and inside lib you will get WAR file. Deploy in in tomcat and test. In our demo we are using tomcat 7. 
    Single File Upload Output
    For single upload use the URL as  http://localhost:8080/CP/singleUpload
    Spring 4 MVC  Single and Multiple File Upload  Example  with Tomcat
    Spring 4 MVC  Single and Multiple File Upload  Example  with Tomcat

    Multiple File Upload Output
    For multiple file upload use the URL as  http://localhost:8080/CP/multipleUpload
    Spring 4 MVC  Single and Multiple File Upload  Example  with Tomcat
    Spring 4 MVC  Single and Multiple File Upload  Example  with Tomcat

    Download Source Code

    spring-4-mvc-single-multiple-file-upload-example-with-tomcat.zip

    这篇关于解决使用Spring Boot、Multipartfile上传文件路径错误问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



    http://www.chinasem.cn/article/576174

    相关文章

    一文详解SpringBoot中控制器的动态注册与卸载

    《一文详解SpringBoot中控制器的动态注册与卸载》在项目开发中,通过动态注册和卸载控制器功能,可以根据业务场景和项目需要实现功能的动态增加、删除,提高系统的灵活性和可扩展性,下面我们就来看看Sp... 目录项目结构1. 创建 Spring Boot 启动类2. 创建一个测试控制器3. 创建动态控制器注

    使用Docker构建Python Flask程序的详细教程

    《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

    Python使用vllm处理多模态数据的预处理技巧

    《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

    Java操作Word文档的全面指南

    《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

    Python使用pip工具实现包自动更新的多种方法

    《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

    Conda与Python venv虚拟环境的区别与使用方法详解

    《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

    Spring Boot中WebSocket常用使用方法详解

    《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

    C#中Guid类使用小结

    《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

    SpringBoot+Docker+Graylog 如何让错误自动报警

    《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

    Python使用python-can实现合并BLF文件

    《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合