Created
July 25, 2017 21:09
-
-
Save slamdev/9937106a015c7ea3ebd05b32f1299bcf to your computer and use it in GitHub Desktop.
Spring resumable.js integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RestController { | |
@RequestMapping(value = "/api/upload", | |
consumes = {"multipart/form-data"}, | |
method = RequestMethod.POST | |
) | |
public ResponseEntity<Void> uploadScreenshotApi( | |
@RequestParam("file") MultipartFile file, | |
@RequestParam("resumableFilename") String resumableFilename, | |
@RequestParam("resumableChunkSize") long resumableChunkSize, | |
@RequestParam("resumableChunkNumber") int resumableChunkNumber, | |
@RequestParam("resumableTotalChunks") int resumableTotalChunks | |
) throws IOException, InterruptedException { | |
Path tempFile = Paths.get("screenshots", resumableFilename + ".tmp"); | |
ByteBuffer out = ByteBuffer.wrap(file.getBytes()); | |
try (FileChannel channel = FileChannel.open(tempFile, WRITE, CREATE)) { | |
channel.position((resumableChunkNumber - 1) * resumableChunkSize); | |
while (out.hasRemaining()) { | |
channel.write(out); | |
} | |
} | |
if (resumableTotalChunks == resumableChunkNumber) { | |
LOGGER.info("finished"); | |
Files.move(tempFile, Paths.get("screenshots", resumableFilename), REPLACE_EXISTING); | |
} else { | |
LOGGER.info("continue {} {}", resumableTotalChunks, resumableChunkNumber); | |
} | |
return toResponse(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment