TShopping

 找回密碼
 註冊
搜索
查看: 2536|回復: 0

[教學] 手機如何用samba連線到server創建檔案

[複製鏈接]
發表於 2020-8-2 16:52:09 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
App執行出現畫面

Samba smb Server Android Java

Samba smb Server Android Java


打開
activity_main.xml檔案加入以下程式碼
activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context=".MainActivity">

  8.     <TextView
  9.         android:id="@+id/txtinfo"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:text="hello_world"
  13.         android:textSize="25sp"
  14.         tools:ignore="MissingConstraints"
  15.         android:typeface="sans" />

  16.     <ProgressBar
  17.         android:id="@+id/pbbar"
  18.         android:layout_width="wrap_content"
  19.         android:layout_height="wrap_content"
  20.         tools:ignore="MissingConstraints" />

  21. </androidx.constraintlayout.widget.ConstraintLayout>
複製代碼



編輯MainActivity.java
  1. package com.chiunwu.sambatest;

  2. import androidx.appcompat.app.AppCompatActivity;

  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.ProgressBar;
  7. import android.widget.TextView;

  8. import jcifs.smb.NtlmPasswordAuthentication;
  9. import jcifs.smb.SmbFile;

  10. public class MainActivity extends AppCompatActivity {
  11.     TextView txtinfo;
  12.     ProgressBar pbbar;
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.         txtinfo = (TextView) findViewById(R.id.txtinfo);
  18.         pbbar = (ProgressBar) findViewById(R.id.pbbar);
  19.         pbbar.setVisibility(View.GONE);

  20.         MyCopy my = new MyCopy();
  21.         my.execute("");
  22.     }

  23.     private class MyCopy extends AsyncTask<String, String, String> {

  24.         @Override
  25.         protected void onPreExecute() {
  26.             // TODO Auto-generated method stub
  27.             pbbar.setVisibility(View.VISIBLE);

  28.         }

  29.         @Override
  30.         protected void onPostExecute(String r) {
  31.             txtinfo.setText(r);
  32.             pbbar.setVisibility(View.GONE);
  33.         }

  34.         @Override
  35.         protected String doInBackground(String... params) {
  36.             // TODO Auto-generated method stub
  37.             String z = "";
  38.             try {

  39.                 for (int y = 0; y < 100; y++) {
  40.                     String url = "smb://192.168.1.51/aaa/"
  41.                             + String.valueOf(y) + ".txt";

  42.                     NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
  43.                             null, "aaa", "web1234");
  44.                     SmbFile sfile = new SmbFile(url, auth);
  45.                     Thread.sleep(100);
  46.                     if (!sfile.exists()) {
  47.                         sfile.createNewFile();
  48.                         z = "Created the file for you!!!!";
  49.                     } else
  50.                         z = "Already exists at the specified location!!!!";
  51.                 }
  52.             } catch (Exception ex) {
  53.                 // TODO: handle exception
  54.                 z = ex.getMessage().toString();
  55.             }
  56.             return z;
  57.         }
  58.     }
  59. }
複製代碼
AndroidManisfest.xml 設置權限
  1. <uses-permission android:name="android.permission.INTERNET" />
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  3. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  4. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
複製代碼

build.gradle加入implementation 'com.jcraft:jsch:0.1.51'
  1. dependencies {
  2.     implementation fileTree(dir: 'libs', include: ['*.jar'])

  3.     implementation 'androidx.appcompat:appcompat:1.1.0'
  4.     implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  5.     testImplementation 'junit:junit:4.12'
  6.     androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  7.     androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
  8.     implementation 'com.jcraft:jsch:0.1.51'
  9. }
複製代碼

Samba Server 加入 aaa帳號
即可,不懂可參閱此篇
https://josephjsf2.github.io/linux/2019/11/01/share_centos_folder_with_windows.html

參考文章
https://parallelcodes.com/android-samba-sharing-creating-files/
來源http://www.netyea.com
#網頁設計 #網站架設 #關鍵字優化 #網頁優化 #App程式設計 #AIOT物聯網



 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-3-28 23:03 , Processed in 0.074839 second(s), 26 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表