Skip to content

Instantly share code, notes, and snippets.

@slamdev
Created July 25, 2017 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save slamdev/9937106a015c7ea3ebd05b32f1299bcf to your computer and use it in GitHub Desktop.
Save slamdev/9937106a015c7ea3ebd05b32f1299bcf to your computer and use it in GitHub Desktop.
Spring resumable.js integration
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