zoukankan      html  css  js  c++  java
  • Java实现将文件推送到共享文件夹

    前言

    由于在和第三方公司对接的过程中涉及到附件发送及格式等相关的问题,经讨论后采用共享文件夹的方式,在我们这边产生新的数据时将数据打包发送到共享文件夹下,他们再需要的时候自动从文件夹下读取数据,避免了数据传输出现的其他各种奇怪的问题。

    实现

    引入jar包

        <!-- https://mvnrepository.com/artifact/org.samba.jcifs/jcifs -->
        <dependency>
    	<groupId>org.samba.jcifs</groupId>
    	<artifactId>jcifs</artifactId>
    	<version>1.3.3</version>
        </dependency>
    

    实现代码

        // 共享文件夹所在服务器ip
        private static String USER_DOMAIN = "192.168.xxx.xxx";
        //访问用户
        private static String USER_ACCOUNT = "userxx";
        //访问密码
        private static String USER_PWS = "xxx";
        //共享文件夹地址
        private static final String shareDirectory = "smb://192.168.xxx.xxx/dir";
        //字节长度
        private static final int byteLen = 1024;
    
        /**
         * 
         * @Title smbPut
         * @Description 向共享目录上传文件
         * @Param shareDirectory 共享目录
         * @Param localFilePath 本地目录中的文件路径
         * @date 2019-01-10 20:16
         */
        public void smbPut(String shareDirectory, File localFile) {
            InputStream in = null;
            OutputStream out = null;
            try {
                String fileName = localFile.getName();
                
                // 域服务器验证
                NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER_DOMAIN, USER_ACCOUNT,
                        USER_PWS);
                SmbFile remoteFile = new SmbFile(shareDirectory + "/" + fileName, auth);
                in = new BufferedInputStream(new FileInputStream(localFile));
                out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
                byte[] buffer = new byte[byteLen];
                while (in.read(buffer) != -1) {
                    out.write(buffer);
                    buffer = new byte[byteLen];
                }
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    out.close();
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    初心回归,时光已逝!
  • 相关阅读:
    Django学习(文件上传与下载)
    Django学习(实现登录功能)
    Django学习(实现注册功能)
    Django学习(环境配置)
    Android-Universal-Image-Loader异步加载图片框架学习
    Android自定义控件_View的绘制流程
    Android自定义控件_Canvas分析
    属性方法介绍——View(1)
    Android平滑移动——Scroller类研究
    记录一个前端二级导航栏的淡入淡出效果
  • 原文地址:https://www.cnblogs.com/yin1361866686/p/11642070.html
Copyright © 2011-2022 走看看