Reputation: 43
I applied this example to add file downloading feature in my project.
Path handling and request successfully done and response came properly also. But file download does not happen at all.
here is my Java Controller of Spring framework.
@Controller
@RequestMapping(value = "/download")
public class FileDownloadController {
@ResponseBody
@RequestMapping(value = "opposingcarroute", method = RequestMethod.GET)
public void downloadFile(HttpServletResponse response,
@RequestParam(value = "path") String path) throws IOException {
File file = null;
file = new File(path);
if(!file.exists()) {
String errorMessage = "No file.";
System.out.println(errorMessage);
OutputStream outputStream = response.getOutputStream();
outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8")));
outputStream.close();
return;
}
String mimeType = URLConnection.guessContentTypeFromName(file.getName());
if(mimeType==null) {
System.out.println("mimetype is not detectable, will take default");;
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));
response.setContentLength((int)file.length());
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
FileCopyUtils.copy(inputStream, response.getOutputStream());
}
}
Response in the image is a csv file's contents that I tried to download.
Have I done something wrong..?
Upvotes: 2
Views: 718
Reputation: 2479
I am not sure if this is what is wanted but if I were you I would use Spring Content. Then you don't need to worry about how to implement any of this code yourself.
It is pretty easy to add to your application.
pom.xml
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-fs</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest</artifactId>
<version>0.2.0</version>
</dependency>
StoreConfig.java
@Configuration
@EnableFilesystemStores
@Import(RestConfiguration.class)
public class EnableFilesystemStoresConfig {
@Bean
File filesystemRoot() {
try {
return new File("/path/to/your/files");
} catch (IOException ioe) {}
return null;
}
@Bean
FileSystemResourceLoader fileSystemResourceLoader() {
return new FileSystemResourceLoader(filesystemRoot().getAbsolutePath());
}
}
NB: and even easier of you are using Spring Boot.
FileStore.java
@StoreRestResource(path="downloads")
public interface FileStore extends Store<String> {
}
And that's it. The FileStore is essentially a generic Spring ResourceLoader. The spring-content-fs
dependency will cause Spring Content to inject a filesystem-based implementation so you don't need to worry about implementing it yourself. Moreover, the spring-content-rest
dependency will cause Spring Content to also inject an implementation if an @Controller
that forwards HTTP requests onto the method of the FileStore
.
So you will now have a fully functional (POST, PUT, GET, DELETE) REST-based file service at /downloads
that will use your FileStore
to retrieve (and store) files in /path/to/your/files
on your server.
So:
GET /downloads/some/file.csv
will download file.csv
from /path/to/your/files/some/
.
And...
curl --upload-file some-other-file.csv /downloads/some-other-file.csv
will upload some-other-file.csv
and store it in /path/to/your/files/
on your server, although I am not sure how interested you are in upload.
HTH
Upvotes: 1