使用以下代码下载文件
<%@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>
此代码工作正常...下面的代码用于在下载后删除该文件...
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);
}
删除代码工作正常,当我删除其他文件时,我需要下载我需要的用户下载文件时,他也可以在下载后删除该文件,但我的下载代码不起作用..它打印删除失败的其他代码
请给我删除该文件的方法..或下载完成后立即删除文件的任何其他方法。