~~Title: Up- and Download of Files~~
One can hardly imagine an application that does not allow for the up- and download of files, whether we want to download reports or upload import data or documents. In any case, we usually want to be able to perform further actions before and after up-/download or access the data.
JVx supports the up- and download of files in its core and allows for comfortable and simple handling.
== Example ==
Our application administers companies, for each of which a logo can be uploaded. The logo can also be saved (downloaded) for promotional purposes.
There are buttons for up- and downloads, respectively, that can be activated/deactivated depending on user rights.
The following is an example of client and server actions, starting with the client implementation for the upload:
/**
* Selects the company logo with a file chooser.
*
* @throws Throwable if an error occurs while file choosing
*/
public void doUploadLogo() throws Throwable
{
getApplication().getLauncher().getFileHandle(this,
"uploadLogo", "Choose company logo");
}
/**
* Sends the company logo to the server.
*
* @param pFileHandle the company logo
* @throws Throwable if a communication error occurs
*/
public void uploadLogo(IFileHandle pFileHandle) throws Throwable
{
getConnection().callAction("uploadLogo",
rdbCompany.getValue("CPNY_ID"),
pFileHandle);
}
Client implementation for the download:
/**
* Downloads the company logo and opens a save dialog.
*
* @throws Throwable if a communication error occurs
*/
public void doDownloadLogo() throws Throwable
{
IFileHandle fileHandle = (IFileHandle)getConnection().callAction("downloadLogo",
rdbCompany.getValue("CPNY_ID"));
if (fileHandle != null)
{
getApplication().getLauncher().saveFileHandle(fileHandle);
}
}
Server implementation for the upload:
/**
* Stores the company logo.
*
* @param pCpnyId the company id
* @param pLogo the company file handle
* @throws Exception if an error occurs during store
*/
public void uploadLogo(BigDecimal pCpnyId, IFileHandle pLogo) throws Exception
{
CallableStatement statement = null;
try
{
//database mode
statement = getDataSource().getConnection().prepareCall(...);
statement.setBigDecimal(1, pCpnyId);
statement.setString(2, pFileHandle.getFileName());
statement.setBinaryStream(3,
new BufferedInputStream(pFileHandle.getInputStream()),
(int)pFileHandle.getLength());
statement.execute();
}
finally
{
try
{
statement.close();
}
catch (Throwable pThrowable)
{
// Do nothing!
}
}
}
Server implementation for the download:
/**
* Sends the company logo to the client.
*
* @param pCpnyId the company id from which we want the logo
* @return the company file handle or null
if the company is not available
* @throws Exception if an error occurs when accessing the company
*/
public RemoteFileHandle downloadFile(BigDecimal pCpnyId) throws Exception
{
CallableStatement statement = null;
try
{
statement = getDataSource().getConnection().prepareCall(...);
statement.setBigDecimal(1, pScjaId);
statement.registerOutParameter(2, Types.VARCHAR);
statement.registerOutParameter(3, Types.BLOB);
statement.execute();
String fileName = statement.getString(2);
if (fileName == null)
{
return null;
}
else
{
return new RemoteFileHandle(fileName, statement.getBlob(3).getBinaryStream());
}
}
finally
{
try
{
statement.close();
}
catch (Throwable pThrowable)
{
// Do nothing!
}
}
}