Java流媒体二进制显示

本文介绍了如何使用Java的UpDownloadClientUtil工具类处理从远程共享计算机获取的二进制流媒体文件,包括图片和音频。通过这个工具,开发者可以方便地下载和显示这些二进制内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UpDownloadClientUtil 工具类

本文获取的文件是从远程共享计算机获取得到的二进制流,如果不需要用到远程获取,可以忽略,运用自己的获取文件方式

/**
 * jcifs.smb.SmbFile  导入jcifs.jar包
 * Title:远程上传下载文件工具类
 * Description: 采用smb技术实现两个功能
 * Company:依万达
 * @author nizg
 * @date 2016-7-8 下午03:35:17
 */
public class UpDownloadClientUtil {

	/**
	 * 上传文件功能
	 * 分布式上传指定共享机器
	 * ConfigUtil 可以通过spring 注解配置
	 * @return int   0:失败  1:成功 
	 */
	public static int uploadToClient(HttpServletResponse repResponse,
			String fileName) {
		/*
		 * SmbFileInputStream in = new SmbFileInputStream(
		 * "smb://nzgUser:121212@172.16.25.136/recordfile/storage/2016-03-20/2015032017341907003O918676636668.wav"
		 * ); byte[] b = new byte[8192]; int n; while((n=in.read(b)) > 0 ) {
		 * System.out.write( b, 0, n ); }
		 */
		// smb://xxx:xxx@172.16.25.136/testDir/
		// xxx:xxx是共享机器的用户名密码
		// repResponse.reset();

		if (fileName != null) {// 传入文件名不能为空!
			String subFile = fileName.substring(0, 4) + "-"
			+ fileName.substring(4, 6) + "-" + fileName.substring(6, 8);

			//获取远程共享机子的信息  ConfigUtil是你获取配置文件的属性值  key---value
			String host = ConfigUtil.get("host");
			String user = ConfigUtil.get("user");
			String password = ConfigUtil.get("password");
			String dir = ConfigUtil.get("recorddir");// /recordfile/storage
			String url = "smb://" + user + ":" + password + "@" + host + "/"
			+ dir + "/" + subFile + "/";

			//连接远程机子  获取文件数据  上传文件
			String fileurl = url + fileName;
			SmbFile sf = null;
			SmbFileInputStream smbIn = null;
			BufferedInputStream bufferedInputStream = null;
			BufferedOutputStream bufferedOutputStream = null;
			OutputStream toClient = null;
			try {
				sf = new SmbFile(fileurl);
				smbIn = new SmbFileInputStream(sf);
				bufferedInputStream = new BufferedInputStream(smbIn);

				toClient = repResponse.getOutputStream();
				bufferedOutputStream = new BufferedOutputStream(toClient);
				byte[] data = new byte[8192];
				int len = 0;
				while ((len = bufferedInputStream.read(data) )!= -1) {
					bufferedOutputStream.write(data,0, len);
				}

				return 1;
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				try {
					if (toClient != null) {
						toClient.close();
					}
					if (bufferedOutputStream != null) {
						bufferedOutputStream.close();
					}
					if (bufferedInputStream != null) {
						bufferedInputStream.close();
					}
					if (smbIn != null) {
						smbIn.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}else {
			System.out.println("传入文件名为空!");
		}
		return -1;

	}


	/**
	 * 下载文件到客户端
	 * @param fileName 下载路径
	 * @return response
	 */
	public static HttpServletResponse getAudio(HttpServletResponse response,String fileName){
		if(fileName!=null){
		//实现缓存于response
		String subFile = fileName.substring(0, 4) + "-"
		+ fileName.substring(4, 6) + "-" + fileName.substring(6, 8);

		//获取远程共享机子的信息
		String host = ConfigUtil.get("host");
		String user = ConfigUtil.get("user");
		String password = ConfigUtil.get("password");
		String dir = ConfigUtil.get("recorddir");// /recordfile/storage/recordfile/RECORD_FILE_DIR/
		String url = "smb://" + user + ":" + password + "@" + host + "/"
		+ dir + "/" + subFile + "/";

		//连接远程机子  获取文件数据  上传文件
		String fileurl = url + fileName;
		SmbFile sf = null;
		SmbFileInputStream smbIn = null;
		InputStream inputStream = null;
		
		try {
			sf = new SmbFile(fileurl);
			smbIn = new SmbFileInputStream(sf);
			inputStream = new BufferedInputStream(smbIn);
			//FileOutputStream out = new FileOutputStream("recruitment/plugin/"+File.separator+fileName);// 文件的存放路径
			response.reset();
            // 设置response的Header
            OutputStream toClient = response.getOutputStream();
            response.setContentType("application/octet-stream");
            byte[] data = new byte[1024];
			int len = 0;
			while ((len = inputStream.read(data) )!= -1) {
				toClient.write(data,0,len);
				//out.write(data,0,len);
				
			}
			
            toClient.close();
            inputStream.close();
			smbIn.close();
			//out.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
		}else{
			System.out.println("传入的文件不可以空");
		}
		return response;   
	}
	/**
	 * 保存文件到本地服务器
	 * @param fileName 文件名
	 * @param savePath 服务器路径
	 */
	public static void getAudiotoClient(String fileName,String savePath){
		//String fileName = request.getParameter("audioID");
		//System.out.println("文件名"+fileName);
		if(fileName!=null){
			//实现缓存于response
			String subFile = fileName.substring(0, 4) + "-"
			+ fileName.substring(4, 6) + "-" + fileName.substring(6, 8);

			//获取远程共享机子的信息
			String host = ConfigUtil.get("host");
			String user = ConfigUtil.get("user");
			String password = ConfigUtil.get("password");
			String dir = ConfigUtil.get("recorddir");// /recordfile/storage/recordfile/RECORD_FILE_DIR/
			String url = "smb://" + user + ":" + password + "@" + host + "/"
			+ dir + "/" + subFile + "/";

			//连接远程机子  获取文件数据  上传文件
			String fileurl = url + fileName;
			SmbFile sf = null;
			SmbFileInputStream smbIn = null;
			InputStream inputStream = null;
			try {
				sf = new SmbFile(fileurl);
				smbIn = new SmbFileInputStream(sf);
				inputStream = new BufferedInputStream(smbIn);
				//String savePath = request.getSession().getServletContext().getRealPath("/upload");
				System.out.println("路径"+savePath);
				File file = new File(savePath);
				//判断上传文件的保存目录是否存在
				if (!file.exists() && !file.isDirectory()) {
					System.out.println(savePath+"目录不存在,需要创建");
					//创建目录
					file.mkdir();
				}
				//消息提示
				DiskFileItemFactory factory = new DiskFileItemFactory();
				ServletFileUpload upload = new ServletFileUpload(factory);
				upload.setHeaderEncoding("UTF-8");
				String filename = sf.getName();
				FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
				byte[] data = new byte[1024];
				int len = 0;
				while ((len = inputStream.read(data) )!= -1) {
					out.write(data,0,len);
				}
				
				inputStream.close();
				smbIn.close();
				out.close();
			}catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			System.out.println("传入的文件不可以空");
		}
	} 

远程获取文件的jar包下载:http://download.youkuaiyun.com/download/luo201227/7739823  如果不需要用到  无需下载

2、

在sturct action里  方式里:
注意:由于getOutputStream()与jsp 的getWriter()  两个方式只能用其中一种   
public class WorkOrderAction extends BaseAction {
//.....
public String getAudio(){
		
		//二进制流执行1  用二进制返回null 或者在jsp 加out.clear();等  因为printWriter 与outputStream二选一
		String fileName = request.getParameter("audioID");
		UpDownloadClientUtil.getAudio(response,fileName);
		
		//文件执行方式2
		/*String path = request.getSession().getServletContext().getRealPath("/upload");
		UpDownloadClientUtil.getAudiotoClient(callinggRecord.getCallingRecord(),path);*/
		return null;//返回null  如果返回SUCCESS;的话 需要在jsp里面加载out.clear().等方法
	}

}
}
3、struct:
<action name="getAudio" class="workorderAction" method="getAudio">
</action>
4、jsp显示:比如音频  图片也是一样
<audio id="audioID" src="<%=basePath %>workorder/getAudio.action?audioID=${audioid}" controls="controls">Your browser does not support the audio tag.</audio>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值