import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping("files")
public class FileServiceController {
@PostMapping("upload")
public Mono<String> upload(@RequestPart("file") FilePart filePart) {
return filePart.content().map(b -> b.asInputStream(true)).reduce(SequenceInputStream::new).map(ins -> {
try {
var outSteam = new ByteArrayOutputStream();
var buffer = new byte[1024];
var len = -1;
while ((len = ins.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
ins.close();
return Base64.encodeBase64String(outSteam.toByteArray());
} catch (IOException ex) {
ex.printStackTrace();
return "false";
}
});
}
}
网友评论