如何使用servlet下载文件(.EXE)?

时间:2014-02-05 09:20:04

标签: java servlets

我需要使用servlet下载EXE文件。

首先,我有一个类将向servlet发送请求或调用servlet。在第一个代码中有按钮。当用户点击此按钮时,请求servlet下载.exe文件。

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();
    }
}

以上代码是为按钮编写的。当用户点击按钮时,会调用该函数调用servlet类。

这是servlet类: -

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();  
  } 

}

现在在这个servlet中我需要有下载文件的代码。 首先,我想在服务器上部署servlet,之后我将运行swing代码点击按钮下载.exe文件。我不想上传任何文件。我只需要下载.exe文件并将其保存在客户端站点。现在,如果有人有代码下载文件,请帮助我。我尝试了很多代码,但没有一个代码适合我。现在我有上面的servlet代码,但它也不适用于我。我没有得到任何错误。而且我没有任何弹出窗口来保存文件。任何建议。

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码来解决您的问题。

    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();
       }
    }