从网络目标位置读写数据的方法

/*
 * Created on 2010-7-14
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package com.intl.cigna.ecommerce.esales.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.Date;

import org.apache.log4j.Logger;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

/**
 * @author lxxiex
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class CopyNetFileUtil {
    private static final Logger logger =
        Logger.getLogger(CopyNetFileUtil.class);

    /**
     * sample
     * @param inputPath e:/remarks.xls
     * @param outputPath /shared/ocean/remarks.xls
     * @throws Exception
     */
    public static void writeShareFile(
        String userName,
        String passWd,
        String ip,
        String inputPath,
        String outputPath)
        throws Exception {
        try {
            SmbFile smbFile =
                new SmbFile(
                    "smb://" + userName + ":" + passWd + "@" + ip + outputPath);
            System.out.println(smbFile.getPath());
            File file = new File(inputPath);
            FileInputStream fi = new FileInputStream(file);
            byte buffer[] = new byte[2048];
            SmbFileOutputStream out = new SmbFileOutputStream(smbFile);
            while (fi.read(buffer) != -1) {
                out.write(buffer);
            }
            fi.close();
            out.flush();
            out.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
            logger.error(e);
            throw e;
        }

    }

    public static void readFromSmb(
        String userName,
        String passWd,
        String ip,
        String smbFilePath,
        String localpath)
        throws Exception {
        File localfile = null;
        InputStream bis = null;
        OutputStream bos = null;
        String smbMachine = null;
        try {
            smbMachine =
                "smb://" + userName + ":" + passWd + "@" + ip + smbFilePath;
            SmbFile rmifile = new SmbFile(smbMachine);
            if (!rmifile.exists()) {
                throw new Exception("源文件不存在!");
            }
            String filename = rmifile.getName();
            bis = new BufferedInputStream(new SmbFileInputStream(rmifile));
            localfile = new File(localpath + filename);
            logger.debug("localfile==" + localfile);
            bos = new BufferedOutputStream(new FileOutputStream(localfile));
            byte[] buffer = new byte[2048];
            while (bis.read(buffer) != -1) {
                bos.write(buffer);
            }
            bos.flush();
        } catch (Exception e) {
            logger.info(e.getMessage());
            throw e;
        } finally {
            try {
                if (bos != null)
                    bos.close();
                if (bis != null)
                    bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        String network_output_ip = "10.131.20.24";
        String network_output_id = "coe";
        String network_output_password = "coe123";
        String network_output_path = "/ca1jettbatch/test.txt";

        try {
            CopyNetFileUtil.readFromSmb(
                network_output_id,
                network_output_password,
                network_output_ip,
                network_output_path,
                "d:/");
            //            CopyNetFileUtil.writeShareFile(
            //                network_output_id,
            //                network_output_password,
            //                network_output_ip,
            //                "d:/t.xml",
            //                network_output_path);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值