如何在我的应用程序中将图像文件发送到 NAS 服务器?
How to send image files to NAS server in my application?
我想发送图片文件(jpg, png)到NAS server in java using smb
我添加了jcifs-1.3.19.jar. as mentioned here and here
编辑:我已经使用这段代码成功地将 jpeg 图像发送到服务器上的共享文件夹,但是速度很慢,发送速度几乎是 1kb/秒。有什么解决办法吗??
static final String USER_NAME = "Waqas";
static final String PASSWORD = "mypass";
static final String NETWORK_FOLDER = "smb://DESKTOP-LAP/Users/Waqas/";
public boolean copyFiles(FileInputStream file, String fileName) {
boolean successful = false;
int cursor;
SmbFileOutputStream sfos;
SmbFile sFile;
String path;
NtlmPasswordAuthentication auth;
try{
String user = USER_NAME + ":" + PASSWORD;
auth = new NtlmPasswordAuthentication(user);
StrictMode.ThreadPolicy tp = StrictMode.ThreadPolicy.LAX; StrictMode.setThreadPolicy(tp);
path = NETWORK_FOLDER + fileName;
sFile = new SmbFile(path, auth);
sfos = new SmbFileOutputStream(sFile);
while((cursor = file.read())!=-1){
sfos.write(cursor);
}
successful = true;
System.out.println("Successful " + successful);
}
catch (Exception e) {
successful = false;
e.printStackTrace();
}
return successful;
}
最后是解决方案:
public boolean copyFiles(FileInputStream file, String fileName) {
boolean successful = false;
int cursor;
SmbFileOutputStream sfos;
SmbFile sFile;
String path;
NtlmPasswordAuthentication auth;
try{
String user = USER_NAME + ":" + PASSWORD;
System.out.println("User: " + user);
auth = new NtlmPasswordAuthentication(user);
StrictMode.ThreadPolicy tp = StrictMode.ThreadPolicy.LAX; StrictMode.setThreadPolicy(tp);
path = NETWORK_FOLDER + fileName+".jpeg";
System.out.println("Path: " +path);
sFile = new SmbFile(path, auth);
sfos = new SmbFileOutputStream(sFile);
long t0 = System.currentTimeMillis();
byte[] b = new byte[8192];
int n, tot = 0;
Log.d("asdf","initiating : total="+tot);
while((n = file.read(b))>0){
sfos.write( b, 0, n );
tot += n;
Log.d("asdf","writing : total="+tot);
}
successful = true;
Log.d("asdf","Successful : total="+tot);
}
catch (Exception e) {
successful = false;
e.printStackTrace();
Log.d("asdf","exxeption ");
}
return successful;
}
我想发送图片文件(jpg, png)到NAS server in java using smb
我添加了jcifs-1.3.19.jar. as mentioned here and here
编辑:我已经使用这段代码成功地将 jpeg 图像发送到服务器上的共享文件夹,但是速度很慢,发送速度几乎是 1kb/秒。有什么解决办法吗??
static final String USER_NAME = "Waqas";
static final String PASSWORD = "mypass";
static final String NETWORK_FOLDER = "smb://DESKTOP-LAP/Users/Waqas/";
public boolean copyFiles(FileInputStream file, String fileName) {
boolean successful = false;
int cursor;
SmbFileOutputStream sfos;
SmbFile sFile;
String path;
NtlmPasswordAuthentication auth;
try{
String user = USER_NAME + ":" + PASSWORD;
auth = new NtlmPasswordAuthentication(user);
StrictMode.ThreadPolicy tp = StrictMode.ThreadPolicy.LAX; StrictMode.setThreadPolicy(tp);
path = NETWORK_FOLDER + fileName;
sFile = new SmbFile(path, auth);
sfos = new SmbFileOutputStream(sFile);
while((cursor = file.read())!=-1){
sfos.write(cursor);
}
successful = true;
System.out.println("Successful " + successful);
}
catch (Exception e) {
successful = false;
e.printStackTrace();
}
return successful;
}
最后是解决方案:
public boolean copyFiles(FileInputStream file, String fileName) {
boolean successful = false;
int cursor;
SmbFileOutputStream sfos;
SmbFile sFile;
String path;
NtlmPasswordAuthentication auth;
try{
String user = USER_NAME + ":" + PASSWORD;
System.out.println("User: " + user);
auth = new NtlmPasswordAuthentication(user);
StrictMode.ThreadPolicy tp = StrictMode.ThreadPolicy.LAX; StrictMode.setThreadPolicy(tp);
path = NETWORK_FOLDER + fileName+".jpeg";
System.out.println("Path: " +path);
sFile = new SmbFile(path, auth);
sfos = new SmbFileOutputStream(sFile);
long t0 = System.currentTimeMillis();
byte[] b = new byte[8192];
int n, tot = 0;
Log.d("asdf","initiating : total="+tot);
while((n = file.read(b))>0){
sfos.write( b, 0, n );
tot += n;
Log.d("asdf","writing : total="+tot);
}
successful = true;
Log.d("asdf","Successful : total="+tot);
}
catch (Exception e) {
successful = false;
e.printStackTrace();
Log.d("asdf","exxeption ");
}
return successful;
}