1
我想作指示的教程here期间的InputStream
一切都显得精细,从使用SFTP远程服务器下载文件下载从远程服务器通过SFTP使用的OutputStream(机器人)的文件,但是当它到达的OutputStream,它崩溃,并给了我一丝错误:
java.io.FileNotFoundException:/内部存储/文件/ sample.txt的: 打开失败:ENOENT(没有这样的文件或目录)
造成者: libcore.io.ErrnoException:打开失败:ENOENT(没有这样的文件或目录 )
任何帮助吗?
这里是我的代码:
public class MainActivity extends Activity {
private static final String SFTPWORKINGDIR = "/path/to/file";
private String user = "username";
private String pass = "password";
private String host = "hostname";
private int portNum = 22;
private String fileName = "sample.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new AsyncTask<Void, Void, List<String>>() {
@Override
protected List<String> doInBackground(Void... params) {
try {
Downloader(fileName);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute();
}
public void Downloader(String fileName) {
JSch jsch = new JSch();
Session session = null;
Channel channel = null;
ChannelSftp sftpChannel = null;
try {
session = jsch.getSession(user, host, portNum);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(pass);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
sftpChannel = (ChannelSftp) channel;
sftpChannel.cd(SFTPWORKINGDIR); //cd to dir that contains file
try {
byte[] buffer = new byte[1024];
BufferedInputStream bis = new BufferedInputStream(sftpChannel.get(fileName));
File newFile = new File("some/file/");
OutputStream os = new FileOutputStream(newFile); //CRASHES HERE
BufferedOutputStream bos = new BufferedOutputStream(os);
int readCount;
while((readCount = bis.read(buffer)) > 0) {
Log.d("Downloading", " " + fileName);
bos.write(buffer, 0, readCount);
}
bis.close();
bos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d(" ", fileName + " has been downloaded. MAYBE");
sftpChannel.exit();
session.disconnect();
} catch (JSchException e) {
e.printStackTrace();
} catch (SftpException e) {
e.printStackTrace();
}
}
}
文件路径应该是整体。 –