gpt4 book ai didi

html - 通过更改 JSP 中的内容类型来下载图像

转载 作者:行者123 更新时间:2023-11-28 01:53:00 24 4
gpt4 key购买 nike

我有一个来自外部 JSP(Second.jsp) 文件的图像。我在 First.jsp 中有我的按钮。当我点击按钮时,应该下载图像。我怎样才能通过更改内容类型来做到这一点?

首先.jsp:

   <div id="first">
<button id="submit"> Download Image</button>
<jsp:include page="second.jsp"></jsp:include>
</div>

第二个.jsp:

    <img alt="Input Voltage" src="/solarether/GraphsServlet?date=<%=date%>&date1=<%=date1%>&siteId=<%=siteID%>&type=<%=type%> height="300" width="600">

最佳答案

点击下载按钮调用一个 servlet 并在流中设置响应文件。文件将被下载。在 doGet 或 doPost 方法中实现您的代码

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
File file = new File(filePath);
int length = 0;
ServletOutputStream outStream = response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType(filePath);

// sets response content type
if (mimetype == null) {
mimetype = "application/octet-stream";
}
response.setContentType(mimetype);
response.setContentLength((int)file.length());
String fileName = (new File(filePath)).getName();

// sets HTTP header
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

byte[] byteBuffer = new byte[BUFSIZE];
DataInputStream in = new DataInputStream(new FileInputStream(file));

// reads the file's bytes and writes them to the response stream
while ((in != null) && ((length = in.read(byteBuffer)) != -1))
{
outStream.write(byteBuffer,0,length);
}

in.close();
outStream.close();
}

关于html - 通过更改 JSP 中的内容类型来下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18953569/

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