1

使用以下代码下载文件

<%@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);
       }

删除代码工作正常,当我删除其他文件时,我需要下载我需要的用户下载文件时,他也可以在下载后删除该文件,但我的下载代码不起作用..它打印删除失败的其他代码

请给我删除该文件的方法..或下载完成后立即删除文件的任何其他方法。

4

2 回答 2

2

您打开文件进行阅读并忘记关闭它。由于您使用的是锁定打开文件的 Windows,因此您无法将其删除。filein.close()向客户端发送文件后调用finally 块。然后尝试删除它。我希望这会奏效。

在linux上它不可能发生!:)

于 2013-01-16T08:56:05.413 回答
2

正如@AlexR 所说,您应该先关闭文件,然后再在 Windows 下删除它。

try{
    filein = new BufferedInputStream(new FileInputStream(file));
    //do your work
}finally{
    if(filein!=null){
        filein.close(); //close the file
    }
}

在类似 OS 的 Unix 下关闭文件输入流以释放文件处理程序。

于 2013-01-16T09:01:59.007 回答