Spring,控制器中使用request及应用绝对路径

来源:互联网 发布:CMS豹弩游侠技能 编辑:程序博客网 时间:2024/05/18 22:47

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

    }

}

------------------------------------------------------------------

原创粉丝点击