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);
    }
全部评论

相关推荐

06-27 15:15
长安大学 Java
哈哈哈,你是老六:这种就是培训机构骗钱的
点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务