java基于jcifs.smb實(shí)現(xiàn)遠(yuǎn)程發(fā)送文件到服務(wù)器
本文實(shí)例為大家分享了java實(shí)現(xiàn)遠(yuǎn)程發(fā)送文件到服務(wù)器的具體代碼,供大家參考,具體內(nèi)容如下
1.依賴的相關(guān)jar包 jcifs-1.3.14.1.jar
2.創(chuàng)建SMB的聲明
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
public class SmbUtil {
// 1. 聲明屬性
private String url = "smb://userName:password@192.168.2.153/mars/";
private SmbFile smbFile = null;
private SmbFileOutputStream smbOut = null;
private static SmbUtil smbUtil = null; // 共享文件協(xié)議
private SmbUtil(String url) {
this.url = url;
this.init();
}
// 2. 得到SmbUtil和連接的方法
public static synchronized SmbUtil getInstance(String url) {
if (smbUtil == null)
return new SmbUtil(url);
return smbUtil;
}
// 3.smbFile連接
public void init() {
try {
System.out.println("開(kāi)始連接...url:" + this.url);
smbFile = new SmbFile(this.url);
smbFile.connect();
System.out.println("連接成功...url:" + this.url);
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.print(e);
} catch (IOException e) {
e.printStackTrace();
System.out.print(e);
}
}
// 4.上傳文件到服務(wù)器
public int uploadFile(File file) {
int flag = -1;
BufferedInputStream bf = null;
try {
this.smbOut = new SmbFileOutputStream(this.url + "/"
+ file.getName(), false);
bf = new BufferedInputStream(new FileInputStream(file));
byte[] bt = new byte[8192];
int n = bf.read(bt);
while (n != -1) {
this.smbOut.write(bt, 0, n);
this.smbOut.flush();
n = bf.read(bt);
}
flag = 0;
System.out.println("文件傳輸結(jié)束...");
} catch (SmbException e) {
e.printStackTrace();
System.out.println(e);
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println(e);
} catch (UnknownHostException e) {
e.printStackTrace();
System.out.println("找不到主機(jī)...url:" + this.url);
} catch (IOException e) {
e.printStackTrace();
System.out.println(e);
} finally {
try {
if (null != this.smbOut)
this.smbOut.close();
if (null != bf)
bf.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return flag;
}
// 5. 在main方法里面測(cè)試
public static void main(String[] args) {
// 服務(wù)器地址 格式為 smb://電腦用戶名:電腦密碼@電腦IP地址/IP共享的文件夾
String remoteUrl = "smb://wangqinghua:wqh123@192.168.2.153/mars/";
String localFile = "F:/開(kāi)關(guān)生產(chǎn)銷售企業(yè)名錄.xls"; // 本地要上傳的文件
File file = new File(localFile);
SmbUtil smb = SmbUtil.getInstance(remoteUrl);
smb.uploadFile(file);// 上傳文件
}
}
需要注意的事項(xiàng):
以上是基于局域網(wǎng),且上傳文件的目錄或者文件夾必須設(shè)置為共享模式。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java上傳文件到服務(wù)器端的方法
- Java上傳文件圖片到服務(wù)器的方法
- 基于HTML5+js+Java實(shí)現(xiàn)單文件文件上傳到服務(wù)器功能
- Java實(shí)現(xiàn)圖片上傳到服務(wù)器并把上傳的圖片讀取出來(lái)
- Java使用SFTP上傳文件到服務(wù)器的簡(jiǎn)單使用
- java實(shí)現(xiàn)將文件上傳到ftp服務(wù)器的方法
- java基于servlet編寫(xiě)上傳下載功能 類似文件服務(wù)器
- Java編程實(shí)現(xiàn)服務(wù)器端支持?jǐn)帱c(diǎn)續(xù)傳的方法(可支持快車、迅雷)
- Java實(shí)現(xiàn)ftp上傳下載、刪除文件及在ftp服務(wù)器上傳文件夾的方法
- JAVA技術(shù)實(shí)現(xiàn)上傳下載文件到FTP服務(wù)器(完整)
相關(guān)文章
Spring boot項(xiàng)目部署到云服務(wù)器小白教程詳解
這篇文章主要介紹了Spring boot項(xiàng)目部署到云服務(wù)器小白教程詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Java實(shí)現(xiàn)解壓zip壓縮包的兩種方法(支持多層級(jí))
壓縮文件在生活中經(jīng)常能用到,在Java中提供了壓縮和解壓縮文件的功能,本文主要介紹了Java實(shí)現(xiàn)解壓zip壓縮包的兩種方法(支持多層級(jí)),感興趣的可以了解一下2024-03-03
Java實(shí)現(xiàn)將彩色PDF轉(zhuǎn)為灰度PDF的示例代碼
本文以Java代碼為例介紹如何實(shí)現(xiàn)將彩色PDF文件轉(zhuǎn)為灰度(黑白)的PDF文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)一下吧2022-03-03
DecimalFormat數(shù)字格式化 0和# 的區(qū)別及說(shuō)明
這篇文章主要介紹了DecimalFormat數(shù)字格式化 0和# 的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Java實(shí)現(xiàn)局域網(wǎng)聊天小程序
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)局域網(wǎng)聊天小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Java中重寫(xiě)和重載的區(qū)別及說(shuō)明
Java語(yǔ)言中的重載和重寫(xiě)是實(shí)現(xiàn)多態(tài)的兩種方式,但他們的實(shí)現(xiàn)方式和規(guī)則有所不同,重載發(fā)生在一個(gè)類中,同名的方法如果有不同的參數(shù)列表,則視為重載,重寫(xiě)則發(fā)生在子類和父類之間,要求子類重寫(xiě)方法和父類被重寫(xiě)方法有相同的返回類型2024-10-10

