2016-03-05 71 views
0

我試圖讓客戶下載示例excel文件。這裏是我的代碼:春天MVC下載文件沒有顯示擴展

String path = UploadController.class.getResource("/com/medify/data/" + file).getFile(); 

    response.setContentType("application/force-download"); 
    response.setContentType("application/excel"); 
    response.setHeader("Content-Disposition", "attachment; filename=" + file); 

    try{ 
     File fi = new File(path); 
     InputStream in = new BufferedInputStream(new FileInputStream(fi)); 
     ServletOutputStream out = response.getOutputStream(); 
     IOUtils.copy(in, out); 
     response.flushBuffer(); 

     return null; 
    } 
    catch(Exception ex){ 
     return null; 
    } 

問題是下載的文件從來沒有文件擴展名。該文件是sample-doctor-uploads.xls它存在於com/medify/data包中。

回答

2

您需要使用正確的內容類型。如果你的文件有.xls的extention你需要使用 -

response.contentType = "application/vnd.ms-excel" 

與拓展應用的完整文件名喜歡 -

response.setHeader "Content-disposition", "attachment;filename=fileNamewithExtention" 
+0

感謝賈漢吉爾,「應用程序/ vnd.ms-EXCEL」的伎倆 – Ashutosh