App執行出現畫面
Samba smb Server Android Java
打開activity_main.xml檔案加入以下程式碼
activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <TextView
- android:id="@+id/txtinfo"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="hello_world"
- android:textSize="25sp"
- tools:ignore="MissingConstraints"
- android:typeface="sans" />
- <ProgressBar
- android:id="@+id/pbbar"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- tools:ignore="MissingConstraints" />
- </androidx.constraintlayout.widget.ConstraintLayout>
複製代碼
編輯MainActivity.java
- package com.chiunwu.sambatest;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import jcifs.smb.NtlmPasswordAuthentication;
- import jcifs.smb.SmbFile;
- public class MainActivity extends AppCompatActivity {
- TextView txtinfo;
- ProgressBar pbbar;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- txtinfo = (TextView) findViewById(R.id.txtinfo);
- pbbar = (ProgressBar) findViewById(R.id.pbbar);
- pbbar.setVisibility(View.GONE);
- MyCopy my = new MyCopy();
- my.execute("");
- }
- private class MyCopy extends AsyncTask<String, String, String> {
- @Override
- protected void onPreExecute() {
- // TODO Auto-generated method stub
- pbbar.setVisibility(View.VISIBLE);
- }
- @Override
- protected void onPostExecute(String r) {
- txtinfo.setText(r);
- pbbar.setVisibility(View.GONE);
- }
- @Override
- protected String doInBackground(String... params) {
- // TODO Auto-generated method stub
- String z = "";
- try {
- for (int y = 0; y < 100; y++) {
- String url = "smb://192.168.1.51/aaa/"
- + String.valueOf(y) + ".txt";
- NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
- null, "aaa", "web1234");
- SmbFile sfile = new SmbFile(url, auth);
- Thread.sleep(100);
- if (!sfile.exists()) {
- sfile.createNewFile();
- z = "Created the file for you!!!!";
- } else
- z = "Already exists at the specified location!!!!";
- }
- } catch (Exception ex) {
- // TODO: handle exception
- z = ex.getMessage().toString();
- }
- return z;
- }
- }
- }
複製代碼 AndroidManisfest.xml 設置權限- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
複製代碼
build.gradle加入implementation 'com.jcraft:jsch:0.1.51'- dependencies {
- implementation fileTree(dir: 'libs', include: ['*.jar'])
- implementation 'androidx.appcompat:appcompat:1.1.0'
- implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
- testImplementation 'junit:junit:4.12'
- androidTestImplementation 'androidx.test.ext:junit:1.1.1'
- androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
- implementation 'com.jcraft:jsch:0.1.51'
- }
複製代碼
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物聯網
|