MultipartFile.getOriginalFilename () null pointer exception, springMVC file upload

Null pointer appears
First look at the method of the form to see if it is a post request, file upload can only be done in the post form

Second, see if the parameter name of the form upload is the same as the parameter name of the MultipartFile in the method, or use @RequestParam (value = “upload”, required = false) MultipartFile upload

Third, see if the id value of the file upload parser is configured

An error in the basic three will cause a null pointer exception

The path to upload to:
1) The war mode can be called the release mode, as you know from the name, this is first packaged as a war package and then released;
(2) the war exploded mode is to directly folder, jsp page, Classes, etc. are moved to the Tomcat deployment folder to load and deploy. Therefore, this method supports hot deployment, and is generally used in development. // that is uploaded to the target package

My tomcat deploys the war package, so the uploaded files are imported into the webapps folder of the tomcat server, and a virtual directory must be defined, otherwise the uploaded files and folders may not be found

File upload steps:
1. Import the commons-fileupload and commons-io jar packages
2. Configure in the xml of springmvc, use mvc: resources to access static resources

<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10485760"></property>
    </bean>

jsp code part:

<head>
    <title>Title</title>
    <script src="js/jquery-3.5.0.min.js"></script>
</head>
<body>
    <form method="post" action="user/fileUpload" enctype="multipart/form-data">//必须的设置
        选择文件:<input type="file" name="upload" />
        <input type="submit" value="上传" />
    </form>
</body>

Java code part:

package com.wz;

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.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class HelloFirst {

    @RequestMapping(value = "/fileUpload" ,method = RequestMethod.POST)
    public String uploadFile(HttpServletRequest request,
                             @RequestParam(value="upload",required=false) MultipartFile upload) throws IOException {
        //上传的位置
        String path = request.getSession().getServletContext().getRealPath("/uploads/");
        //判断是否存在该文件夹,无则创建
        File file = new File(path);
        if(!file.exists()){
            file.mkdir();
        }

        //获取文件名称
        String filename = upload.getOriginalFilename();
        //把文件名称变成唯一值
        String  uuid = UUID.randomUUID().toString().replace("-","");
        filename = uuid+"_"+filename;
        //完成文件上传
        upload.transferTo(new File(path,filename));
        return "success";
    }
}

Published 43 original articles · praised 2 · visits 979

Guess you like

Origin blog.csdn.net/study_azhuo/article/details/105615069