【问题标题】:downloaded file supposed to be .png下载的文件应该是.png
【发布时间】:2020-02-06 08:47:02
【问题描述】:

我不知道我的代码有什么问题,我尝试使用 sambautil 从服务器下载 png

if (!path.equals("")) {
    SambaUtil su = new SambaUtil();
    byte[] data = su.openfile(path);
    log.info(path);
    ByteArrayInputStream inStream =  new ByteArrayInputStream(su.openfile(path));
    PrintWriter outStream1  = resp.getWriter();
    int bytes;
    while ((bytes = inStream.read()) != -1) {
        outStream1.write(bytes);
    }

    inStream.close();
    outStream1.close();

}

连接正确,但输出数据似乎错误

the output

有什么想法吗?

【问题讨论】:

  • 为什么要打开两次? su.openfile(path);
  • 你看到流的乞求处的字母JFIF了吗?那是因为您下载的图像文件是 JPEG,而不是 PNG。

标签: java samba downloadfile


【解决方案1】:

那是因为您正在获取构成 .PNG 文件的字节并将其打印出来,而不是将其保存为我认为您想要做的文件。

代替

PrintWriter outStream1  = resp.getWriter();

用这个替换

OutputStream outStream1 = new FileOutputStream("somefile.png");

然后打开“somefile.png”,您应该可以看到您通过 Samba 下载的图像

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2020-09-21
    相关资源
    最近更新 更多