D
D
Dmitry Pashko2018-03-12 19:35:30
Java
Dmitry Pashko, 2018-03-12 19:35:30

How to save and upload pictures to google drive?

Here is the code that does this locally. Please tell me where you can read about this and what is needed for this?

public void addNews(News news, MultipartFile image) {
        final String name = image.getOriginalFilename();
        final String location = "E://pictures//";
        FileEntity fileEntity = new FileEntity();
        try {
            if (image.getSize() > 0 && image.getInputStream() != null && name != null) {
                final Path outputFile = Paths.get(location, name);
                fileEntity.setLocation(location);
                fileEntity.setFileName(name);
                news.setFileEntity(fileEntity);
                fileEntity.setNews(news);
                try (final ReadableByteChannel input = Channels.newChannel(image.getInputStream());
                     final WritableByteChannel output = Channels.newChannel(new FileOutputStream(outputFile.toFile()))) {
                    final ByteBuffer buffer = ByteBuffer.allocate(1024);
                    while (input.read(buffer) >= 0 || buffer.position() > 0) {
                        buffer.flip();
                        output.write(buffer);
                        buffer.compact();
                    }
                }
            }
        } catch (IOException e) {
            logger.error(e.getMessage());
        }

        newsDao.saveOrUpData(news);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
piatachki, 2018-03-14
@piatachki

First, everything is already written. Use .
Second, MultipartFile has a transferTo(File file) method, stop reinventing the wheel already :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question