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);