|
|
How to Create Downloadable link in jsp - JSP/Java
|
Views : 2619
|
|
Tagged in : JSP-Java
|
|
|
Report This Scrap as Inappropriate We request you to choose the appropriate categroy and subcategory that suits your
objectionable concern about the scrap, So that our team can review and find out whether it violates our Guidelines or the
scrap is not suitable for all viewers.
|
The Bellow code will help you to create a download link using jsp
<%@page contentType="application/octet-stream"%>
<%@page pageEncoding="UTF-8"%>
<%@ page language="java" import="java.io.*,java.net.*,java.util.*,javax.servlet.* "%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
BufferedInputStream filein = null;
BufferedOutputStream outputs = null;
try {
File file = new File("./path/filename.csv");//specify the file path
byte b[] = new byte[2048];
int len = 0;
filein = new BufferedInputStream(new FileInputStream(file));
outputs = new BufferedOutputStream(response.getOutputStream());
response.setHeader("Content-Length", ""+file.length());
response.setContentType("application/force-download");
response.setHeader("Content-Disposition","attachment;filename=Backup.csv");
response.setHeader("Content-Transfer-Encoding", "binary");
while ((len = fin.read(b)) > 0) {
outputs.write(b, 0, len);
outputs.flush();
}
}
catch(Exception e){
out.println(e);
}
%>
</body>
</html>
|
|
By Vinoth, On - 2009-04-09 |
|
|
|