2012-06-29 124 views
0

我想使用Java下載.msi文件。我試圖下載文件使用下面的代碼如何使用Java下載.msi文件

PrintWriter out = null; 
FileInputStream fileToDownload = null; 
BufferedReader bufferedReader = null; 
try { 
     out = response.getWriter(); 
     fileToDownload = new FileInputStream(DOWNLOAD_DIRECTORY + FILE_NAME); 
     bufferedReader = new BufferedReader(new InputStreamReader(fileToDownload)); 

     //response.setContentType("application/text"); 
     //response.setContentType("application/x-msi"); 
     //response.setContentType("application/msi"); 
     //response.setContentType("octet-stream"); 
     response.setContentType("application/octet-stream"); 
     //response.setContentType("application/x-7z-compressed"); 
     //response.setContentType("application/zip"); 
     response.setHeader("Content-disposition","attachment; filename=" +FILE_NAME); 
     response.setContentLength(fileToDownload.available()); 

     System.out.println("\n now file download is starting"); 
     String NextLine = ""; 
     while((NextLine = bufferedReader.readLine()) != null){ 
      out.println(NextLine); 
     } 
     out.flush();         

    } catch (IOException e) { 
     out.write("<center><h2>The Installer is not Available on Server</h2></center>"); 
     System.out.println("\n Got Exception while getting the input Stream from the file==>"+e); 
     log.error("Error::", e); 
    } 
    finally{ 
     if(null != bufferedReader){ 
      try { 
       bufferedReader.close(); 
      } catch (IOException e) { 
       System.out.println("\n Error in closing buffer Reader==>"+e); 
       log.error("Error::", e); 
      } 
     }// End of if 

     if(null != fileToDownload){ 
      try { 
       fileToDownload.close(); 
      } catch (IOException e) { 
       System.out.println("\n Error in closing input stream==>"+e); 
       log.error("Error::", e); 
      } 
     }// End of if 
    }// End of finally   
+0

您是否檢查過msi的基本文件下載損壞,完全在Java之外?也許它沒有正確上傳,因爲任何這些內容類型都應該工作,除非導致它的代碼有其他錯誤。 – SilverbackNet

+0

內容類型對正在下載的實際數據沒有影響。代碼的其餘部分是怎麼看的? –

+0

我在特定目錄下粘貼.msi文件並下載相同的文件以便在客戶端機器上使用。以下代碼嘗試過使用 –

回答

1

您無法讀取二進制文件(MSI)在此case.Your代碼readline()文件是完全錯誤的,將無法正常工作。 這是一個簡單的功能,可以讓你做你想做的事。

private void doDownload(HttpServletRequest req, HttpServletResponse resp,String filename, String original_filename)throws IOException 
      { 
      File f = new File(filename); 
      int length = 0; 
      ServletOutputStream op = resp.getOutputStream(); 
      ServletContext context = getServletConfig().getServletContext(); 
      String mimetype = context.getMimeType(filename); 
      resp.setContentType((mimetype != null) ? mimetype : "application/octet-stream"); 
      resp.setContentLength((int)f.length()); 
      resp.setHeader("Content-Disposition", "attachment; filename=\"" + original_filename + "\""); 
      byte[] bbuf = new byte[BUFSIZE]; 
      DataInputStream in = new DataInputStream(new FileInputStream(f)); 
      while ((in != null) && ((length = in.read(bbuf)) != -1)){ 
      op.write(bbuf,0,length); 
      } 
      in.close(); 
      op.flush(); 
      op.close(); 
     } 

servlet創建doDownload()功能和doGet傳遞requierd參數,該功能,doPost或任何有效的地方,你喜歡的。

參數:

  • @參數REQ請求

  • @參數RESP的響應

  • @參數文件名要下載的文件的名稱。

  • @param original_filename瀏覽器應該接收的名稱。

+0

感謝這個很好的下載文件。 –