Csv file is read using byte stream

// csv下载
    @RequestMapping(value = "/selectTaskDownFile.do", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<byte[]> selectTaskDownFile(HttpServletRequest req, HttpServletResponse res) {

        Map<String, Object> map = new HashMap<>();
        String path = req.getParameter("path");
        try {

            // ServletContext servletContext = req.getServletContext();
            String fileName = path.replaceAll("\\\\", "/").split("/")[path.replaceAll("\\\\", "/").split("/").length
                    -. 1 ];
             // String = ServletContext.getRealPath realpath (path); // get the file location 
            String realpath = path.replaceAll ( "\\\\", "/"); // get the file location 
            InputStream in = new new the FileInputStream ( new new file (realpath)); // the file on the input stream into 
            byte [] body = null ; 
            body = new new  byte [in.available ()]; // next call returns on this input stream the method may input stream without blocking read (or skipped) estimated remaining bytes 
            in.read (body); // read to the input stream which 
            fileName = new new String(fileName.getBytes("gbk"), "iso8859-1");// 防止中文乱码
            HttpHeaders headers = new HttpHeaders();// 设置响应头
            headers.add("Content-Disposition", "attachment;filename=" + fileName);
            HttpStatus statusCode = HttpStatus.OK;// 设置响应吗
            ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

 

Guess you like

Origin www.cnblogs.com/tanada/p/11386834.html