vendredi 27 février 2015

Storing files in project directory using Spring

I am trying to work through Spring tutorials on file uploads. What I'm trying to do is have the file be saved to a folder within the project. The folder is called "files" and is separate from the src folder.



|bin
|build
|src
|files


I have this code which accepts a file upload:



public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));


stream.write(bytes);
stream.close();

//file.transferTo(); help?
return "You successfully uploaded " + name + "!";


What I want to do is use transferTo()to move the file to the "files" directory. When I try the true path, or try some sort of relative path I get this error which is created in the web window I am uploading files in.



Failed to upload file => "uploadedFileName/directory" does not exist


I am not sure why the file name is being appended to the directory path. Any assistance on this is much appreciated.


Aucun commentaire:

Enregistrer un commentaire