解决使用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

    相关文章

    一文详解如何在idea中快速搭建一个Spring Boot项目

    《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

    Python常用命令提示符使用方法详解

    《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

    Java对异常的认识与异常的处理小结

    《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

    Python并行处理实战之如何使用ProcessPoolExecutor加速计算

    《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

    Python中help()和dir()函数的使用

    《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

    Linux脚本(shell)的使用方式

    《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

    SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

    《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

    Java使用HttpClient实现图片下载与本地保存功能

    《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

    Python中使用uv创建环境及原理举例详解

    《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

    LiteFlow轻量级工作流引擎使用示例详解

    《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF