簡體   English   中英

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

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

我需要使用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代碼,但它也不適用於我。 我沒有得到任何錯誤。而且我沒有任何彈出窗口來保存文件。 任何建議。

您可以使用以下代碼來解決您的問題。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM