我想将文件上传到项目中的文件夹。但是当我选择一个文件并单击上传按钮时,我收到以下错误: HTTP状态500 - 请求处理失败;嵌套异常是java.lang.IllegalArgumentException:预期的MultipartHttpServletRequest:是否配置了MultipartResolver?
这是我的MultipartResolver bean:
<beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<beans:property name="maxUploadSize" value="10000000"/>
</beans:bean>
这是我的控制器:
@Controller
public class UploadController {
private String folderTujuanUpload="/uploads";
private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);
@RequestMapping(value = "/upload/form",method = RequestMethod.GET)
public void tampilkanFormUpload(){}
@RequestMapping(value = "/upload/form",method = RequestMethod.POST)
public String prosesFormUpload(HttpSession session, @RequestParam("filegambar") MultipartFile hasilUpload) throws IOException{
String lokasiUpload = tujuanUpload(session).getAbsolutePath();
LOGGER.debug("name File [{}]",hasilUpload.getName());
LOGGER.debug("Size File [{}]",hasilUpload.getSize());
LOGGER.debug("Type File [{}]",hasilUpload.getContentType());
File tujuan = new File(lokasiUpload + File.separator + hasilUpload.getOriginalFilename());
try{
hasilUpload.transferTo(tujuan);
}catch (Exception ex) {
LOGGER.error("cant upload file");
LOGGER.error("Penyebab : {}", ex.getMessage());
}
return "redirect:list";
}
@RequestMapping(value = "/upload/list")
public ModelMap tampilkanhasilUpload(){
ModelMap mm=new ModelMap();
return mm;
}
private File tujuanUpload(HttpSession session){
String lokasiFullPath = session.getServletContext().getRealPath(folderTujuanUpload);
LOGGER.debug("lokasi full path : [{}]",lokasiFullPath);
File hasil = new File(lokasiFullPath);
if (!hasil.exists())
{
LOGGER.debug("lokasiblabla");
hasil.mkdirs();
}
return hasil;
}
}
这是我拥有的jsp页面:
form.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>form upload</h1>
<form method="POST" enctype="multipart/form-data">
<table border="0">
<tbody>
<tr>
<td>pilla palla </td>
<td>
<input type="file" name="filegambar">
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" value="Üpload">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
的List.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>upload</title>
</head>
<body>
<h1>hassandsjaiadsf upload</h1>
</body>
</html>