Spring MVC入門–ファイルをアップロードするメモ
1.必要なjar
commons-fileupload-1.3.1.jar
commons-io-2.4.jar
2.設定ファイル
<!– upload settings –>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="102400000″></property>
</bean>
3.アップロードコード
@RequestMapping(value="/upload",method=RequestMethod.POST)
public String upload(HttpServletRequest req) throws Exception{
MultipartHttpServletRequest strMps = (MultipartHttpServletRequest)req;
MultipartFile file = strMps.getFile(“file");
String fileName = file.getOriginalFilename();
SimpleDateFormat sdf = new SimpleDateFormat(“yyyyMMddHHmmss");
FileOutputStream fos = new FileOutputStream(req.getSession().getServletContext().getRealPath(“/")+
“upload/"+sdf.format(new Date())+fileName.substring(fileName.lastIndexOf('.’)));
fos.write(file.getBytes());
fos.flush();
fos.close();
return “upss";
}
4.formコード
<form action="mvc/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file"><br>
<input type="submit" value="submit">
</form>