JAVA從局域網共享文件夾中下載上傳文件
先下載jar包,
http://jcifs.samba.org/
- 從共享目錄下載文件
- public static void smbGet(String remoteUrl,String localDir) {
- InputStream in = null;
- OutputStream out = null;
- try {
- SmbFile remoteFile = new SmbFile(remoteUrl);
- if(remoteFile==null){
- System.out.println("共享文件不存在");
- return;
- }
- String fileName = remoteFile.getName();
- File localFile = new File(localDir+File.separator+fileName);
- in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
- out = new BufferedOutputStream(new FileOutputStream(localFile));
- byte[] buffer = new byte[1024];
- while(in.read(buffer)!=-1){
- out.write(buffer);
- buffer = new byte[1024];
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- out.close();
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void smbGet(String remoteUrl,String localDir) {
- InputStream in = null;
- OutputStream out = null;
- try {
- SmbFile remoteFile = new SmbFile(remoteUrl);
- if(remoteFile==null){
- System.out.println("共享文件不存在");
- return;
- }
- String fileName = remoteFile.getName();
- File localFile = new File(localDir+File.separator+fileName);
- in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
- out = new BufferedOutputStream(new FileOutputStream(localFile));
- byte[] buffer = new byte[1024];
- while(in.read(buffer)!=-1){
- out.write(buffer);
- buffer = new byte[1024];
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- out.close();
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- 向共享目錄上傳文件
- public static void smbPut(String remoteUrl,String localFilePath) {
- InputStream in = null;
- OutputStream out = null;
- try {
- File localFile = new File(localFilePath);
-
- String fileName = localFile.getName();
- SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName);
- in = new BufferedInputStream(new FileInputStream(localFile));
- out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
- byte[] buffer = new byte[1024];
- while(in.read(buffer)!=-1){
- out.write(buffer);
- buffer = new byte[1024];
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- out.close();
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
遠程url smb://192.168.0.77/test若是須要用戶名密碼就這樣:
smb://username:password@192.168.0.77/test
歡迎關注本站公眾號,獲取更多信息