2014-06-10 21 views

回答

1

如果你不想提供文件內容作爲HTTP響應有效載荷,你不應該把它寫入response.getWriter()。順便說一句,你是而不是「在控制檯中顯示它」,你發送它作爲對客戶請求的HTTP響應。

要在本地磁盤上保存包含該內容的文件,只需創建一個FileOutputStream並在其中寫入文件內容。

至於HTTP響應,只需提供URL到新創建的可下載文件(作爲Location頭或其他)。

+0

感謝您的回答 – Yanni

0

您可以做類似的事情,您需要設置正確的mime-typecontent-length標題。

File downloadFile = new File(filePath); 
    FileInputStream inStream = new FileInputStream(downloadFile); 

    // obtains response's output stream 
    OutputStream outStream = response.getOutputStream(); 

    byte[] buffer = new byte[4096]; 
    int bytesRead = -1; 

    while ((bytesRead = inStream.read(buffer)) != -1) { 
     outStream.write(buffer, 0, bytesRead); 
    } 

    inStream.close(); 
    outStream.close(); 
+0

感謝您的回覆 – Yanni

相關問題