用流写出文件到前端代码示例
Posted Beyond The API
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用流写出文件到前端代码示例相关的知识,希望对你有一定的参考价值。
import java.io.*;
public class FileDownloadServlet extends HttpServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
String filename = "hello.docx";
String filepath = "/path/to/your/file/" + filename;
File file = new File(filepath);
String contentType = getServletContext().getMimeType(file.getAbsolutePath());
response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment; filename=\\"" + filename + "\\"");
InputStream inputStream = new FileInputStream(file);
OutputStream outputStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1)
outputStream.write(buffer, 0, bytesRead);
inputStream.close();
outputStream.close();
本文来自博客园,作者:ukyo--BlackJesus,转载请注明原文链接:https://www.cnblogs.com/ukzq/p/17488608.html
以上是关于用流写出文件到前端代码示例的主要内容,如果未能解决你的问题,请参考以下文章