欢迎使用CSDN-markdown编辑器

来源:互联网 发布:声音编辑软件手机版 编辑:程序博客网 时间:2024/05/22 00:43
public void fileOutputStream(HttpServletRequest req, HttpServletResponse resp)             throws ServletException, IOException {        String filepath = req.getRequestURI();        int index = filepath.indexOf(Global.USERFILES_BASE_URL);        if(index >= 0) {            filepath = filepath.substring(index + Global.USERFILES_BASE_URL.length());        }        try {            filepath = UriUtils.decode(filepath, "UTF-8");        } catch (UnsupportedEncodingException e1) {            logger.error(String.format("解释文件路径失败,URL地址为%s", filepath), e1);        }        File file = new File(Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL + filepath);        try {            FileCopyUtils.copy(new FileInputStream(file), resp.getOutputStream());            resp.setHeader("Content-Type", "application/octet-stream");            return;        } catch (FileNotFoundException e) {            req.setAttribute("exception", new FileNotFoundException("请求的文件不存在"));            req.getRequestDispatcher("/WEB-INF/views/error/404.jsp").forward(req, resp);        }    }
0 0
原创粉丝点击