Как отправить файлы изображений на сервер NAS в моем приложении?

Я хочу отправить файлы изображений (jpg, png) на NAS-сервер в java, используя smb

Я добавил jcifs-1.3.19.jar< /а>. как указано здесь и здесь

Редактировать: я успешно отправил изображение в формате jpeg в общую папку на сервере, используя этот код, но скорость очень низкая, его отправка почти 1 КБ/сек. Любое решение??

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

person Waqas K    schedule 24.12.2017    source источник
comment
В чем проблема просто отправить файл jpg? Вы можете использовать класс inputStream для чтения файла и отправки данных на NAS с помощью класса SmbFileOutputStream.   -  person Eliad Cohen    schedule 25.12.2017
comment
Я обновил код, пожалуйста, проверьте его сейчас   -  person Waqas K    schedule 26.12.2017


Ответы (1)


Наконец вот решение:

  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;
        }
person Waqas K    schedule 24.12.2017
comment
(например) Кстати, вам не нужно использовать SmbFile, если путь является локальным путем. - person Eliad Cohen; 27.12.2017