文件流形式读取图片

前台js中代码
if(json.data.headPhoto!=null && json.data.headPhoto!=""){
                    	$('#headPhoto').attr('src', contextPath + '/o2oUser/getPhoto?photoURL=' + json.data.headPhoto);
                    }else{
                    	$('#headPhoto').attr('src', basePath + '/dist/img/SF.jpg');
                    }

后端读取文件流

/**
   * 获取图片
   * @param 
   * @return
   * @throws Exception
   */
	@RequestMapping(value = "/getPhoto", method = RequestMethod.GET)
	@ResponseBody
	public void getPhoto(@RequestParam(value = "photoURL") String photoURL, HttpServletResponse response) throws Exception {
		logger.info("获取图片url:" + photoURL);
		InputStream inputStream = null;
		OutputStream os = null;
	    try {
//	    	if(photoURL.endsWith("203_.jpg"))
//	    		photoURL = "E:/INC_SGS_CORE_PIC/20160324/201/755/59-1-715070207621595136_20160330_203_.jpg";
//	    	else if(photoURL.endsWith("201_.jpg"))
//	    		photoURL = "E:/INC_SGS_CORE_PIC/20160324/201/755/60-1-715070207621595137_20160330_201_.jpg";
//	    	else if(photoURL.endsWith("1667_20160324_201_755.jpg"))
//	    		photoURL = "E:/INC_SGS_CORE_PIC/20160324/201/755/30-1-712592950940401667_20160324_201_755.jpg";
//	    	else 
//	    		photoURL = "E:/INC_SGS_CORE_PIC/20160324/201/755/52-1-712839709792927745_20160324_201_755.jpg";
            inputStream = new FileInputStream(new File(photoURL));
            if(photoURL.endsWith(".jpg"))
            	response.setContentType("image/jpeg; charset=GBK");
            else if(photoURL.endsWith(".tif"))
            	response.setContentType("image/tiff; charset=GBK");
            	 
            os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }
        } catch (Exception e) {
        	logger.error("获取图片出错",e);
            e.printStackTrace();
        } finally {
			os.close();
			inputStream.close();
		}
	}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值