在控制器中,可以通過在方法參數中加入一個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";
}
}
}
------------------------------------------------------------------