java操做遠程共享目錄

一.前言數組

     根據客戶反饋,在進行文件下載的時候,新增遠程共享目錄,下載對應的文件到遠程共享目錄,採用經常使用的IO操做模式,提示下載成功,可是客戶去遠程共享目錄查看對應的下載文件,反饋說沒有找到對應的文件。要求系統須要支持上傳遠程共享目錄,爲何有一個這樣的需求?因爲下載的文件涉及到了支付文件,裏面的金額不容許進行修改,若是放在本地路徑有可能會不會出現人爲的修改,通常涉及到錢的問題,客戶都是比較謹慎的,恰好沒有接觸過操做遠程共享目錄的,就google了一下看有沒有對應的操做說明,下面簡單總結一下。app

二.遠程共享目錄操做dom

一、須要下載對應的jcifs-1.3.18.jar,本例子採用3.18版本的,下載連接:https://jcifs.samba.org/性能

二、涉及的主要類是  SmbFile(遠程文件操做類) ,還有就是進行登陸驗證,驗證對應的遠程目錄的合法性的操做,其餘操做就普通的IO流的操做。測試

三、從遠程共享目錄下載文件google

/**
     * 方法說明:從遠程共享目錄下載文件
     * @param localDir         本地臨時路徑
     * @param removeDir        遠程共享路徑
     * @param _fileName        遠程共享文件名
     * @param removeIp         遠程共享目錄IP
     * @param removeLoginUser  遠程共享目錄用戶名
     * @param removeLoginPass  遠程共享目錄密碼
     * @return
     * @throws Exception
     */
    public static int smbDownload(String localDir, String removeDir,
            String _fileName, String removeIp, String removeLoginUser,
            String removeLoginPass) throws Exception {
        InputStream in = null;
        OutputStream out = null;
        try {
            NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
                    removeIp, removeLoginUser, removeLoginPass);
            SmbFile remoteFile = new SmbFile(removeDir + _fileName, auth);
            if (!remoteFile.exists()) {
                return 0;
            }

            File dir = new File(localDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            String fileName = _fileName.substring(_fileName.lastIndexOf("\\")+1, _fileName.length());
            File localFile = new File(localDir + fileName);
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
            out = new BufferedOutputStream(new FileOutputStream(localFile));
            byte[] buffer = new byte[1024];
            while (in.read(buffer) != -1) {
                out.write(buffer);
                buffer = new byte[1024];
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != out) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (null != in) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return 1;
    }

四、上傳文件都遠程共享目錄spa

/**
     * 方法說明:上傳文件到遠程共享目錄
     * @param localDir         本地臨時路徑(A:/測試/測試.xls)
     * @param removeDir        遠程共享路徑(smb://10.169.2.xx/測試/,特殊路徑只能用/)
     * @param removeIp         遠程共享目錄IP(10.169.2.xx)
     * @param removeLoginUser  遠程共享目錄用戶名(user)
     * @param removeLoginPass  遠程共享目錄密碼(password)
     * @return
     * @throws Exception   0成功/-1失敗
     */
    public static int smbUploading(String localDir, String removeDir,
            String removeIp, String removeLoginUser, String removeLoginPass) throws Exception {
        NtlmPasswordAuthentication auth = null;
        OutputStream out = null;
        int retVal = 0;  
        try {
            File dir = new File(localDir);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            
            InetAddress ip = InetAddress.getByName(removeIp); 
            UniAddress address = new UniAddress(ip);
            // 權限驗證
             auth = new NtlmPasswordAuthentication(removeIp, removeLoginUser, removeLoginPass);
            SmbSession.logon(address,auth); 
            
            //遠程路徑判斷文件文件路徑是否合法
            SmbFile remoteFile = new SmbFile(removeDir + dir.getName(), auth);
            remoteFile.connect();       
            if(remoteFile.isDirectory()){ 
                retVal = -1;
            }
        
            // 向遠程共享目錄寫入文件
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
            out.write(toByteArray(dir));
        } catch (UnknownHostException e) {
            retVal = -1;
            e.printStackTrace();
        } catch (MalformedURLException e) {
            retVal = -1;
            e.printStackTrace();
        } catch (SmbException e) {
            retVal = -1;
            e.printStackTrace();
        } catch (IOException e) {
            retVal = -1;
            e.printStackTrace();
        } finally{
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        
        return retVal;
    }
    
    /**
     * Mapped File way MappedByteBuffer 能夠在處理大文件時,提高性能
     *
     * @param file 文件
     * @return   字節數組
     * @throws IOException IO異常信息
     */
    @SuppressWarnings("resource")
    public static byte[] toByteArray(File file) throws IOException {
        FileChannel fc = null;
        try {
            fc = new RandomAccessFile(file, "r").getChannel();
            MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0,
                    fc.size()).load();
            byte[] result = new byte[(int) fc.size()];
            if (byteBuffer.remaining() > 0) {
                byteBuffer.get(result, 0, byteBuffer.remaining());
            }
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                fc.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
相關文章
相關標籤/搜索