Package de.elo.ix.jscript
Class HttpFile
java.lang.Object
de.elo.ix.jscript.FileBase
de.elo.ix.jscript.HttpFile
This class provides convenient methods to send HTTP requests.
Download from an URL
This examples downloads a file from an URL. The file is created in the temporary directory with a generated file name.
function download() {
var file = new Packages.de.elo.ix.jscript.HttpFile(url);
var fileName = file.download();
log.info("fileName=" + fileName);
}
Upload to an URL
The upload function can be used to send a file via a HTTP POST request to a web application.
function upload() {
var file = new Packages.de.elo.ix.jscript.HttpFile(url);
var ret = file.upload(fileName);
log.info("ret=" + ret);
}
Read and Write Functions
The read functions can download contents into a byte array or into a String. The write functions can post the contents of a byte array or a String to the server.
function receiveText() {
var url = "http://server:port/...";
var file = new Packages.de.elo.ix.jscript.HttpFile(url);
var content = file.readAllText();
log.info("content=" + content);
}
function sendText() {
var url = "http://server:port/...";
var file = new Packages.de.elo.ix.jscript.HttpFile(url, "text/plain; charset=UTF-8");
var content = "abcde";
var response = file.writeAllText(content);
log.info("response=" + response);
}
function receiveBytes() {
var url = "http://server:port/...";
var file = new Packages.de.elo.ix.jscript.HttpFile(url);
var content = file.readAllBytes();
log.info("content=" + content);
}
function sendBytes() {
var url = "http://server:port/...";
var file = new Packages.de.elo.ix.jscript.HttpFile(url);
var content = ...;
var response = file.writeAllBytes(content);
log.info("response=" + response);
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondownload()
Download the resource into a temporary file.Download a file from the resource.getUrl()
void
setContentType
(String contentType) void
toString()
Upload a file to the resource.Methods inherited from class de.elo.ix.jscript.FileBase
copyAllBytes, copyAllText, getCharset, isUseBom, readAllBytes, readAllText, setCharset, setUseBom, writeAllBytes, writeAllText
-
Constructor Details
-
HttpFile
Constructor. This constructor initializes the contentType property for the write methods with "application/octet-stream".- Parameters:
url
- Resource URL- Throws:
MalformedURLException
- See Also:
-
HttpFile
Constructor.- Parameters:
url
- Resource URLcontentType
- Content type, e.g. "text/plain; charset=UTF-8"- Throws:
MalformedURLException
-
HttpFile
public HttpFile(String url, String contentType, String contentEncoding) throws MalformedURLException Constructor.- Parameters:
url
- Resource URLcontentType
- Content type, e.g. "text/plain"contentEncoding
- A character set like UTF-8, UTF-16LE etc.- Throws:
MalformedURLException
-
-
Method Details
-
upload
Upload a file to the resource.- Parameters:
fileName
- File name- Returns:
- Server response
- Throws:
IOException
- An exception is thrown, if the server response is not HTTP-OK (200).
-
download
Download a file from the resource.- Parameters:
fileName
- Optional. File name. If null, a temporary file is created.- Throws:
IOException
- An exception is thrown, if the server response is not HTTP-OK (200).
-
download
Download the resource into a temporary file.- Returns:
- Name of the temporary file.
- Throws:
IOException
- An exception is thrown, if the server response is not HTTP-OK (200).
-
toString
-
getContentType
-
setContentType
-
getUrl
-
setUrl
-