Java 使用OutputStream(android)通过SFTP从远程服务器下载文件
我正在尝试使用SFTP从远程服务器下载一个文件,如教程中所述 在InputStream期间,一切似乎都很好,但当它到达OutputStream时,它崩溃并给我一个跟踪错误: java.io.FileNotFoundException:/Internal Storage/Documents/sample.txt: 打开失败:enoint(没有这样的文件或目录) 原因: libcore.io.ErrnoException:open失败:enoint(没有这样的文件或 目录) 有什么帮助吗 这是我的密码:Java 使用OutputStream(android)通过SFTP从远程服务器下载文件,java,android,sftp,outputstream,Java,Android,Sftp,Outputstream,我正在尝试使用SFTP从远程服务器下载一个文件,如教程中所述 在InputStream期间,一切似乎都很好,但当它到达OutputStream时,它崩溃并给我一个跟踪错误: java.io.FileNotFoundException:/Internal Storage/Documents/sample.txt: 打开失败:enoint(没有这样的文件或目录) 原因: libcore.io.ErrnoException:open失败:enoint(没有这样的文件或 目录) 有什么帮助吗 这是我的密
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();
}
}
}
公共类MainActivity扩展活动{
私有静态最终字符串SFTPWORKINGDIR=“/path/to/file”;
私有字符串user=“username”;
私有字符串pass=“password”;
私有字符串host=“主机名”;
私有int-portNum=22;
私有字符串fileName=“sample.txt”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建异步任务(){
@凌驾
受保护列表doInBackground(无效…参数){
试一试{
下载程序(文件名);
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
}.execute();
}
公共无效下载程序(字符串文件名){
JSch JSch=新的JSch();
会话=空;
通道=空;
ChannelSftp sftpChannel=null;
试一试{
session=jsch.getSession(用户、主机、端口号);
session.setConfig(“StrictHostKeyChecking”、“no”);
session.setPassword(pass);
session.connect();
通道=会话.openChannel(“sftp”);
channel.connect();
sftpChannel=(ChannelSftp)信道;
sftpChannel.cd(SFTPWORKINGDIR);//cd到包含文件的目录
试一试{
字节[]缓冲区=新字节[1024];
BufferedInputStream bis=新的BufferedInputStream(sftpChannel.get(fileName));
File newFile=新文件(“some/File/”;
OutputStream os=newfileoutputstream(newFile);//此处崩溃
BufferedOutputStream bos=新的BufferedOutputStream(os);
整数读取计数;
而((readCount=bis.read(buffer))>0){
Log.d(“下载”和“+文件名);
写入(缓冲区,0,读取计数);
}
二、关闭();
bos.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
Log.d(“,fileName+”已下载。可能”);
sftpChannel.exit();
session.disconnect();
}捕获(JSCHEException e){
e、 printStackTrace();
}捕获(SFTPE例外){
e、 printStackTrace();
}
}
}
我想我可以通过指定外部存储来修复它:
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS);
File newFile = new File(path, "/" + fileName);
我想我可以通过指定外部存储来修复它:
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS);
File newFile = new File(path, "/" + fileName);
文件路径应该是完整的。文件路径应该是完整的。