Spring,控制器中使用request及應用絕對路徑

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

    }

}

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章