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
|
public String buttonUpload_action() {
// TODO: Process the button click action. Return value is a navigation
// case name where null will return to the same page.
// TODO: Process the button click action. Return value is a navigation
// case name where null will return to the same page.
UploadedFile uploadedFile = this.fileUploadValeurs.getUploadedFile();
String uploadedFileName = uploadedFile.getOriginalName();
String realPath = "";
// 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;
}
}
String uploadedFileType = uploadedFile.getContentType();
if ( uploadedFileType.equalsIgnoreCase("application/vnd.ms-excel")) {
try {
ServletContext theApplicationsServletContext = (ServletContext) this.getExternalContext().getContext();
realPath = theApplicationsServletContext.getRealPath("/upload");
File file = new File(realPath + File.separatorChar + justFileName);
uploadedFile.write(file);
System.out.println("Ecriture ok");
} catch (Exception ex) {
error("Cannot upload file: " + justFileName);
}
} else {
error("You must upload a microsoft excel 97-2003 file.");
new File(realPath + File.separatorChar + justFileName).delete();
}
return null;
} |
Partager