Search code examples
javaandroidfileoutputstreamsmbnas

How to send image files to NAS server in my application?


I want to send image files (jpg, png) to NAS server in java using smb

I have added the jcifs-1.3.19.jar. as mentioned here and here

Edit: I have successfully sent jpeg image to a shared folder on server using this code, but the speed is very slow, its sending almost 1kb/second. Any Solution??

    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;
    }

Solution

  • Finally here is the solution:

      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;
            }