This is example using servlet, you must be basic knowledge of file handling, because here we have to create file into specifric folder.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/DownLoadFile")
public class DownLoadFile extends HttpServlet {
private static final long serialVersionUID = 1L;
public DownLoadFile() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//create file name
String filename = "customer.csv";
//create folder
String upload = "/folderCsv/";
//method for getting path
String filepath = request.getServletContext().getRealPath(upload);
// creates the save directory if it does not exists
File folder = new File(filepath);
if (!folder.exists()) {
folder.mkdir();
}
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\""
+ filename + "\"");
FileInputStream fileInputStream = new FileInputStream(filepath
+ File.separator + filename);
int i;
while ((i = fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
out.close();
}
now jsp contents are in my coding
<a href="DownloadExportFile" target="_blank"> <img src="img/excellogo.png" height="200px" width="200px"></a></center>
</div><br>
here i have use a image for downloading
Thanks
Keep Happy Learning
No comments:
Post a Comment