`

http请求后直接下载数据

 
阅读更多

@RequestMapping("/public/download")

public void download(String id, HttpServletRequest request, HttpServletResponse res) throws IOException{

     Attachment att = dbServices.getEntity(Attachment.class, id ) ;

     String fileName = att.getFileName();

    OutputSteam os = res.getOutputStream() ;

    String path = att.getFileDir() + "/" + att.getPhysicalName() ;

    try{

         res.reset() ;

         res.setHeader("Content-Disposition", "attachment;filename=" + new           String(fileName.getBytes("GB2312"),"iso-8859-1"));

         res.setContentType("application/octet-stream;charset=utf-8");

         os.write(FileUtils.readFileToByteArray(new File(path)));

         os.flush();

    } finally{

        if(os != null){

          os.close();

       }

    }

 

}

 

  • 大小: 73.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics