1# 跨设备文件拷贝 2 3分布式文件系统为应用提供了跨设备文件拷贝的能力,开发者在跨设备跨应用进行文件拷贝时,通过[基础文件接口](../reference/apis-core-file-kit/js-apis-file-fs.md),可跨设备跨应用拷贝文件。例如:多设备数据流转的场景,设备组网互联之后,设备A上的应用可在复制时,将A设备的沙箱文件,拷贝到A设备的分布式路径下,设备B在粘贴的时候,从设备B的分布式路径下,将文件拷贝到对应的沙箱文件中。 4 5## 开发步骤 6 71. 完成分布式组网。 8 首先将需要进行跨设备访问的设备连接到同一局域网中,同帐号认证完成组网。 9 102. 拷贝跨设备文件。 同一应用不同设备之间实现跨设备文件拷贝,只需要将对应的文件放在应用沙箱的分布式文件路径即可。 11 12 将A设备的待拷贝沙箱文件,拷贝到A设备的分布式路径下。 13 14 ```ts 15 import fs from '@ohos.file.fs'; 16 import common from '@ohos.app.ability.common'; 17 import { BusinessError } from '@ohos.base'; 18 import fileUri from '@ohos.file.fileuri'; 19 20 let context = getContext(this) as common.UIAbilityContext; // 获取设备A的UIAbilityContext信息 21 let pathDir: string = context.filesDir; 22 let distributedPathDir: string = context.distributedFilesDir; 23 // 待拷贝文件沙箱路径 24 let filePath: string = pathDir + '/src.txt'; 25 26 try { 27 // 文件不存在时,需要创建文件并写入内容 28 let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 29 fs.writeSync(file.fd, 'Create file success'); 30 fs.closeSync(file); 31 } catch (error: BusinessError) { 32 let err: BusinessError = error as BusinessError; 33 console.error(`Failed to createFile. Code: ${err.code}, message: ${err.message}`); 34 } 35 36 // 获取待拷贝文件uri 37 let srcUri = fileUri.getUriFromPath(filePath); 38 39 // 将待拷贝的沙箱文件,拷贝到分布式目录下 40 let destUri: string = fileUri.getUriFromPath(distributedPathDir + '/src.txt'); 41 42 try { 43 // 将沙箱路径下的文件拷贝到分布式路径下 44 fs.copy(srcUri, destUri).then(()=>{ 45 console.info("Succeeded in copying---. "); 46 console.info("src: " + srcUri + "dest: " + destUri); 47 }).catch((error: BusinessError)=>{ 48 let err: BusinessError = error as BusinessError; 49 console.info(`Failed to copy. Code: ${err.code}, message: ${err.message}`); 50 }) 51 } catch (error: BusinessError) { 52 let err: BusinessError = error as BusinessError; 53 console.error(`Failed to getData. Code: ${err.code}, message: ${err.message}`); 54 } 55 ``` 56 57 B设备在获取A端沙箱文件时,从B设备的分布式路径下将对应的文件拷贝走,以此完成跨设备拷贝。 58 59 ```ts 60 import fs from '@ohos.file.fs'; 61 import common from '@ohos.app.ability.common'; 62 import { BusinessError } from '@ohos.base'; 63 import fileUri from '@ohos.file.fileuri'; 64 65 let context = getContext(this) as common.UIAbilityContext; // 获取设备B的UIAbilityContext信息 66 let pathDir: string = context.filesDir; 67 let distributedPathDir: string = context.distributedFilesDir; 68 // 待拷贝文件的目标沙箱路径 69 let filePath: string = pathDir + '/dest.txt'; 70 71 // 获取目标路径uri 72 let destUri = fileUri.getUriFromPath(filePath); 73 74 // 获取分布式路径下的源文件 75 let srcUri: string = fileUri.getUriFromPath(distributedPathDir + '/src.txt'); 76 77 // 定义拷贝回调 78 let progressListener: fs.ProgressListener = (progress: fs.Progress) => { 79 console.info(`progressSize: ${progress.processedSize}, totalSize: ${progress.totalSize}`); 80 }; 81 let options: fs.CopyOptions = { 82 "progressListener" : progressListener 83 } 84 85 try { 86 // 将分布式路径下的文件拷贝到其他沙箱路径下 87 fs.copy(srcUri, destUri, options).then(()=>{ 88 console.info("Succeeded in copying of paste. "); 89 console.info("src: " + srcUri + "dest: " + destUri); // file://com.example.myapplication/data/storage/el2/distributedfiles/src.txt 90 }).catch((error: BusinessError)=>{ 91 let err: BusinessError = error as BusinessError; 92 console.info(`Failed to copy. Code: ${err.code}, message: ${err.message}`); 93 }) 94 } catch (error: BusinessError) { 95 let err: BusinessError = error as BusinessError; 96 console.error(`Failed to copy. Code: ${err.code}, message: ${err.message}`); 97 } 98 ```