英文:
Java - The name of the file is always 'null' when downloading it from server to my location
问题
我有以下方法可以从服务器的某个文件夹下载文件。
public DefaultStreamedContent downloadFiles(String s) {
try {
File file = new File(s);
InputStream is = new DefaultStreamedContent(new FileInputStream(file), Files.probeContentType(file.toPath())).getStream();
byte[] array = IOUtils.toByteArray(is);
return new DefaultStreamedContent(new ByteArrayInputStream(array));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
这段代码运行良好,因为文件夹中的文件被正确下载(我已检查其内容)。问题是:当通过浏览器下载并在本地的“Downloads”文件夹中保存时,文件名始终为“null”。文件应该与服务器上存储的文件具有相同的名称。假设要下载的原始文件是server\folder1\folder2\file.extension
。为什么在下载时,文件名会作为“null”保存在我的“Downloads”文件夹中?
我认为“文件路径”被视为文件的名称,因此浏览器/操作系统将其保存为“null”,因为斜杠“\”对于文件名无效。
我该如何解决这个问题?
英文:
I have the following method to download files from a certain folder in my server.
public DefaultStreamedContent downloadFiles(String s) {
try {
File file = new File(s);
InputStream is = new DefaultStreamedContent(new FileInputStream(file), Files.probeContentType(file.toPath())).getStream();
byte[] array = IOUtils.toByteArray(is);
return new DefaultStreamedContent(new ByteArrayInputStream(array));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
The code works well, as the files in the folder are downloaded correctly (I checked it´s contents). The problem is: The file name, when downloaded by the browser and saved in my Downloads
folder locally, is always null
. The files should have the same name of those files stored in the server. Let´s assume that the original file to be downloaded is server\folder1\folder2\file.extension
. Why, when downloading it, the file name goes as null
to my Downloads
folder?
I thought that the file path
is assumed as the file´s name, and therefore the Browser/OS saves it as null
because slashes \
are not valid for file´s names.
How can I solve this?
答案1
得分: 0
可以尝试类似这样的代码吗?不确定是否需要Files.probeContentType(file.toPath()
。
public DefaultStreamedContent downloadFiles(String s) {
try {
File file = new File(s);
String filename = file.getName();
InputStream is = new DefaultStreamedContent(new FileInputStream(file), Files.probeContentType(file.toPath())).getStream();
byte[] array = IOUtils.toByteArray(is);
return new DefaultStreamedContent(new ByteArrayInputStream(array), filename);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
英文:
Can you try something like this? Not sure if the Files.probeContentType(file.toPath()
is required.
public DefaultStreamedContent downloadFiles(String s) {
try {
File file = new File(s);
String filename = file.getName();
InputStream is = new DefaultStreamedContent(new FileInputStream(file), Files.probeContentType(file.toPath())).getStream();
byte[] array = IOUtils.toByteArray(is);
return new DefaultStreamedContent(new ByteArrayInputStream(array), filename);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
答案2
得分: 0
我使用了这个答案来解决我的问题。
我基本上复制了代码并根据我的需求进行了调整。所以,它完美地运行了!
英文:
I solved my problem using this answer.
I basically copied the code and adapted to my needs. So, it works perfectly!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论