Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9132157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:15:52+00:00 2026-06-17T08:15:52+00:00

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%>

  • 0

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 .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:15:53+00:00Added an answer on June 17, 2026 at 8:15 am

    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! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to download a zip file by using the following code; HttpConnection
I’m using the following code to download a file from a remote ftp server:
Im using the following code to present a file for download. The file saves
I'm using the following code to download a file off a webserver that I
In a Servlet, I am using the following code to download the file. The
I am trying to download a file from a website using the following code:
I am using the following code segment to download a file to the SD
I'm using the following code to download a pdf from a list of pdfs
I am using the following line of code to download the contents of an
I am using the following lines of code to download and save an html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.