简体   繁体   English

JSP:在客户端上保存文件而不将其存储在服务器上

[英]JSP: save a file on client without storing it on server

In my jsp someone wants to export a query result to csv. 在我的jsp中,有人想将查询结果导出到csv。 Can't use frameworks. 不能使用框架。 At the moment, here is the process: 目前,这是一个过程:

  1. press button, go to servlet 按下按钮,转到servlet
  2. prepare data for csv 为csv准备数据
  3. make csv and save to server make csv并保存到服务器
  4. back to jsp and let them download the fresh-made file from an anchor tag. 回到jsp,让他们从锚标签下载新制作的文件。

Thing is, I' don't want to create this file on server, as I have to dispose it afterwards, but I still want to give the user the "save as" window. 事实是,我不想在服务器上创建这个文件,因为我必须在之后处理它,但我仍然想给用户“另存为”窗口。 So, is there a way, putting for example an OutputStream object in session, to achieve this result? 那么,是否有一种方法,例如在会话中放置一个OutputStream对象来实现这个结果? Thank you all! 谢谢你们!

A servlet can generate any type of content. servlet可以生成任何类型的内容。 So, when you click the button to run the servlet, simply have the servlet write the file back to the client at that time. 因此,当您单击按钮以运行servlet时,只需让servlet将文件写回客户端。 You'll need to set the Content-type header to " text/csv " (and making sure that you set encoding properly). 您需要将Content-type标头设置为“ text / csv ”(并确保正确设置编码)。 You don't need to set the Content-Length header; 您不需要设置Content-Length标头; the browser can deal with that. 浏览器可以处理。

When the servlet returns data to the browser, the user will be prompted to save the file or open it with an application. 当servlet将数据返回给浏览器时,将提示用户保存文件或使用应用程序打开它。

Yes. 是。 You don't need to save the file. 您无需保存文件。 Just hold it in the session and send it in-memory to the OutputStream . 只需在会话中保留它并将其在内存中发送到OutputStream I would trash it after a given time or after delivery in order to save memory. 我会在给定时间后或分娩后将其丢弃以节省内存。

Set the content type and the content disposition appropriately. 适当地设置内容类型和内容配置。 This will mean that the browser interprets the output correctly and prompts your user to save it or launch the appropriate application. 这意味着浏览器会正确解释输出并提示用户保存输出或启动相应的应用程序。

See this SO answer for more details. 有关详细信息,请参阅此SO答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用JSP从客户端读取excel文件并处理数据而不进行存储 - How to read excel file from client using JSP and process data without storing 使用 PDFBox 保存方法将文件上传到 SFTP 服务器而不将文件存储到本地系统? - Upload a file to an SFTP server using PDFBox save method without storing the file to the local system? 生成/下载CSV文件而不存储在服务器上 - Generate/download CSV file without storing on server 在服务器上存储文件的同时下载文件? 如何将文件的下载功能提供给客户端以在服务器上存储文件? - download file withough storing file on server? How to give download functionality of file to client withought storing file on server? 存储JSP呈现的HTML文件 - Storing HTML file rendered by JSP java server / client将downloadad文件保存为HDD - java server/client save a downloadad file NOT to HDD 从服务器JSP创建的文件中读取客户端上的更新数据 - read updated data on client from file created by server JSP 捕获JSP输出,保存到文件 - Capture JSP output, save to file 如何在 JSP 中保存 XML 文件? - How to save a XML file in JSP? 如何仅在JSP中使用Web Camera捕获图像(不使用Servlet)并将其保存到MS SQL Server - How to Capture Image using Web Camera in JSP only (Without using Servlet) and save it into MS SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM