Examples
Provides a file upload form and displays the results of the upload. Note
the form's enctype must is set to "multipart/form-data".
<form jwcid="form" enctype="multipart/form-data">
<table bgcolor="#c0c0c0" cellpadding="4">
<tr>
<td colspan="2">File: <input jwcid="fileUpload" type= "file"></input></td>
</tr>
<tr>
<td colspan="2"><input type= "submit"value="Upload"></input></td>
</tr>
<tr>
<td colspan="2"><hr></td>
</tr>
<tr>
<td>Filename:</td><td><span jwcid="insertFileName"/>/td>
</tr>
<tr>
<td>Client path:</td><td><span jwcid="insertClientPath"/></td>
</tr>
<tr>
<td>Server Path:</td><td><span jwcid="insertServerPath"/></td>
</tr>
<tr>
<td>File Truncated:</td><td><span jwcid="insertFileTruncated"/></td>
</tr>
<tr>
<td>File Size:</td><td><span jwcid="insertFileSize"/> bytes</td>
</tr>
</table>
</form>
<component id="form" type="Form">
<binding name="listener" expression="listener.formSubmit"/>
</component>
<component id="fileUpload" type="Upload">
<binding name="file" expression="file"/>
</component>
<component id="insertFileName" type="Insert">
<binding name="value" expression="filename"/>
</component>
<component id="insertClientPath" type="Insert">
<binding name="value" expression="clientPath"/>
</component>
<component id="insertServerPath" type="Insert">
<binding name="value" expression="serverPath"/>
</component>
<component id="insertFileTruncated" type="Insert">
<binding name="value" expression="fileTruncated"/>
</component>
<component id="insertFileSize" type="Insert">
<binding name="value" expression="fileSize"/>
<field-binding name="format" field-name="UploadPage.SIZE_FORMAT"/>
</component>
public class UploadPage extends BasePage {
public static final Format SIZE_FORMAT = NumberFormat.getNumberInstance();
private IUploadFile file;
private File serverFile;
public IUploadFile getFile() { return file; }
public void setFile(IUploadFile value) {
file = value;
}
public String getFilename() {
if (file != null) {
return file.getFileName();
} else {
return "";
}
}
public String getClientPath() {
if
(file != null)
{ returnfile.getFilePath(); }else
{
return ""; } } public String
getServerPath() { if (serverFile ! =
null) { return
serverFile.getAbsolutePath(); } else { return
""; }
}
public long getFileSize() {
if (serverFile != null) {
return serverFile.length();
} else {
return 0;
}
}
public boolean isFileTruncated() {
if (file != null) {
return file.isTruncated();
} else {
return false;
}
}
public void formSubmit(IRequestCycle cycle)
{ InputStream fis=
file.getStream(); FileOutputStream fos=
null;
try { fos=
new FileOutputStream(new File(file.getFileName()));
byte[] buffer =
new
byte[1024]; while (true) {
int length =
fis.read(buffer); if
(length < 0) {
break;
}
fos.write(buffer, 0, length);
}
fis.close();
fos.close();
serverFile = new File(file.getFileName());
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (fis != null) {
try { fis.close(); } catch (IOException ioe) {}
}
if (fos != null) {
try { fos.close(); } catch (IOException ioe) {}
}
}
}
public void detach() {
file = null;
serverFile = null;
super.detach();
}
}
|