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

2022-05-05 00:00:00 路径 返回 流到

因为客户机的访问不到政务云存储的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
		}
	}

前端视频代码

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

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

    原文作者:xujiahn
    原文地址: https://blog.csdn.net/xujiahn/article/details/121561330
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章