/*
* 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();
}
}
}