Java使用SAMBA协议实现上传和下载功能

背景:
对于系统来说,用户的数据和用户相关的文件一般都会分离放置,如果这个文件会被多个系统去调用,那么就会把这部分文件放到一个大家都可以拿到的地方,方便大家调阅,具体的实现有很多种,SAMBA就是其中一种。
说明:
SAMBA也是一种服务,它相当于是给大家提供了一个共享目录,消费者可以通过调用接口去存放文件和获取文件。
以下部分是Java实现上传和下载的部分:
首先引入需要的jar包

<dependency>
<groupId>org.samba.jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.14-kohsuke-1</version>
</dependency>

        @Test
	public void testPut() {
		try {
			String ip = "localhost";//SAMBA服务的ip
                        String name="test";//SAMBA服务的域名
                        String username="123";//请求时需要的用户名
                        String Pwd="123";//请求时需要的密码
			UniAddress ua = UniAddress.getByName(ip);
			NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(name, username, username);
			SmbSession.logon(ua, auth);//验证是否能够成功登录
			//创建Smb文件,地址一定要使用smb://
			SmbFile sf = new SmbFile("smb://"+ip+"/samba/test.txt", auth);
			IOUtils.copyLarge(new FileInputStream("d:/in.txt"), sf.getOutputStream());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SmbException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

        @Test
	public void testGet() {
		try {
			String ip = "localhost";//SAMBA服务的ip
                        String name="test";//SAMBA服务的域名
                        String username="123";//请求时需要的用户名
                        String Pwd="123";//请求时需要的密码
			UniAddress ua = UniAddress.getByName(ip);
			NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(name, username, username);
			SmbSession.logon(ua, auth);//验证是否能够成功登录
			//创建Smb文件,地址一定要使用smb://
			SmbFile sf = new SmbFile("smb://"+ip+"/samba/test.txt", auth);
			IOUtils.copyLarge(sf.getInputStream(), new FileOutputStream("d:/out.txt"));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SmbException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

以上的下载文件是下载文件到本地(即服务端),下面我们将其改装为下载到客户端(浏览器端)
*以下仅为示例,ip等参数后期可进行抽取,将此提为工具接口

        @RequestMapping("/downLoad")
	public void downLoad(HttpServletResponse response) {
		try {
			String ip = "localhost";//SAMBA服务的ip
                        String name="test";//SAMBA服务的域名
                        String username="123";//请求时需要的用户名
                        String Pwd="123";//请求时需要的密码
                        String fileName="001.txt";//下载后的文件名
			UniAddress ua = UniAddress.getByName(ip);
			NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(name, username, username);
			SmbSession.logon(ua, auth);//验证是否能够成功登录
			//创建Smb文件,地址一定要使用smb://
			SmbFile sf = new SmbFile("smb://"+ip+"/samba/test.txt", auth);
			InputStream in=new BufferedInputStream(sf.getInputStream(),4096);
                        response.setContentType("application/octet-stream");
                        response.setHeader("Content-Disposition","attachment;filename="+fileName);
                        OutputStream os=new BufferedOutputStream(response.getOutputStream());
                        byte[] bytes=new byte[4096];
                        int i=0;
                        while((i=in.read(bytes))>0){
                          os.write(bytes,0,i);
                        }
                        os.flush();
                        in.close();
                        os.close();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SmbException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

发表回复

您的电子邮箱地址不会被公开。

13 − 2 =