【问题标题】:Saving Byte Array as a PNG file into smb2 server将字节数组作为 PNG 文件保存到 smb2 服务器中
【发布时间】:2022-08-18 17:05:18
【问题描述】:

我正在尝试创建一个应用程序,它将 Byte[] 数组中的图像作为 .png 文件保存到我的 smb2 服务器中,我能够保存一个文件,但它只包含 Array 作为文件名,大小为 0kb。

从相机获取图像

@SuppressLint(\"MissingSuperCall\")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(requestCode == REQUEST_CODE) {
            if (resultCode != RESULT_CANCELED) {
                image = (Bitmap) data.getExtras().get(\"data\");
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                bytesCapturedLogbook = byteArrayOutputStream.toByteArray();

                MyCopy my = new MyCopy();
                my.execute(bytesCapturedLogbook);
            }
        }
    }

将文件插入我的服务器的类

private class MyCopy extends AsyncTask<byte[], String, String> {

        @Override
        protected String doInBackground(byte[]... bytes) {
            String z = \"\";
            try {

                String url = \"smb://000.000.0.000/spm/Image/\" + bytes + \".png\";

                NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(
                        null, \"********\", \"**********\");
                SmbFile sfile = new SmbFile(url, auth);

                if (!sfile.exists()) {
                    sfile.createNewFile();
                    z = \"Created the file for you!!!!\";
                } else
                    z = \"Already exists at the specified location!!!!\";

            } catch (Exception ex) {
                // TODO: handle exception
                z = ex.getMessage().toString();
            }
            return z;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub


        }

        @Override
        protected void onPostExecute(String r) {
        }


    }

文件资源管理器中的结果

  • 我没有看到您实际尝试将任何字节写入文件的任何地方。请参阅此处了解如何做到这一点:byte[] to file in Java 或参阅this answer using newer NIO features
  • 当我尝试了您提供的链接中的所有答案时,没有文件被保存。
  • doInBackground:java.io.FileNotFoundException:004920224616080824:打开失败:EROFS(只读文件系统)004920224616080824是文件名
  • 如果您使用保存 doBackgroundbytes 参数的操作更新问题,将会有所帮助。目前这会创建空文件。顺便说一下,由于...,参数被定义为byte[][]
  • stackoverflow.com/questions/71394187/…,我像这样更新了我的代码,但我得到了 \'java.io.FileNotFoundException\'

标签: java android-studio jcifs


【解决方案1】:

您的问题可能是partly answered in the question you found。它通过 byte[] 显示文件到 smbfile 副本。由于您已经拥有byte[],因此您应该调整doInBackground(byte[]... bytes) 以将您的单个byte[] 传递给smbfile,如下所示:

SmbFileOutputStream outNew = new SmbFileOutputStream(smbFile);
outNew.write(bytes[0]);
outNew.close();

或者在以后的 JVM 中,最好使用 try-with-resources 为您清理:

try (SmbFileOutputStream outNew = new SmbFileOutputStream(smbFile)) {
   outNew.write(bytes[0]);
}

请注意,我无法测试上述任何内容,并且由于您还显示了问题java.io.FileNotFoundException: 004920224616080824: open failed: EROFS,那么您可能只是尝试写入无效位置(路径问题?),因为它看起来像您放将错误的文件名放入new SmbFile(...)

【讨论】:

    猜你喜欢
    • 2018-03-26
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 2013-10-27
    相关资源
    最近更新 更多