public interface File extends HierarchyItem
Defines methods that WebDAV server file items must implement.
In addition to methods provided by HierarchyItem
this interface also provides methods for reading and writing file content.
getContentType()
> method must return the MIME type of the file.
Modifier and Type | Method and Description |
---|---|
long |
getContentLength()
Gets the size of the file content in bytes.
|
String |
getContentType()
Gets the media type of the file.
|
String |
getEtag()
Gets entity tag - string that identifies current state of resource's content.
|
void |
read(OutputStream output,
long startIndex,
long count)
Writes the content of the file to the specified stream.
|
long |
write(InputStream content,
String contentType,
long startIndex,
long totalFileSize)
Saves the content of the file from the specified stream to the WebDAV repository.
|
copyTo, delete, getCreated, getModified, getName, getPath, getProperties, getPropertyNames, moveTo, updateProperties
String getContentType() throws ServerException
Mime-type provided by this method is returned in a Content-Type header with GET request.
When deciding which action to perform when downloading a file some WebDAV clients and browsers (such as Internet Explorer) rely on file extension, while others (such as Firefox) rely on Content-Type header returned by server. For identical behavior in all browsers and WebDAV clients your server must return a correct mime-type with a requested file.
ServerException
- In case of an error.String getEtag() throws ServerException
ServerException
- In case of an errorlong getContentLength() throws ServerException
ServerException
- In case of an error.void read(OutputStream output, long startIndex, long count) throws ServerException, IOException
Client application can request only a part of a file specifying Range header. Download managers may use this header to download single file using several threads at a time.
output
- Output stream.startIndex
- Zero-bazed byte offset in file content at which to begin copying bytes to the output stream.count
- Number of bytes to be written to the output stream.ServerException
- In case of an error.IOException
- I/O exception occurred.
@Override public void read(OutputStream out, long startIndex, long count) throws ServerException { Path fullPath = this.getFullPath(); byte[] buf = new byte[BUFFER_SIZE]; int retVal; try (InputStream in = Files.newInputStream(fullPath)) { in.skip(startIndex); while ((retVal = in.read(buf)) > 0) { // Strict servlet API doesn't allow to write more bytes then content length. So we do this trick. if (retVal > count) { retVal = (int) count; } out.write(buf, 0, retVal); startIndex += retVal; count -= retVal; } } catch (IOException x) { throw new ServerException(x); } }
long write(InputStream content, String contentType, long startIndex, long totalFileSize) throws LockedException, ServerException, IOException
If totalContentLength
is -1 then content parameter
contains entire file content. startIndex
parameter
is always 0 in this case.
The Java WebDAV Server Engine can process two types of upload requests:
To provide information about what segment of a file is being uploaded PUT request will contain optional Content-Range: bytes XXX-XXX/XXX header.
The following example demonstrates upload to WebDAV server using POST with multipart encoding by legacy web browser. The file will be created in /mydocs/ folder.
<html> <head><title>POST Upload to WebDAV Server</title></head> <body> <form action="/mydocs/" method="post" enctype="multipart/form-data"> <input type="file" name="dummyname" /><br /> <input type="submit" /> </form> </body> </html>
content
- InputStream
to read the content of the file from.contentType
- Indicates media type of the file.startIndex
- Index in file to which corresponds first byte in content
.totalFileSize
- Total size of the file being uploaded. -1 if size is unknown.@Override public long write(InputStream content, String contentType, long startIndex, long totalFileLength) throws LockedException, ServerException, IOException { ensureHasToken(); SeekableByteChannel writer = Files.newByteChannel(getFullPath(), this.allowedOpenFileOptions); if (startIndex == 0) { // If we override the file we must set position to 0 because writer could be at not 0 position. writer = writer.truncate(0); } else { // We must set to start position in case of resume upload. writer.position(startIndex); } incrementSerialNumber(); byte[] inputBuffer = new byte[BUFFER_SIZE]; long totalWrittenBytes = startIndex; int readBytes; try { while ((readBytes = content.read(inputBuffer)) > -1) { ByteBuffer byteBuffer; byteBuffer = ByteBuffer.wrap(inputBuffer, 0, readBytes); writer.write(byteBuffer); totalWrittenBytes += readBytes; } try { getEngine().getSearchFacade().getIndexer().indexFile(getName(), decode(getPath()), null, this); } catch (Exception ex){ getEngine().getLogger().logError("Errors during indexing.", ex); } } catch (Exception ex) { ex.printStackTrace(); } finally { writer.close(); } getEngine().getWebSocketServer().notifyUpdated(getPath(), getWebSocketID()); return totalWrittenBytes; }
LockedException
- File was locked and client did not provide lock token.ServerException
- In case of an error.IOException
- I/O error.Copyright © ITHit. All Rights Reserved.