o file sempre chega null no post
segue codigo
@RequestMapping(method=RequestMethod.POST)
public ModelAndView grava(MultipartFile sumario, @Valid Produto produto, BindingResult result, RedirectAttributes redirectAttributes){
if(result.hasErrors()){
return form(produto);
}
produto.setSumarioPath(fileSaver.write("arquivo-sumario", sumario));
produtoDAO.gravar(produto);
redirectAttributes.addFlashAttribute("sucesso","Producto cadastrado com sucesso");
ModelAndView modelAndView = new ModelAndView("redirect:produtos");
return modelAndView;
}
@Bean
public MultipartResolver multipartResolver(){
return new StandardServletMultipartResolver();
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ tagliburi="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ tagliburi="http://www.springframework.org/tags/form" prefix="form" %>
<%@ tagliburi="http://www.springframework.org/tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Livros...</title>
</head>
<body>
<form:form action="${s:mvcUrl('PC#grava').build() }" method="post" commandName="produto" enctype="multipart/form-data">
<div>
<label>Titulo</label>
<form:input path="titulo" />
<form:errors path="titulo"></form:errors>
</div>
<div>
<label>Descricao</label>
<form:textarea path="descricao" rows="10" cols="20" /></textarea>
<form:errors path="descricao"></form:errors>
</div>
<div>
<label>Data Lançamento</label>
<form:input path="dataLancamento" />
<form:errors path="dataLancamento"></form:errors>
</div>
<div>
<label>Paginas</label>
<form:input path="paginas" />
<form:errors path="paginas"></form:errors>
</div>
<c:forEach items="${tipos}" var="tipoPreco" varStatus="status">
<div>
<label>${tipoPreco}</label>
<form:input path="precos[${status.index}].valor"/>
<form:hidden path="precos[${status.index}].tipo" value="${tipoPreco}"/>
</div>
</c:forEach>
<div>
<label>Sumario</label>
<input type="file" path="sumario" />
</div>
<button type="submit">Cadastrar</button>
</form:form>
</body>
</html>
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class[]{AppWebConfiguration.class, JPAConfiguration.class, FileSaver.class};
}
package br.com.casadocodigo.loja.infra;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@Component
public class FileSaver {
@Autowired
private HttpServletRequest request;
public String write(String basePath, MultipartFile file){
try {
String realPath = request.getServletContext().getRealPath("/"+basePath);
String path = realPath+"/"+file.getOriginalFilename();
file.transferTo(new File(path));
return basePath+"/"+file.getOriginalFilename();
} catch (IllegalStateException | IOException e) {
// TODO Auto-generated catch block
throw new RuntimeException();
}
}
}