麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 網(wǎng)站 > 建站經(jīng)驗 > 正文

Java遠(yuǎn)程共享目錄的操作代碼

2019-11-02 14:11:56
字體:
供稿:網(wǎng)友

一.前言

     根據(jù)客戶反饋,在進(jìn)行文件下載的時候,新增遠(yuǎn)程共享目錄,下載對應(yīng)的文件到遠(yuǎn)程共享目錄,采用常用的IO操作模式,提示下載成功,但是客戶去遠(yuǎn)程共享目錄查看對應(yīng)的下載文件,反饋說沒有找到對應(yīng)的文件。要求系統(tǒng)需要支持上傳遠(yuǎn)程共享目錄,為什么有一個這樣的需求?由于下載的文件涉及到了支付文件,里面的金額不允許進(jìn)行修改,如果放在本地路徑有可能會不會出現(xiàn)人為的修改,一般涉及到錢的問題,客戶都是比較謹(jǐn)慎的,剛好沒有接觸過操作遠(yuǎn)程共享目錄的,就google了一下看有沒有對應(yīng)的操作說明,下面簡單總結(jié)一下。

二.遠(yuǎn)程共享目錄操作

1、需要下載對應(yīng)的jcifs-1.3.18.jar,本例子采用3.18版本的,下載鏈接:https://jcifs.samba.org/

2、涉及的主要類是  SmbFile(遠(yuǎn)程文件操作類) ,還有就是進(jìn)行登錄驗證,驗證對應(yīng)的遠(yuǎn)程目錄的合法性的操作,其他操作就普通的IO流的操作。

3、從遠(yuǎn)程共享目錄下載文件

/**  * 方法說明:從遠(yuǎn)程共享目錄下載文件  * @param localDir   本地臨時路徑  * @param removeDir  遠(yuǎn)程共享路徑  * @param _fileName  遠(yuǎn)程共享文件名  * @param removeIp   遠(yuǎn)程共享目錄IP  * @param removeLoginUser 遠(yuǎn)程共享目錄用戶名  * @param removeLoginPass 遠(yuǎn)程共享目錄密碼  * @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("http://")+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; }

4、上傳文件都遠(yuǎn)程共享目錄

/**  * 方法說明:上傳文件到遠(yuǎn)程共享目錄  * @param localDir   本地臨時路徑(A:/測試/測試.xls)  * @param removeDir  遠(yuǎn)程共享路徑(smb://10.169.2.xx/測試/,特殊路徑只能用/)  * @param removeIp   遠(yuǎn)程共享目錄IP(10.169.2.xx)  * @param removeLoginUser 遠(yuǎn)程共享目錄用戶名(user)  * @param removeLoginPass 遠(yuǎn)程共享目錄密碼(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);   // 權(quán)限驗證    auth = new NtlmPasswordAuthentication(removeIp, removeLoginUser, removeLoginPass);   SmbSession.logon(address,auth);    //遠(yuǎn)程路徑判斷文件文件路徑是否合法   SmbFile remoteFile = new SmbFile(removeDir + dir.getName(), auth);   remoteFile.connect();     if(remoteFile.isDirectory()){     retVal = -1;   }   // 向遠(yuǎn)程共享目錄寫入文件   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 字節(jié)數(shù)組  * @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();   }  } }
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 色淫网站免费视频 | 成人男男视频拍拍拍在线观看 | 99视频观看| 青草伊人网 | 综合精品 | 欧美成人精品h版在线观看 国产一级淫片在线观看 | 韩国美女一区 | 黄视频免费在线 | 黄色羞羞视频在线观看 | 成人一级在线 | 在线免费视频a | 欧美日韩爱爱视频 | av色先锋| 黄网站色成年大片免费高 | 国产女做a爱免费视频 | 午夜视频成人 | 72pao成人国产永久免费视频 | 精品中文字幕久久久久四十五十骆 | 久草在线最新免费 | 91久久国产综合精品女同国语 | 成人一级在线 | 国产精品亚洲三区 | 免费国产一级特黄久久 | 一级@片| 日本看片一区二区三区高清 | 毛片免费一区二区三区 | 污片在线观看视频 | 欧美精品一区二区久久 | 国产一区二区三区视频免费 | 黄色小视频在线免费看 | 成人在线视频网 | 成人福利免费在线观看 | 国产69精品久久久久孕妇黑 | www.狠狠插.com | 国产日产精品一区四区介绍 | 一区二区三区日韩 | 日本欧美一区二区三区在线观看 | 久久精品影视 | 操网 | 成人在线观看免费视频 | 欧美日韩亚洲在线 |