gpt4 book ai didi

java - 如何将文件从 MySQL BLOB 下载发送给最终用户?

转载 作者:行者123 更新时间:2023-12-01 15:56:26 26 4
gpt4 key购买 nike

如何将下载的文件发送给最终用户?我已经实现了一个页面的表单提交,其中包含一列数据作为数据库的 session 。这一列包含一个在 MySQL 数据库中以 BLOB 形式保存的文件。

当处理 actionforward 并使用带有 CV 的表的映射文件时,我可以通过 cv.getFileContent() 访问文件内容。我必须将数据逐字节发送给用户吗?如何提供文件下载?

最佳答案

您应该使用 servlet 作为下载机制。

使用 Get 或 Post 将表单提交给 Servlet,然后在 Servlet 类中相应地实现 doGet 或 doPost。

使用 Hibernate 将列作为字节数组获取。

protected void doGet(HttpServletRequest req, HttpServletResponse resp) {

//Getting data s byte Array;
byte[] csvFile = daoService.getFile(id);

resp.setContentType("text/csv");
resp.setContentLength((int) csvFile.length());
ServletOutputStream out;

try {
out = resp.getOutputStream();
out.write(csvFile);
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
out.close();
out.flush();
}

}

希望有帮助。

更新:

这就是我映射byte[]的方式。

<property
name="image"
update="true"
insert="true"
not-null="false"
unique="false"
type="binary"
>
<column name="image" />
</property>

关于java - 如何将文件从 MySQL BLOB 下载发送给最终用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4970267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com