java - 如何在我的应用程序中将图像文件发送到 NAS 服务器?

标签 java android fileoutputstream smb nas

我想发送图片文件(jpg、png)到NAS server在java中使用smb

我添加了 jcifs-1.3.19.jar 。如前所述herehere

编辑:我已经使用此代码成功将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;
        }

关于java - 如何在我的应用程序中将图像文件发送到 NAS 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47961360/

相关文章:

java - 连续运行ExecutorService任务

java - 反射和私有(private)本地方法

java - 我可以通过spring-roo中的xx.java修改xx_Roo_JavaBean.aj中的setter getter吗?

android - Ant 构建在 Eclipse 中失败

java - 未生成 Zip 文件

c++ - 仅当设置了标志时才打开文件(稍后关闭)

java - 有没有直接的方法将 Map<String, List<String>> 转换为 JSON 字符串(使用 javax.json 库)

android - 更改主要 Activity 中的 ListView 字段属性

android - 覆盖位图时报错android.graphics.Canvas.throwIfRecycled

android - 写入外部存储文件notfoundexception