欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年08月31日 12:03:44   作者:獨(dú)具匠心  
這篇文章主要介紹了java操作遠(yuǎn)程共享目錄的實(shí)現(xiàn)代碼,非常不粗,具有參考借鑒價(jià)值,需要的朋友可以參考下

一.前言

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

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

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

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

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

/**
  * 方法說(shuō)明:從遠(yuǎn)程共享目錄下載文件
  * @param localDir   本地臨時(shí)路徑
  * @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("\\")+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)程共享目錄

/**
  * 方法說(shuō)明:上傳文件到遠(yuǎn)程共享目錄
  * @param localDir   本地臨時(shí)路徑(A:/測(cè)試/測(cè)試.xls)
  * @param removeDir  遠(yuǎn)程共享路徑(smb://10.169.2.xx/測(cè)試/,特殊路徑只能用/)
  * @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)限驗(yà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 可以在處理大文件時(shí),提升性能
  *
  * @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();
   }
  }
 }

總結(jié)

以上所述是小編給大家介紹的Java遠(yuǎn)程共享目錄的操作代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • RestTemplate請(qǐng)求失敗自動(dòng)重啟機(jī)制精講

    RestTemplate請(qǐng)求失敗自動(dòng)重啟機(jī)制精講

    這篇文章主要為大家介紹了RestTemplate請(qǐng)求失敗自定義處理的方法,自動(dòng)重試的機(jī)制精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多所進(jìn)步,早日升職加薪
    2022-03-03
  • IDEA新建javaWeb以及Servlet簡(jiǎn)單實(shí)現(xiàn)小結(jié)

    IDEA新建javaWeb以及Servlet簡(jiǎn)單實(shí)現(xiàn)小結(jié)

    這篇文章主要介紹了IDEA新建javaWeb以及Servlet簡(jiǎn)單實(shí)現(xiàn)小結(jié),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11
  • java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段

    java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段

    這篇文章主要為大家詳細(xì)介紹了java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Spring?MVC中JSON數(shù)據(jù)處理方式實(shí)戰(zhàn)案例

    Spring?MVC中JSON數(shù)據(jù)處理方式實(shí)戰(zhàn)案例

    Spring MVC是個(gè)靈活的框架,返回JSON數(shù)據(jù)的也有很多五花八門的方式,下面這篇文章主要給大家介紹了關(guān)于Spring?MVC中JSON數(shù)據(jù)處理方式的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • 最新評(píng)論