2017-02-24 33 views
2

我想在Spring中有一個用於下載CSV的動態URL,例如, Bernhard_content_24Feb2017.csv。更正式地說,這將是在URL中使用時間戳的Spring RequestMapping

{id}_content_{timestamp}.csv

Me_content_1Jan2017.csvYou_content_31Dec2017.csv每個請求都應該去的方法在下面的代碼(getCsv(...))。請注意,我希望保留後綴.csv。用戶應該下載一個名爲Me_content_1Jan2017.csv的文件。

所以我在春天嘗試這樣:

@RequestMapping("{id}_content_{timestamp}.csv") 
public void getCsv(@PathVariable String id, @PathVariable String timestamp, HttpServletResponse response) { 
    response.getWriter().print(createCsv(id)); 
} 

可惜,這是行不通的。

這將是什麼正確的語法?鑑於,這是可能的。

感謝,

伯恩哈德

UPDATE:

我去這個解決方案(重命名URL一點點,現在想Me_1Jan2017_content.csv):

@RequestMapping("{idAndTimestamp}_content.csv") 
public void getCsv(@PathVariable String idAndTimestamp, HttpServletResponse response) { 
    String id = idAndTimestamp.split("_")[0]; 
    response.getWriter().print(createCsv(id)); 
} 
+0

爲什麼不把currentTime.millis()當作路徑,而不是像Bernhard_content_24Feb2017這樣的東西,並生成csv文件,你可以給任何名稱.....然後在爲客戶端進行strem後... – sitakant

+0

可以你請提及時間戳和ID格式? – SachinSarawgi

+0

ID和時間戳只是隨機字符串。所以ID也是[a-zA-Z0-9]。*和時間戳。順便說一句,我不使用時間戳。我只是想讓用戶下載一個帶有時間戳的文件:'Download CSV' – Bernie

回答

1

我找不到爲@PathVariable使用複雜定義的方法。嘗試在一個變量中創建完整的文件名,然後拆分方法中的部分。像這樣的東西。

@RequestMapping("{fileName}") 
    public void getCsv(HttpServletResponse response, @PathVariable String fileName) { 
    String[] parts = fileName.split(".")[0].split("_"); 
    String id = parts[0]; 
    String timestamp = parts[2]; 
} 
+0

謝謝,我正在爲您的解決方案,分裂1 PathVariable而不是使用2 PathVariables。 – Bernie

0

你應該嘗試這樣的事情

@RequestMapping(value="/{path}/**", method = RequestMethod.GET) 
public void getCsv(@PathVariable("path") String path, HttpServletRequest request){ 
    //your code here... 

}

1

像這樣改變你的id和時間戳參數。

@RequestMapping(value = "downloadCvs/{id}/{timestamp}", method = RequestMethod.POST) 
public void getCsv(HttpServletResponse response, @PathVariable String id, @PathVariable String timestamp) { 

    response.setContentType("text/csv"); 

    String reportName = "CSV_Report_Name_What_you_Want.csv"; 

    response.setHeader("Content-disposition", "attachment;filename="+reportName); 

    response.getOutputStream().print(createCsv(id)); 

    response.getOutputStream().flush(); 
} 
+0

我認爲這非常接近解決方案;-) 但是:用戶應該下載一個名爲「Bernhard_content_24Feb2017.csv」的文件,在你的情況下,文件名將只是「24Feb2017」(沒有.csv,我猜)。 – Bernie

+0

絕對是的,但是如何在控制器中設置文件名如下:response.setContentType(「text/csv」); String reportName =「CSV_Report_Name_What_you_Want。csv「; response.setHeader(」Content-disposition「,」attachment; filename =「+ reportName); response.getOutputStream()。print(createCsv(id)); response.getOutputStream()。flush(); Just內容寫入HttpServletResponse的。這是爲您提供更加實用的解決方案? – javasenior

+0

是的,那會更好,所以+1。 最後,我用一個稍微不同的解決方案去了。 感謝您的評論。 – Bernie

0

嘗試是這樣的

@RequestMapping("{filename:.+}") //putting ".+" will keep ".csv" extension otherwise you will lose it. 
public void getCsv(@PathVariable(fileName) String fileName) { 
    String[] parts = fileName.split("_"); 
    String id = parts[0]; 
    String timestamp = parts[2].split(".")[0]; 
} 

如果你想讓它更侷限於使用:

@RequestMapping("{filename:[A-Za-z0-9]+(_content_)[A-Za-z0-9]+(.csv)}") 

希望它能幫助。