Java源码示例:io.vertx.core.http.HttpServerFileUpload
示例1
/**
* Creates a temporary file.
*
* @return a new Temp File from getDiskFilename(), default prefix, postfix and baseDirectory
*/
static synchronized File tempFile(HttpServerFileUpload upload) {
String newpostfix;
String diskFilename = new File(upload.filename()).getName();
newpostfix = '_' + diskFilename;
File tmpFile;
try {
if (baseDirectory == null) {
// create a temporary file
tmpFile = File.createTempFile(prefix, newpostfix);
} else {
tmpFile = File.createTempFile(prefix, newpostfix, new File(
baseDirectory));
}
if (deleteOnExitTemporaryFile) {
tmpFile.deleteOnExit();
}
return tmpFile;
} catch (IOException e) {
// Really bad, can't create the tmp file.
throw new IllegalStateException(e);
}
}
示例2
@Override
public HttpServerRequest uploadHandler(Handler<HttpServerFileUpload> handler) {
delegate.uploadHandler(handler);
if (!userSetState) {
delegate.resume();
}
return this;
}
示例3
/**
* Creates an instance of {@link org.wisdom.framework.vertx.file.VertxFileUpload}.
*
* @param vertx the Vert.X instance
* @param upload the upload object
* @param limitSize the threshold. If the amount of uploaded data is below this limit,
* {@link org.wisdom.framework.vertx.file.MemoryFileUpload} is used to backend the uploaded file.
* Otherwise, it uses a {@link org.wisdom.framework.vertx.file.DiskFileUpload}.
*/
public MixedFileUpload(final Vertx vertx,
final HttpServerFileUpload upload,
final long limitSize,
final long maxSize,
final Handler<Result> errorHandler) {
super(upload, errorHandler);
delegate = new MemoryFileUpload(upload, errorHandler);
upload.exceptionHandler(event -> LoggerFactory.getLogger(MixedFileUpload.class)
.error("Cannot read the uploaded item {} ({})", upload.name(), upload.filename(), event))
.endHandler(event -> delegate.close())
.handler(
event -> {
if (event != null) {
// We are still in memory.
if (delegate instanceof MemoryFileUpload) {
MemoryFileUpload mem = (MemoryFileUpload) delegate;
checkSize(mem.buffer.length() + event.length(), maxSize, upload);
if (mem.buffer.length() + event.length() > limitSize) {
// Switch to disk file upload.
DiskFileUpload disk = new DiskFileUpload(vertx, upload, errorHandler);
// Initial push (mem + current buffer)
disk.push(mem.buffer.appendBuffer(event));
// No cleanup required for the memory based backend.
delegate = disk;
// the disk based implementation use a pump.
} else {
delegate.push(event);
}
}
}
}
);
}
示例4
@Override
public HttpServerRequest uploadHandler(Handler<HttpServerFileUpload> handler) {
delegate.uploadHandler(handler);
return this;
}
示例5
@Override
public HttpServerRequest uploadHandler(Handler<HttpServerFileUpload> handler) {
delegate.uploadHandler(handler);
return this;
}
示例6
public FileUploadImpl(String uploadedFileName, HttpServerFileUpload upload) {
this.uploadedFileName = uploadedFileName;
this.upload = upload;
}
示例7
/**
* Checks whether we exceed the max allowed file size.
*
* @param newSize the expected size once the current chunk is consumed
* @param maxSize the max allowed size.
* @param upload the upload
*/
private void checkSize(long newSize, long maxSize, HttpServerFileUpload upload) {
if (maxSize >= 0 && newSize > maxSize) {
upload.handler(null);
report(new IllegalStateException("Size exceed allowed maximum capacity"));
}
}
示例8
/**
* Creates an instance of {@link org.wisdom.framework.vertx.file.DiskFileUpload}.
*
* @param vertx the Vert.X instance
* @param upload the Vert.X file upload object
* @param errorHandler the error handler
*/
public DiskFileUpload(Vertx vertx, HttpServerFileUpload upload, Handler<Result> errorHandler) {
super(upload, errorHandler);
this.file = tempFile(upload);
this.vertx = vertx;
}
示例9
/**
* Creates an instance of {@link org.wisdom.framework.vertx.file.MemoryFileUpload}.
*
* @param upload the Vert.X file upload object
* @param errorHandler the error handler
*/
public MemoryFileUpload(HttpServerFileUpload upload, Handler<Result> errorHandler) {
super(upload, errorHandler);
}
示例10
/**
* Creates the {@link org.wisdom.framework.vertx.file.VertxFileUpload}.
*
* @param upload the {@link HttpServerFileUpload} that is uploaded.
* @param errorHandler the error handler.
*/
protected VertxFileUpload(HttpServerFileUpload upload, Handler<Result> errorHandler) {
this.upload = upload;
this.errorHandler = errorHandler;
}