Hello i’m trying create an application allowing me host any kind of file.
In order to do it i’m exececuting following magic:
@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET)
@ResponseBody
public FileSystemResource
getFile(
@PathVariable("file_name") String fileName) {
System.out.println(fileName);
String filePath = "./files/";
return new FileSystemResource(new File(filePath+fileName));
}
But this approach brings three unwanted problems:
-
Some random data is beeing appended to the file
-
The file gets opened in the browser window instead of beeing
downloaded – i’ve tried to hack this using something likeproduces = “application/octet-stream”
but it only resulted in 406 error.
-
The test.txt is beeing truncated into test, i found a walkaround in providing the app with test.txt/ as fileName but it looks a bit messy.
As stated on spring manual
I think your problem is spring doesn’t come with a HttpMessageConverter than can process FileSystemResource.
A list of builtin HttpMessageConverter is available here. I suggest you try converting your response into byte array somehow, maybe it will pick ByteArrayHttpMessageConverter instead and help solve your issue