The difference between the method of printing bytes and strings in response

The difference between the method of printing bytes and strings in response

 The content in the response will automatically return to the page as long as mvc returns, as you can see in the corresponding structure, +return null; or return;

Use response.getWriter().print("image not found");///////////// print ordinary characters or response.getOutputStream().write(bytes,0,length);// /print stream

This is automatically returning the request page (download, etc.) in front of the page

 

@RequestMapping(value = "/account/tbCusFirmChg/showImage")

    public void showReportImage(@RequestParam(value = "path") String path,

                                HttpServletRequest request,HttpServletResponse response) throws IOException, NumberFormatException, EsteelException {

//             response.setContentType("image/jpeg");

 

            response.setCharacterEncoding("UTF-8");

//        String filePath= WebConfig.get("filePath");

            String pathBase = StaticVariables.Base_Path;

        File file=null;

        if(path!=null&&!"".equals(path)){

            file=new File(pathBase+path);

        }

        if( file==null||!file.exists()){

                response.getWriter().print("Picture not found");///////////// print normal characters

        }else {

            response.setContentType("image/jpeg");

            FileInputStream fos = new FileInputStream(file);

            byte[] bytes = new byte[1024*1024];

            int length = 0;

            while((length=fos.read(bytes))!=-1){

                response.getOutputStream().write(bytes,0,length);///打印流

            }

        }

    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326367191&siteId=291194637