1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
UploadedFile uploadedFile = fileUpload1.getUploadedFile();
if( uploadedFile == null )
return null;
String uploadedFileName = uploadedFile.getOriginalName();
// Some browsers return complete path name, some don't
// Make sure we only have the file name
// First, try forward slash
int index = uploadedFileName.lastIndexOf('/');
String justFileName;
if ( index >= 0) {
justFileName = uploadedFileName.substring( index + 1 );
} else {
// Try backslash
index = uploadedFileName.lastIndexOf('\\');
if (index >= 0) {
justFileName = uploadedFileName.substring( index + 1 );
} else {
// No forward or back slashes
justFileName = uploadedFileName;
}
}
this.fileNameStaticText.setValue(justFileName);
Long uploadedFileSize = new Long(uploadedFile.getSize());
this.fileSizeStaticText.setValue(uploadedFileSize);
String uploadedFileType = uploadedFile.getContentType();
this.fileTypeStaticText.setValue(uploadedFileType);
if ( uploadedFileType.equals("image/jpeg")
|| uploadedFileType.equals("image/pjpeg")
|| uploadedFileType.equals("image/gif")
|| uploadedFileType.equals("image/png")
|| uploadedFileType.equals("image/x-png")) {
try {
File file = new File(this.realImageFilePath);
uploadedFile.write(file);
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("image.gif")));
out.write(this.getFileUpload1().getUploadedFile().getBytes());
out.close();
// FctUtil.FctFile.copyFile(s, d);
} catch (Exception ex) {
error("Cannot upload file: " + justFileName +"|"+ex.getMessage());
}
} else {
error("You must upload a JPEG, PJPEG, GIF, PNG, or X-PNG file.");
new File(this.realImageFilePath).delete();
}
this.getLabel5().setText(this.IMAGE_URL);
this.getLabel6().setText(new File(this.realImageFilePath).exists());
return null;
} |
Partager