java 下载

@RequestMapping(value = "down",method = RequestMethod.GET)
    @ResponseBody
    public void down(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取根目录
        String realPath = request.getSession().getServletContext().getRealPath("/");
     //文件名
        String filename = "com.association.kingsuper_1.1.1_25.apk";
        // GET请求中,参数中包含中文需要自己动手来转换。
        // 当然如果你使用了“全局编码过滤器”,那么这里就不用处理了
        filename = new String(filename.getBytes("ISO-8859-1"), "UTF-8");
      //文件地址
        String filepath = realPath + filename;
        File file = new File(filepath);
        if(!file.exists()) {
            response.getWriter().print("您要下载的文件不存在!");
            return;
        }
        // 所有浏览器都会使用本地编码,即中文操作系统使用GBK
        // 浏览器收到这个文件名后,会使用iso-8859-1来解码
        filename = new String(filename.getBytes("GBK"), "ISO-8859-1");
        response.addHeader("content-disposition", "attachment;filename=" + filename);
        IOUtils.copy(new FileInputStream(file), response.getOutputStream());
    }

猜你喜欢

转载自www.cnblogs.com/SeaWxx/p/10943428.html
今日推荐