`

How Java access to shared folder

 
阅读更多
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

public class SMBTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			SmbFile file = new SmbFile(
					"smb://administrator:password@ip/hejian/hello.txt");
			SmbFileInputStream sfis = new SmbFileInputStream(file);
			byte b[] = new byte[2];
			while (-1 != sfis.read(b)) {
				System.out.print(new String(b));
			}
			sfis.close();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SmbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

		}
	}

}

   

 

假如公司有域认证的话,上面的方法就行不通了,不过还有另外一方法:

 

package com.rits.samplescan.smb;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

public class SMBTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String domain = "domain";
		String username = "username";
		String password = "password";
		NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(domain,username,password);
		try {
			SmbFile file = new SmbFile(
					"smb://10.10.10.18/share/hello.txt" ,auth);
			SmbFileInputStream sfis = new SmbFileInputStream(file);
			byte b[] = new byte[2];
			while (-1 != sfis.read(b)) {
				System.out.print(new String(b));
			}
			sfis.close();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SmbException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

		}
	}

}

 

 

0
0
分享到:
评论

相关推荐

    Move to Applications folder

    "Move to Applications folder" 是一个针对Mac应用的实用工具,其主要目的是帮助用户方便地将应用程序移动到Mac的"应用程序"文件夹中。在Mac OS操作系统中,将应用程序放置在"应用程序"文件夹是标准的做法,因为这...

    Easy Lan Folder Share

    As we all know, We often need to share some files including such as documents, video,... All visits are under your surveillance, you can quickly stop any shared folder to prevent the unauthorized access.

    KaKaSoft Folder Protector V5.39(附破解补丁)

    软件名:KaKaSoft Folder Protector V5.39(附破解补丁) 简介:轻松对电脑、U盘、数码相机、移动硬盘等中的文件夹进行加密 使用方法: 1. 双击“Folder Protector V5.39 破解补丁.exe”破解软件 2. 软件...

    fail_to_use_service_name_to_access_share_folder_in_Win2008_cluster

    fail_to_use_service_name_to_access_share_folder_in_Win2008_cluster

    C# to Java 代码转换工具

    Folder conversion: Entire folders of C# code files are converted to Java code files.(文件夹转换) 2. Code snippet and file conversion: Our snippet conversion accuracy is outstanding and does not ...

    解决VC6.0 Add Files to Folder 失效

    这个问题主要体现在"Add Files to Folder"功能失效,即用户无法通过VC6.0向工程中添加新的文件或文件夹。 "Add Files to Folder"是VC6.0中的一个重要功能,它允许开发者将源代码、头文件或其他资源文件添加到项目中...

    安装提示folder depth is too large to run the setup

    ### 安装提示“folder depth is too large to run the setup”的解决方案 在进行软件或应用程序安装过程中,有时会遇到一个令人头疼的问题:“folder depth is too large to run the setup”。这个问题通常发生在...

    Java邮件开发Fundamentals of the JavaMail API

    Instructions on how to download and install the JavaMail API are contained in the course. In addition, you will need a development environment such as the JDK 1.1.6+ or the Java 2 Platform, Standard...

    win7 office2007 删除Groove Folder Synchronization菜单项 视频

    win7 office2007 删除Groove Folder Synchronization菜单项 http://hi.baidu.com/fei126222/blog/item/dde559dc439ae2adcc11661b.html

    java_folder:核心java编程

    【标题】"java_folder:核心java编程"是一个与Java编程相关的资源包,它可能包含了学习和深入理解Java语言核心概念的教程、代码示例或项目。这个资源可能适合初学者和有一定经验的开发者,帮助他们巩固基础知识,...

    External Access Folder - Honeywell Confidential.docx

    External Access Folder - Honeywell Confidential.docx

    使用Java编写脚本来将PDF转换为图像,Java的PDF处理库,Apache PDFBox

    java PDFToImageConverter path_to_pdf_file.pdf path_to_output_folder 请将path_to_pdf_file.pdf替换为您要转换的PDF文件的路径,将path_to_output_folder替换为输出图像的文件夹路径。 脚本将使用Apache PDFBox库...

    Unable to build: the file dx.jar was not loaded from the SDK folder!

    Unknown error: Unable to build: the file dx.jar was not loaded from the SDK folder! 问题的缺失文件 eclipse.buildId=M20100211-1343 java.version=1.6.0_10-rc2 java.vendor=Sun Microsystems Inc. ...

    kill_folder 专杀

    "kill_folder 专杀"工具是一款专门针对名为"kill_folder"的文件病毒设计的安全软件。在深入了解这个工具之前,我们首先要理解什么是文件病毒以及它如何影响计算机系统。 文件病毒是一种恶意软件,它会附着在可执行...

    VC-folder-tree-access.rar_Access VC_tree_vc access_vc tree

    在IT领域,特别是软件开发中,"VC folder tree access" 指的是使用Visual C++(简称VC)来实现对文件系统的目录结构进行树形展示和访问的功能。这个功能常见于许多应用程序,如资源管理器或者自定义的文件操作界面,...

    windows-folder-remark-master.zip

    windows-folder-remark-master.zip windows-folder-remark-master.zip windows-folder-remark-master.zip windows-folder-remark-master.zip windows-folder-remark-master.zip windows-folder-remark-master.zip ...

    File access sample.zip

    This sample shows how to create, read, write, copy and delete a file, how to retrieve file properties, and how to track a file or folder so that your app can access it again. This sample uses Windows....

    文件管理 ToFolder v1.0

    《文件管理 ToFolder v1.0:提升Windows文件操作效率》 在计算机使用过程中,文件管理是一项基础且频繁的任务。Windows操作系统内置的“资源管理器”以其直观的图形用户界面(GUI)为用户提供了丰富的文件操作功能...

    HitPaw.Watermark.Remover V1.3.8最新版本

    一键去除视频和图片水印

    java-folder

    本项目名为"java-folder",可能是一个Java学习资源或项目的代码库。 1. **Java基础知识** - 类与对象:Java是一种面向对象的语言,一切皆为对象。类是创建对象的蓝图,包含数据(属性)和行为(方法)。 - 封装、...

Global site tag (gtag.js) - Google Analytics