springboot下载共享文件夹路径,为什么有的电脑通过\192.168.0.8\SuperxShareFile这种路径可以成功的下载文件,而有的电脑\192.168.0.241\城市双碳 却下载失败
idea报的以下异常


springboot下载共享文件夹路径,为什么有的电脑通过\192.168.0.8\SuperxShareFile这种路径可以成功的下载文件,而有的电脑\192.168.0.241\城市双碳 却下载失败
idea报的以下异常


该回答引用于gpt与OKX安生共同编写:
这个问题可能是因为文件夹在某些电脑上共享出现了权限问题,导致有些电脑可以成功访问,而有些电脑则无法访问。您可以检查一下具体的共享文件夹权限设置是否正确。
此外,如果您使用Spring Boot下载共享文件夹中的文件,可以尝试使用Java中的SMB库来实现,该库提供了连接到Samba(SMB / CIFS)共享的功能。以下是一个演示代码您可以试一下:
import jcifs.smb.SmbFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
@Controller
public class FileDownloadController {
@GetMapping("/download/{filename:.+}")
public void downloadFile(HttpServletResponse response, @PathVariable String filename) throws IOException {
// 解码文件名
filename = URLDecoder.decode(filename, StandardCharsets.UTF_8);
// 拼接共享文件夹路径和文件名
String sharePath = "smb://192.168.0.241/城市双碳";
String filePath = sharePath + "/" + filename;
// 创建一个SMB文件对象
SmbFile smbFile = new SmbFile(filePath);
// 设置响应头,告诉浏览器要下载文件
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(StandardCharsets.UTF_8), "ISO-8859-1"));
// 创建输入输出流,并将文件内容写入响应输出流中
try (BufferedInputStream bis = new BufferedInputStream(smbFile.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) {
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
}
}
}
以上代码演示了如何使用SMB库连接到共享文件夹中的文件并进行下载。请注意,在上述代码中,我们将共享文件夹路径和文件名拼接在一起,以便创建SMB文件对象。在实际操作中,请替换为您自己的共享文件夹路径和文件名。