MultipartFile.getOriginalFilename()空指针异常,springMVC文件上传

出现空指针异常
第一看先看表单的method是不是post请求,文件上传只能在post表单中进行

第二看表单上传的参数name与方法中MultipartFile的参数名是否一致,或者使用 @RequestParam(value=“upload”,required=false) MultipartFile upload

第三看springmvc.xml中文件上传解析器的id值是否配置

基本三个中出现一个错误都会导致空指针异常

上传到的路径:
1)war模式这种可以称之为是发布模式,看名字也知道,这是先打成war包,再发布;
(2)war exploded模式是直接把文件夹、jsp页面 、classes等等移到Tomcat 部署文件夹里面,进行加载部署。因此这种方式支持热部署,一般在开发的时候也是用这种方式。//即上传到target包中

我tomcat部署的war包方式,所以上传的文件导入到了tomcat服务器webapps文件夹中,要定义虚拟目录,否则可能找不到上传的文件和文件夹

文件上传步骤:

1.导入commons-fileupload和commons-io的jar包

<dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

2.springmvc的xml中配置,使用mvc: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代码部分:

<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代码部分:

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";
    }
}

阿里云oss文件上传:
导包

<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactId>aliyun-sdk-oss</artifactId>
    <version>2.8.2</version>
</dependency>

xml配置统一的 ID Secret填写 鼠标悬浮再个人头像后的 AccessKey

<!--阿里云OSS  -->
	<bean id="ossClient" class="com.aliyun.oss.OSSClient">
		<constructor-arg index="0" value="oss-cn-shenzhen.aliyuncs.com"></constructor-arg>
		<constructor-arg index="1" value="AccessKeyID"></constructor-arg>
		<constructor-arg index="2" value="AccessKeySecret"></constructor-arg>
	</bean>
  @Autowired
    private OSSClient ossClient;
    @PostMapping("/oss")
    public String ossUpload(@RequestParam("file")MultipartFile file , String folder) {

        String bucketName ="qingchengwz";
        String fileName =folder+"/"+ UUID.randomUUID()+file.getOriginalFilename();

        try {
            ossClient.putObject(bucketName,fileName,file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "https://"+bucketName+".oss-cn-shenzhen.aliyuncs.com/"+fileName;
    }
  • 12
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值