SpringMVC文件的上传与下载

@Autowired
    private HttpServletRequest request;
    @RequestMapping(value="upload")
    public String upload(@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException{
         // 文件保存路径 
        String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/"  
                +new Date().getTime() +UUID.randomUUID() +"_"+ file.getOriginalFilename(); 
        // 转存文件 
        file.transferTo(new File(filePath));  
        return "success.jsp";
    }

    @RequestMapping(value="download")
    public void download(HttpServletRequest request,HttpServletResponse response) throws IOException{
         String filename="1.jpg";
         response.setContentType("application/x-msdownload");
         FileInputStream fis = new FileInputStream(
                 new File(request.getSession().getServletContext().getRealPath("/upload/")+filename));
         response.setHeader("Content-Disposition", "attachment;filename="+filename);
         OutputStream os = response.getOutputStream();
         IOUtils.copy(fis,os);
    }
全部评论

相关推荐

挣K存W养DOG:他真的很中意你,为什么不回他
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务