ssm项目上传报错Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory

Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory

原因:没有引入commons的maven坐标,这个是必须的必!!
不引入这个maven坐标启动不报错,但是上传就有问题了

   <!--添加fileupload依赖-->
    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.3</version>
    </dependency>

ssm上传案例:

第一步springmvc配置文件注册

   <!--4 文件上传配置-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"/>
        <!--最大上传大小:单位是1字节-->
        <property name="maxUploadSize" value="10485760"/>
        <property name="maxInMemorySize" value="40960"/>
    </bean>

第二步,controller案例代码

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@RestController
public class FileController {
    @RequestMapping("/upFile")
    public String upFile(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        //设置文件保存路径
        String path = request.getServletContext().getRealPath("/upload");
        System.out.println("path:" + path);
        File realPath = new File(path);
        if (!realPath.exists()) {
            realPath.mkdir();
        }
        System.out.println("上传的文件地址:" + realPath);
        file.transferTo(new File(realPath + "/" + file.getOriginalFilename()));
        return "localhost:8080/upload/"+file.getOriginalFilename();
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值