本文實(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的聲明
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
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í)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/u011271894/article/details/48708637