V
V
Virgil Merkel2020-10-10 16:11:44
Spring
Virgil Merkel, 2020-10-10 16:11:44

How to save files to google drive using Spring?

I have a project on Spring, I want to save photos on the news tab.
application.properties initially had the full path to the files C:\\Users\\Public\\Pictures
now I wanted to upload the project to Heroku, and there this full path would not work, so I wanted to google drive

spring.datasource.url=${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost/another_db}
spring.datasource.username=${SPRING_DATASOURCE_USERNAME:postgres}
spring.datasource.password=${SPRING_DATASOURCE_PASSWORD:root}
spring.jpa.generate-ddl=true

spring.freemarker.expose-request-attributes=true

#upload.path=C:\\Users\\Public\\Pictures
upload.path=https://drive.google.com/drive/folders/1pHwD2krPl1maSyJDzI1Fw2yTl2no9yr3?usp=sharing


spring code section

@PostMapping("/main")
    public String addMessage(@AuthenticationPrincipal User user, @RequestParam String text,
                             @RequestParam String tag, Map<String, Object> model,
                             @RequestParam("file") MultipartFile file) throws IOException {

        Message message = new Message(text, tag, user);

        if(file != null && !file.getOriginalFilename().isEmpty()){
            File uploadDir = new File(uploadPath);

            if (!uploadDir.exists() ){
                uploadDir.mkdir();
            }

            String uuidFile = UUID.randomUUID().toString();

            String resultFilename = uuidFile + "." + file.getOriginalFilename();

            file.transferTo(new File(uploadPath+"/"+resultFilename));

            message.setFilename(resultFilename);
        }


        messageRepo.save(message);

        Iterable<Message> messages = messageRepo.findAll();
        model.put("messages", messages);

        return "main";
    }


I get an error

java.io.FileNotFoundException: C:\Users\79393\AppData\Local\Temp\tomcat.4572879847737378616.8080\work\Tomcat\localhost\ROOT\https:\drive.google.com\drive\folders\1pHwD2krPl1maSyJDzI1Fw2yTl2no9yr3?usp=sharing\bb284e1d-c324-480b-8ebd-b7a8307948e2.logo.png (Синтаксическая ошибка в имени файла, имени папки или метке тома)
java.io.IOException: java.io.FileNotFoundException: C:\Users\79393\AppData\Local\Temp\tomcat.4572879847737378616.8080\work\Tomcat\localhost\ROOT\https:\drive.google.com\drive\folders\1pHwD2krPl1maSyJDzI1Fw2yTl2no9yr3?usp=sharing\bb284e1d-c324-480b-8ebd-b7a8307948e2.logo.png (Синтаксическая ошибка в имени файла, имени папки или метке тома)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2020-10-10
@azerphoenix

Hello!
I don't quite understand, have you implemented the methods to work with the Google Drive API? If not, read the Google Drive API documentation first.
https://developers.google.com/drive/api/v3/about-sdk
Here's how to implement file upload to disk
https://developers.google.com/drive/api/v3/manage-...
Well and then read on...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question