zoukankan      html  css  js  c++  java
  • JAVA从局域网共享文件夹中下载上传文件以及java访问共享文件夹

      1 package com.xx.test;
      2 
      3 
      4 import java.io.BufferedInputStream;
      5 import java.io.BufferedOutputStream;
      6 import java.io.File;
      7 import java.io.FileInputStream;
      8 import java.io.FileOutputStream;
      9 import java.io.IOException;
     10 import java.io.InputStream;
     11 import java.io.OutputStream;
     12 import jcifs.smb.SmbFile;
     13 import jcifs.smb.SmbFileInputStream;
     14 import jcifs.smb.SmbFileOutputStream;
     15 
     16 /**   
     17  * java访问共享目录
     18  *
     19  * @author 林计钦
     20  * @version 1.0 2013-7-16 上午09:18:38   
     21  */
     22 public class test02 {
     23 
     24     public static void main(String[] args) throws Exception {
     25         //smb://xxx:xxx@192.168.2.188/testIndex/  
     26         //xxx:xxx是共享机器的用户名密码
     27         String url="smb://C1307890:Ivo123@10.20.2.33/CIMPublic/";
     28         SmbFile file = new SmbFile(url);
     29         if(file.exists()){
     30             SmbFile[] files = file.listFiles();
     31             for(SmbFile f : files){
     32                 System.out.println(f.getName());
     33             }
     34         }
     35         
     36         smbGet("smb://C1307890:Ivo123@10.20.2.33/CIMPublic/02 MES/SPC/Spc_Check_IVO_V1_00.ppt", "D:/ap_log");
     37         smbPut("smb://C1307890:Ivo123@10.20.2.33/CIMPublic/", "D:/qra/fileUpload/SQL_JOIN.pptx");
     38         
     39     }
     40     
     41 //    从共享目录下载文件   
     42     @SuppressWarnings("unused")
     43     public static void smbGet(String remoteUrl,String localDir) {       
     44        InputStream in = null;       
     45        OutputStream out = null;       
     46        try {       
     47            SmbFile remoteFile = new SmbFile(remoteUrl);       
     48            if(remoteFile==null){       
     49               System.out.println("共享文件不存在");       
     50               return;       
     51            }       
     52            String fileName = remoteFile.getName();       
     53            File localFile = new File(localDir+File.separator+fileName);       
     54            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));       
     55            out = new BufferedOutputStream(new FileOutputStream(localFile));          
     56            byte[] buffer = new byte[1024];       
     57            while(in.read(buffer)!=-1){       
     58               out.write(buffer);       
     59               buffer = new byte[1024];       
     60            }       
     61        } catch (Exception e) {       
     62            e.printStackTrace();       
     63        } finally {       
     64            try {       
     65               out.close();       
     66               in.close();       
     67            } catch (IOException e) {       
     68               e.printStackTrace();       
     69            }       
     70        }       
     71     }  
     72     
     73    //向共享目录上传文件     
     74     public static void smbPut(String remoteUrl,String localFilePath) {       
     75        InputStream in = null;       
     76        OutputStream out = null;       
     77        try {       
     78            File localFile = new File(localFilePath);       
     79                  
     80            String fileName = localFile.getName();       
     81            SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);       
     82            in = new BufferedInputStream(new FileInputStream(localFile));          
     83            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));       
     84            byte[] buffer = new byte[1024];       
     85            while(in.read(buffer)!=-1){       
     86               out.write(buffer);       
     87               buffer = new byte[1024];       
     88            }       
     89        } catch (Exception e) {       
     90            e.printStackTrace();       
     91        } finally {       
     92            try {       
     93               out.close();       
     94               in.close();       
     95            } catch (IOException e) {       
     96               e.printStackTrace();       
     97            }       
     98        }       
     99     }   
    100     
    101 }

     远程url     smb://192.168.0.77/test如果需要用户名密码就这样:
    smb://username:password@192.168.0.77/test

  • 相关阅读:
    如何正确记忆单词
    转:超级通用型分页存储过程
    Delphi报表开发ReportMachine的小计和总计的计算
    DELPHI编程用SQLDMO呈现带进度条的SQL Server数据库Databnse备份!
    datasnap 2010 心跳包,连接断开处理
    合并BPL包图文教程
    Borland DataSnap(MIDAS)三层架构编程中,主细表的处理方式
    网上摘的 杀进程函数
    为RB定制支持参数的自定义函数
    获取一个数据库中的所有表的名称、一个表中所有字段的名称
  • 原文地址:https://www.cnblogs.com/tianhyapply/p/3721381.html
Copyright © 2011-2022 走看看