java返回图片流和视频流到前端播放(http路径)

因为客户机的访问不到政务云存储的ip, 所以决定使用后台返回视频和图片流到前端

注意: 如果上边返回的流会导致图片不清晰或者马赛克, 请使用下边注释的代码返回流

前端视频用的是ckplayer

/**
	 * 返回视频流和图片流
	 * @param response
	 * @param imgPath
	 */
	@RequestMapping("/getViewImg1")
	public void execute1(HttpServletResponse response,@RequestParam(value="imgPath") String imgPath){
        //由于数据库存的是绝对路径,之前的老数据只能这样转换了
		imgPath=imgPath.replace("http://oss-cn-a-internal.aliyuncs.com/", "http://oss-cn-yczw-d01-a.yc-ops.com.cn/");
		System.out.println("路径-"+imgPath);
		try {
			
			if(imgPath.indexOf("http")>-1) {
				URL url = null;
		        InputStream input = null;
		        try{
		            url = new URL(imgPath);
		            HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
		            httpUrl.connect();
		            httpUrl.getInputStream();
		            input = httpUrl.getInputStream();
		        }catch (Exception e) {
		            e.printStackTrace();
		            return;
		        }
				response.setContentType(url.openConnection().getContentType());  
				ServletOutputStream out=response.getOutputStream();
				try {
			        byte[] buf = new byte[2048];
			        while(input.read(buf)>=0){
						out.write(buf);
					}
					out.flush();
					out.close();
					
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					if(input!=null){
						try {
					        input.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}	
				
// 如果边返回的流会导致图片不清晰或者马赛克, 请使用下边注释的代码返回流

           // 第一个


                    try {
                    //filePath:图片完整路径
                    URL urls = new URL(imgPath);
                    HttpURLConnection conn =                                 (HttpURLConnection)urls.openConnection();
                    conn.setRequestMethod("GET");
                    conn.setConnectTimeout(50 * 1000);
                    conn.setReadTimeout(50 * 1000);
                    InputStream inStream = conn.getInputStream();
                    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[2048];
                    int len = 0;
                    while( (len=inStream.read(buffer)) != -1 ){
                        outStream.write(buffer, 0, len);
                    }
                    inStream.close();
                    byte data[] = outStream.toByteArray();
                    response.setContentType(urls.openConnection().getContentType());
                    OutputStream os = response.getOutputStream();
                    os.write(data);
                    os.flush();
                    os.close();
                }catch (Exception e){
                    e.printStackTrace();
                }




           // 第二个

				/*
				   URL urlimg = new URL(imgPath);
	              //创建链接对象
	              URLConnection urlConnection = urlimg.openConnection();
	              //设置超时
	              urlConnection.setConnectTimeout(1000);
	              urlConnection.setReadTimeout(5000);
	              urlConnection.connect();
	              //获取流
	              InputStream inputStream = urlConnection.getInputStream();
	              
	              
	              // 判读是mp4格式还是jpg格式
	              
	              String format=imgPath.substring(imgPath.lastIndexOf(".")+1);
	              if(".mp4".equals(format)) {
	            	  response.setContentType("video/mp4"); // 设置返回的文件类型
	            	  response.addHeader("Content-Type", "audio/mp4;charset=UTF-8");
	            	  IOUtils.copy(inputStream, response.getOutputStream());
	            	  response.flushBuffer();
	              }else if(".mp3".equals(format)) {
	            	  response.addHeader("Content-Type", "audio/mpeg;charset=UTF-8");
	            	  IOUtils.copy(inputStream, response.getOutputStream());
	            	  response.flushBuffer();
	              }else if (".jpg".equals(format)) {
	            	  //读取图片
		              BufferedImage bufferedImage = ImageIO.read(inputStream);
		              if (bufferedImage!=null){
		                  //打印图片
		                  ImageIO.write(bufferedImage,format,response.getOutputStream());// 将文件流放入response中
		                  
		              }
		              if(inputStream!=null){
							try {
								inputStream.close();
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}
	              }*/
	             
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

前端视频代码

  • 6
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值