---
title: "Spring,控制器中使用request及应用绝对路径"
author: "feixingfei"
date: "2010-04-03T02:03:00Z"
tags: []
slug: "spring-request"
draft: false
lastmod: "2011-12-22T12:43:56Z"
id: 1320672
url: "https://feixingfei.iteye.com/blog/1320672"
---

在控制器中，可以通过在方法参数中加入一个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";  
}

}

}

\------------------------------------------------------------------