我在嘗試使用RESTful調用下載Excel文件。我認爲它應該很簡單,但我一直在收到HTTP 406不適用Eror。這是我的控制器方法是什麼樣子:使用RESTful給文件下載HTTP 406不適用錯誤
@RequestMapping(value = "/gettemplate", method = RequestMethod.GET, produces = "application/vnd.ms-excel")
@ResponseBody
public Response getExcelTemplate() throws Exception {
File templateFile = new File("TestFile.xlsx");
ResponseBuilder builder = Response.ok(templateFile,"application/vnd.ms-excel");
builder.header("Content-Disposition", "attachment; filename=\"" + templateFile.getName() + "\"");
return builder.build();
}
我試過設置請求頭接受application/vnd.ms-excel
,我也用application/octet-stream
代替vnd.ms-excel
嘗試。在任何一種情況下,我都會收到一個帶有406錯誤消息的HTML響應。這裏是我的Ajax測試呼叫的樣子:
Ext.Ajax.request({
url: 'myservice/gettemplate',
//dataType: 'application/vnd.ms-excel',
headers: {
'Accept': 'application/vnd.ms-excel, text/plain, */*'
//'Content-Type': 'application/vnd.ms-excel'
},
success: function (response, options) {
alert(1);
},
failure: function (response, options) {
alert(2);
}
});
我註釋掉了,我試過和刪除,因爲它並沒有幫助的線條。這可能是一個非常簡單的配置更改,但我似乎無法弄清楚。
內容類型被聲明兩次,一次在@RequestMapping註釋中的「產生式」值和另一個在ResponseBuilder上。嘗試刪除其中一個。 – ESala
@ESala刪除一個沒有幫助。嘗試改變爲'ResponseBuilder builder = Response.ok(templateFile);'仍然得到相同的錯誤。 –