Android 使用2.0进行SFTP文件传输

Android 使用2.0进行SFTP文件传输,android,retrofit,sftp,Android,Retrofit,Sftp,Is翻新为我们提供了在SFTP服务器上上载文件的功能,任何人都可以给出任何示例。翻新是一个库,它将您的HTTP API转换为Java接口。这不是为了任何FTP目的。但您可以使用此库使用多部分请求上载文件,例如: private void uploadFile(Uri fileUri) { // create upload service client FileUploadService service = ServiceGenerator.createService(File

Is翻新为我们提供了在SFTP服务器上上载文件的功能,任何人都可以给出任何示例。

翻新是一个库,它将您的HTTP API转换为Java接口。这不是为了任何FTP目的。但您可以使用此库使用多部分请求上载文件,例如:

private void uploadFile(Uri fileUri) {  
// create upload service client
FileUploadService service =
        ServiceGenerator.createService(FileUploadService.class);

// https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
// use the FileUtils to get the actual file by uri
File file = FileUtils.getFile(this, fileUri);

// create RequestBody instance from file
RequestBody requestFile =
        RequestBody.create(
                     MediaType.parse(getContentResolver().getType(fileUri)),
                     file
         );

// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part body =
        MultipartBody.Part.createFormData("picture", file.getName(), requestFile);

// add another part within the multipart request
String descriptionString = "hello, this is description speaking";
RequestBody description =
        RequestBody.create(
                okhttp3.MultipartBody.FORM, descriptionString);

// finally, execute the request
Call<ResponseBody> call = service.upload(description, body);
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call,
                           Response<ResponseBody> response) {
        Log.v("Upload", "success");
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        Log.e("Upload error:", t.getMessage());
    }
});
private void上传文件(Uri fileUri){
//创建上载服务客户端
文件上传服务=
ServiceGenerator.createService(FileUploadService.class);
// https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
//使用FileUtils按uri获取实际文件
File File=FileUtils.getFile(这是fileUri);
//从文件创建RequestBody实例
请求体请求文件=
RequestBody.create(
parse(getContentResolver().getType(fileUri)),
文件
);
//Part还用于发送实际的文件名
多部分主体。部分主体=
MultipartBody.Part.createFormData(“picture”,file.getName(),requestFile);
//在多部分请求中添加另一部分
String descriptionString=“您好,我是description”;
请求主体描述=
RequestBody.create(
okhttp3.MultipartBody.FORM,descriptionString);
//最后,执行请求
Call Call=service.upload(描述,正文);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用,
回应(回应){
Log.v(“上传”、“成功”);
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“上传错误:,t.getMessage());
}
});

}

您好,请纠正问题,因为它是错误的,对于sameok解决方案也是错误的,但我有SFTP服务器,在连接到服务器之前我必须提供用户名和密码,在上面的代码中我应该这样做,我有JCraft library JSH的替代方案,但我想知道我们是否可以使用改型不,您不能使用改型... 使用像@VishalMokal这样的替代品