1

I'm new to Spring-Boot,i'm trying to upload file to Mongodb Database, save its objectId and link it to my document, in order to download it Later. Here is my class Person:

@Document
public class Person {
@Id
private String id;

private String firstName;
private String lastName;
private String age;
private String filename;
private ObjectId file;
 ....}

This is my controller method:

@RequestMapping(value="/addperson",method=RequestMethod.POST)
public String ajout(Model model,@Valid PersonForm pf){

    Person p=new Person(pf.getFirstname(),pf.getLastName(),pf.getAge());
    InputStream inputStream=null;
    try {
        inputStream = new FileInputStream("E:/usb/");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    p.setFilename(pf.getFilename());
    repository.linkFileToMyDoc(p,inputStream,p.getFilename());
    repository.save(p);
    model.addAttribute("PersonForm",pf);
    return "personview";
}

I wonder if it is the best method to do it. the error i get is that access to "E:/usb/" is denied. Can anyone help please

METTAIBI
  • 3,201
  • 5
  • 22
  • 29
  • What do you have in E drive? Why not simply use the home folder of your logged in user. Secondly, too much service logic you have in Controller, put it in service. Thirdly, Why not simply use MultiPart to send files. This way, you can save other information like file-size, name, metadata, etc. – We are Borg Sep 11 '15 at 19:37
  • i want to store the uploaded files there in E:/usb/ ..can u correct me if i'm wrong, and provide me more infos about MultiPart.Thanks for ur comment, once i add the service Layer i'll make modifications – METTAIBI Sep 11 '15 at 20:54
  • please write a simple java file handler and check the file is really accessible or not ? – ajayramesh Sep 12 '15 at 04:16
  • http://stackoverflow.com/questions/32394697/how-to-send-an-retrieved-image-from-mongo-using-gridfs-in-spring-rest-call try this link – ajayramesh Sep 12 '15 at 04:18
  • i resolved the problem. Check my answer below. I'll be pleased for your feedback – METTAIBI Sep 12 '15 at 21:27

1 Answers1

1

i found the solution after reading MultipartFile documentation. I uploaded file to server and then transfered it to directory,after allowing total control to directory "E:/usb/". this is my controller method:

   Person p=new Person(pf.getFirstname(),pf.getLastName(),pf.getAge());


    try {
        File dest = null;
        String str = "C:/tt/"+pf.getFile().getOriginalFilename();
        File dir = new File(str);
        dir.createNewFile();
        pf.getFile().transferTo(dir);
        p.setFilename(str);
        InputStream inputStream=pf.getFile().getInputStream();
         repository.linkFileToMyDoc(p,inputStream,pf.getFile().getOriginalFilename());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    repository.save(p);
    model.addAttribute("PersonForm",pf);
    return "personview";
}

the linkFileToMydoc method, you'll find it in this link: How to reference GridFSFile with @DbRef annotation (spring data mongodb)

Community
  • 1
  • 1
METTAIBI
  • 3,201
  • 5
  • 22
  • 29