response打印字节和字符串的方法区别

response打印字节和字符串的方法区别

 response中的内容只要mvc返回了就自动会返回页面,在相应的结构可以看到,+return null;或者return;

用response.getWriter().print("未找到图片");/////////////打印普通字符或者response.getOutputStream().write(bytes,0,length);///打印流

这是自动当着页面返回请求页(下载之类)

 

@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("未找到图片");/////////////打印普通字符

        }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);///打印流

            }

        }

    }

 

猜你喜欢

转载自yuhuiblog6338999322098842.iteye.com/blog/2352576