SpringMvc org.springframework.web.multipart.MultipartException: The current request is not a multipart reques

运行到multipart file会一直报开指针异常

控制器代码
*因为在注解里required为false的时候name可以获取到,但是每次调用到file都是空指针异常,大概就几个出错原因,在web.xml d中multipartResolver解析器配置的时候id一定要加上,切名字必须时multipartResolver,大都是请求页面的问题,因为spring其实是通过Apache的 commons-fileupload来检测请求是否为文件上传的请求。而commons-fileupload又是通过如下两个条件来判断:

  1. 请求方法必须是 post.
  2. 请求的contentType 必须设置为以 “multipart/” 开头。

把jsp中的请求头设置成
*

meta http-equiv=“Content-Type” content=“multipart/form-data;charset=utf-8”

package controllor;

import java.awt.PageAttributes.MediaType;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.servlet.ModelAndView;
import pojo.Demo;
import pojo.people;
@Controller
public class DemoControllor {
	
	@RequestMapping("upload")
	public String upload(@RequestParam(value="name",required=true)String name,@RequestParam(value="file",required=true)MultipartFile file)
			throws IOException {	
		System.out.println(name);
		System.out.println("1");
		System.out.println(file.isEmpty());
		String fileName=file.getOriginalFilename();
		System.out.println("2");
		System.out.println(name);
		String suffix=fileName.substring(fileName.lastIndexOf("."));
		String uuid=UUID.randomUUID().toString();
		FileUtils.copyInputStreamToFile(file.getInputStream(), new File("C:\\Users\\Qingquan Wu\\Desktop",fileName+suffix));
//		MultipartResolver mr=null;
		return "/login.jsp";
	}

代码

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 扫描注解 -->
<context:component-scan base-package="controllor"></context:component-scan>
<!-- 注解驱动 -->
<!-- org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping -->
<!-- org.springframework.web.context.support.AnnotationConfigWebApplicationContext -->
<mvc:annotation-driven ></mvc:annotation-driven>

<!-- 静态资源 
	如果url请求时/abc/**这种格式,**代表什么就到/js/下找**
-->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
<mvc:resources location="/files/" mapping="/files/**"></mvc:resources>
<!-- multipartResolver解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  <property name="maxUploadSize" value="5242880" />
     <property name="maxInMemorySize" value="4096" />
     <property name="defaultEncoding" value="UTF-8"></property>
</bean>
</beans>

jsp代码

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%
		String path=request.getContextPath();
		String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";					
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Insert title here</title>
</head>
<body>
<form action="upload"  method="post" enctype="multipart/form-data">
	姓名<input type="text" name="name">
	<input type="file" name="file"><br/>
	<input type="submit" value="up">
</form>
</form>
</body>
</html>
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐