我面临着与 link 上相同的问题并没有得到正确的回答。我正在使用下面的代码。
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try{
String url = "smb://DHARMU-PC/" + sharedFolder + "/new plugin.txt";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null);
SmbFile sfile = new SmbFile(url,auth);
if (sfile!=null)
{
Toast.makeText(Main2Activity.this,"Not", Toast.LENGTH_SHORT).show();
sfile.getInputStream();
}
else
{
Toast.makeText(Main2Activity.this, "Succsess", Toast.LENGTH_SHORT).show();
}
} catch (MalformedURLException e) {
Toast.makeText(Main2Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
当我评论 sfile.getInputStream(); 应用程序工作完美但 getinputstream 应用程序在 try catch block 中没有任何异常。由于上述问题没有得到回答,所以我再次提出这个问题。
编辑
现在我尝试使用异步任务
class CopySMBFile extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... f_url) {
try {
String fileContent = "This is a test file";
try{
String user = "myusername:mypassword";
System.out.println("User: " + user);
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
String path = "smb://192.168.1.12/k/temp/A.txt";
System.out.println("Path: " +path);
SmbFile sFile = new SmbFile(path, auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write(fileContent.getBytes());
System.out.println("Finished attempt to write file");
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
Log.e("Connected", e.getMessage());
}
return null;
}
现在使用 Async Task SmbFileOutputStream 可以正常工作,但仍然显示 getinputstream 的 fatal error 。
编辑
发现 Appcrash 背后的原因是 networkmainthread 异常。现在请指导解决。
最佳答案
我认为 NtlmPasswordAuthentication
调用是错误的。它期望字符串为 host;user:password
。在您的情况下,它应该是 192.168.1.12;myusername:mypassword
。您还可以使用构造函数的 3 参数版本:NtlmPasswordAuthentication("192.168.1.12","myusername","mypassword")
。
关于java - SMB getinputstream 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48643736/