/*** 方法说明:上传文件到远程共享目录
*@paramlocalDir 本地临时路径(A:/测试/测试.xls)
*@paramremoveDir 远程共享路径(smb://10.169.2.xx/测试/,特殊路径只能用/)
*@paramremoveIp 远程共享目录IP(10.169.2.xx)
*@paramremoveLoginUser 远程共享目录用户名(user)
*@paramremoveLoginPass 远程共享目录密码(password)
*@return*@throwsException 0成功/-1失败*/
public static intsmbUploading(String localDir, String removeDir,
String removeIp, String removeLoginUser, String removeLoginPass)throwsException {
NtlmPasswordAuthentication auth= null;
OutputStream out= null;int retVal = 0;try{
File dir= newFile(localDir);if (!dir.exists()) {
dir.mkdirs();
}
InetAddress ip=InetAddress.getByName(removeIp);
UniAddress address= newUniAddress(ip);//权限验证
auth = newNtlmPasswordAuthentication(removeIp, removeLoginUser, removeLoginPass);
SmbSession.logon(address,auth);//远程路径判断文件文件路径是否合法
SmbFile remoteFile = new SmbFile(removeDir +dir.getName(), auth);
remoteFile.connect();if(remoteFile.isDirectory()){
retVal= -1;
}//向远程共享目录写入文件
out = new BufferedOutputStream(newSmbFileOutputStream(remoteFile));
out.write(toByteArray(dir));
}catch(UnknownHostException e) {
retVal= -1;
e.printStackTrace();
}catch(MalformedURLException e) {
retVal= -1;
e.printStackTrace();
}catch(SmbException e) {
retVal= -1;
e.printStackTrace();
}catch(IOException e) {
retVal= -1;
e.printStackTrace();
}finally{if (out != null) {try{
out.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}returnretVal;
}/*** Mapped File way MappedByteBuffer 可以在处理大文件时,提升性能
*
*@paramfile 文件
*@return字节数组
*@throwsIOException IO异常信息*/@SuppressWarnings("resource")public static byte[] toByteArray(File file) throwsIOException {
FileChannel fc= null;try{
fc= new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer byteBuffer= fc.map(MapMode.READ_ONLY, 0,
fc.size()).load();byte[] result = new byte[(int) fc.size()];if (byteBuffer.remaining() > 0) {
byteBuffer.get(result,0, byteBuffer.remaining());
}returnresult;
}catch(IOException e) {
e.printStackTrace();throwe;
}finally{try{
fc.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}