jcifs- 读取网络共享文件图片资源

来源:互联网 发布:淘宝联盟好用吗 编辑:程序博客网 时间:2024/06/10 23:01

最近在做数据同步工作的时候,为了同步远程文件图片资源的想到了使用jcifs,具体的实现步骤

第一步:引入maven依赖

 <dependency><groupId>jcifs</groupId><artifactId>jcifs</artifactId><version>1.3.17</version></dependency> 

第二步:编写程序

       @Autowiredprivate Environment env;/** * 处理商品图片 * @param doType  处理的类型:1表示商品的图片,2表示商品类型的图片 * @throws IOException  *  */public  void remoteLoadImge(String doType) throws IOException {OutputStream out = null;String  localProjectPath =  env.getProperty("localProjectPath");String remotePhotoUrl = env.getProperty("remotePhotoUrl");String savePath = localProjectPath+"ProductionPhoto\\";//存储图片的路径String remoteUrl = remotePhotoUrl+"/ProductionPhoto/";// 共享图片的URL// 获取共享图片的URLSmbFile remoteFile = new SmbFile(remoteUrl);SmbFile[] files = remoteFile.listFiles();for (SmbFile file : files) {InputStream is = file.getInputStream();out = new BufferedOutputStream(new FileOutputStream(savePath +file.getName()));byte[] buffer = new byte[4096];int len = 0; while ((len = is.read(buffer, 0, buffer.length)) != -1) {out.write(buffer, 0, len);}out.flush(); //刷新缓冲的输出流is.close();out.close();}}
此处的远程文件资源的URL和存储图片的路径可以配置在properties文件,便于维护,例如:

remotePhotoUrl = smb://administrator:12121@192.168.0.231/upload/

localProjectPath = E:\\projectes\\supermarket\\supermarkethj\\0.0.1\\report-parent\\report-core\\upload\\

0 0
原创粉丝点击