简体   繁体   中英

how to download a file (.EXE) using servlet?

I need to download EXE file using servlet.

First I have one class which will send request to servlet or call a servlet. In 1st code there is button. When user clicks on this button request is given servlet to download the .exe file.

enter code here:
LoginDemo.class  (Written using Swing) :- 

public void actionPerformed(ActionEvent ae)
{
    String uname=text1.getText();
    String pwd=text2.getText();

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8080/myDemo/LoginAction?uname="+uname+"&pwd="+pwd);

    try {
        HttpResponse rsp = client.execute(post);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Above code is written for button. When user clicks on button this function gets called which calls servlet class.

Here is the servlet class:-

public class LoginAction extends HttpServlet 
{

  protected void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException 
  {
          System.out.println("In Servlet...");
          //code to download a file 
          File file = new File("C:\\a\\wrar501.exe");  
      response.setContentType("application/octet-stream");  
      response.setContentLength((int) file.length());  
      response.setHeader("Content-Disposition",  
         "attachment; filename=\"" + file.getName() + "\"");  
      InputStream in = new BufferedInputStream(new FileInputStream(file));  
      OutputStream out = response.getOutputStream();  
      byte[] buffer = new byte[4096];  
      while (true) {  
         int bytesRead = in.read(buffer, 0, buffer.length);  
         if (bytesRead < 0)  
            break;  
         out.write(buffer, 0, bytesRead);  
      }  
      out.flush();  
      out.close();  
      in.close();  
  } 

}

Now in this servlet I need to have the code which will download a file. Here first I want to deploy servlet on server and after I will just run swing code to click on button to download the .exe file. I don't want to upload any file. I just need to download a .exe file and save it at client site. Now if anyone have a code to download a file please help me. I tried many codes but none of them are working for me. Now I have above servlet code, but it is also not working for me. I didn't get any errors.And also i do not have any popup window to save a file. Any suggestion.

You can use following code this might solve your problem.

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    public class DownloadServlet extends HttpServlet
    {
       public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException
       {
          File file = new File("C:\\CDROM\\MSCDEX.EXE");
          response.setContentType("application/octet-stream");
          response.setContentLength((int) file.length());
          response.setHeader("Content-Disposition",
             "attachment; filename=\"" + file.getName() + "\"");
          InputStream in = new BufferedInputStream(new FileInputStream(file));
          OutputStream out = response.getOutputStream();
          byte[] buffer = new byte[4096];
          while (true) {
             int bytesRead = in.read(buffer, 0, buffer.length);
             if (bytesRead < 0)
                break;
             out.write(buffer, 0, bytesRead);
          }
          out.flush();
          out.close();
          in.close();
       }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM