using following code for download file
<%@page import="java.nio.channels.OverlappingFileLockException"%>
<%@page import="java.nio.channels.FileLock"%>
<%@page import="java.nio.channels.FileChannel"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page contentType="application/octet-stream"%>
<%@page pageEncoding="UTF-8"%>
<%@page language="java" import="java.io.*,java.net.*,java.util.*,javax.servlet.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>JSP Page</title>
</head>
<body>
<%
BufferedInputStream filein = null;
BufferedOutputStream output = null;
try {
File file = new File("d:/ttt.mp3"); // path of file
if (file.exists()) {
byte b[] = new byte[2];
int len = 0;
filein = new BufferedInputStream(new FileInputStream(file));
output = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/force-download");
response.setHeader("content-Disposition", "attachment; filename=Backup.gz");
response.setHeader("content-Transfer-Encoding", "binary");
while ((len = filein.read(b)) > 0) {
output.write(b, 0, len);
output.flush();
// output.close();
}
Calendar present = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String a = sdf.format(present.getTime());
String ip = request.getRemoteAddr();
String mail = session.getAttribute("emailid").toString();
try {
PreparedStatement pst = null;
String connectionURL = "jdbc:mysql://localhost/logapp_log";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = (Connection) DriverManager.getConnection(connectionURL, "root", "nic@123");
String query = "insert into logapp_log_table(uname,date_time,user_ip) values ('" + mail + "','" + a + "','" + ip + "')";
pst = connection.prepareStatement(query);
pst.executeUpdate();
pst.close();
connection.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
session.setAttribute("Ferror", "File not available Or Deleted");
getServletContext().getRequestDispatcher("/link.jsp").forward(request, response);
}
} catch (Exception ex) {
System.out.print(ex.getMessage());
}
%>
</body>
this code is work fine …
and below code for delete that file after download …
File file = new File("d:/ttt.mp3");
if(file.exists()) {
if (file.delete()) {
System.out.println("File Deleted");
session.setAttribute("Ferror","Deleted");
getServletContext().getRequestDispatcher("/link.jsp").forward(request, response);
response.sendRedirect("login.jsp");
} else {
System.out.println("Delete failed");
session.setAttribute("Ferror","Under Process");
getServletContext().getRequestDispatcher("/link.jsp").forward(request, response);
}
}
else {
session.setAttribute("Ferror","File not available Or Deleted");
getServletContext().getRequestDispatcher("/link.jsp").forward(request, response);
}
delete code is work fine , when i delete file other then i download
i need when user download a file he can also delete that file after download it , but my download code is not working .. it print the else code which is delete failed
please give me the way to delete that file .. or any other way to immediately delete the file after download is complete .
You opened file for reading and forgot to close it. Since you are on Windows that locks open files you cannot then delete it. Call
filein.close()in finally block after sending file to client. Then try to remove it. I hope this will work.On linux it can’t happen! 🙂