java访问共享目录
新一篇: jsf结合myfaces实现文件上传
由于工作需要读取局域网中一台机器的 共享目录中的文件,需要jcifs-1.1.11.jar的支持,使用SMB协议,以下是实现了远程读取并复制到本地,然后删除本地文件的功能: import java.io.BufferedInputStream;
import java.io.BufferedInputStream; import java.io.BufferedOutputStream;
import java.io.BufferedOutputStream; import java.io.File;
import java.io.File; import java.io.FileOutputStream;
import java.io.FileOutputStream; import java.io.IOException;
import java.io.IOException; import java.io.InputStream;
import java.io.InputStream; import java.io.OutputStream;
import java.io.OutputStream; import java.util.Date;
import java.util.Date;
 import jcifs.smb.SmbFile;
import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileInputStream;
 public class TestReadSmb {
public class TestReadSmb { public static void main(String[] args) {
    public static void main(String[] args) { String smbMachine="smb://10.108.23.200/temp/说明文件.txt";
            String smbMachine="smb://10.108.23.200/temp/说明文件.txt"; String localPath="D:/temp";
            String localPath="D:/temp"; File file=readFromSmb(smbMachine,localPath);
            File file=readFromSmb(smbMachine,localPath); removeFile(file);
            removeFile(file); }
    }
 /**
    /** * 从smbMachine读取文件并存储到localpath指定的路径
     * 从smbMachine读取文件并存储到localpath指定的路径 *
     *  * @param smbMachine
     * @param smbMachine *            共享机器的文件,如smb://xxx:xxx@10.108.23.112/myDocument/测试文本.txt,xxx:xxx是共享机器的用户名密码
     *            共享机器的文件,如smb://xxx:xxx@10.108.23.112/myDocument/测试文本.txt,xxx:xxx是共享机器的用户名密码 * @param localpath
     * @param localpath *            本地路径
     *            本地路径 * @return
     * @return */
     */ public static File readFromSmb(String smbMachine,String localpath){
public static File readFromSmb(String smbMachine,String localpath){ File localfile=null;
        File localfile=null; InputStream bis=null;
        InputStream bis=null; OutputStream bos=null;
        OutputStream bos=null; try {
        try { SmbFile rmifile = new SmbFile(smbMachine);
            SmbFile rmifile = new SmbFile(smbMachine); String filename=rmifile.getName();
            String filename=rmifile.getName(); bis=new BufferedInputStream(new SmbFileInputStream(rmifile));
            bis=new BufferedInputStream(new SmbFileInputStream(rmifile)); localfile=new File(localpath+File.separator+filename);
            localfile=new File(localpath+File.separator+filename); bos=new BufferedOutputStream(new FileOutputStream(localfile));
            bos=new BufferedOutputStream(new FileOutputStream(localfile)); int length=rmifile.getContentLength();
            int length=rmifile.getContentLength(); byte[] buffer=new byte[length];
            byte[] buffer=new byte[length]; Date date=new Date();
            Date date=new Date(); bis.read(buffer);
            bis.read(buffer); bos.write(buffer);
            bos.write(buffer);             Date end=new Date();
            Date end=new Date(); int time= (int) ((end.getTime()-date.getTime())/1000);
            int time= (int) ((end.getTime()-date.getTime())/1000); if(time>0)
            if(time>0) System.out.println("用时:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒");
                System.out.println("用时:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒");             } catch (Exception e) {
        } catch (Exception e) { // TODO Auto-generated catch block
            // TODO Auto-generated catch block System.out.println(e.getMessage());
            System.out.println(e.getMessage()); 
             }finally{
        }finally{ try {
            try { bos.close();
                bos.close(); bis.close();
                bis.close(); } catch (IOException e) {
            } catch (IOException e) { //                // TODO Auto-generated catch block
//                // TODO Auto-generated catch block e.printStackTrace();
                e.printStackTrace(); }
            }             }
        } return localfile;
        return localfile; }
    } public static boolean removeFile(File file) {
    public static boolean removeFile(File file) { return file.delete();
        return file.delete(); }
    } }
}
.jpg) 
  
 
                    
                
 

 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号