在控制器中,可以通过在方法参数中加入一个request参数就可以得到当前的request对象
然后通过调用request.getSession().getServletContext().getRealPath("/")可以得到web应用在系统中的绝对路径
该绝对路径类似: E:/skyway/Skyway Visual Perspectives CE/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/nbinfo-Web/
------------------------代码如下:-----------------------------
@Scope("singleton")
@Controller("DownloadController")
public class DownloadUploadController{
@RequestMapping(value = "/upload.do", method = RequestMethod.POST)
public String handleFormUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file,HttpServletRequest request ) throws IOException {
if (!file.isEmpty()) {
byte[] bytes = file.getBytes();
FileOutputStream fos = new FileOutputStream(request.getSession().getServletContext().getRealPath("/") + name);
fos.write(bytes);
fos.flush();
fos.close();
return "redirect:uploadSuccess";
} else {
return "redirect:uploadFailure";
}
}
}
------------------------------------------------------------------