我正在使用 Spring Frame work 3.05,我想使用相同的框架下载文件。谷歌搜索后,我在此堆栈溢出链接中找到了答案。
Downloading a file from spring controllers
在上面的链接中 -> 斯科特·卡尔森 - 回答
但是如果我尝试按照上面链接中的建议进行操作。我收到以下错误:
"由于 :java.io.NotSerializedException: org.springframework.core.io.FileSystemResource 写入输出流时出错 "
我的代码看起来像:
FileSystemResource fsr = new FileSystemResource(path);
try
{
OutputStream outstr = res.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outstr);
oos.writeObject(fsr);
oos.flush();
oos.close();
}
catch(Exception e)
{
log4log.error("Error while writing to output stream due to :"+e.toString());
return null;
}
请帮帮我...
最佳答案
您想要做的是将 FileSystemResource
内容复制到响应的 Outputstream
中:
InputStream in = fsr.getInputStream();
OutputStream out = response.getOutputStream();
org.apache.commons.io.IOUtils.copy(in, out);
您在代码中尝试执行的操作实际上是尝试 serialize an object进入响应的输出流。
关于java - 从 spring Controller 下载文件 NotSerializedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34987483/