• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Upload in JSF  RSS feed

 
Fawad Ali
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,
Is there anyway to upload a file using JSF only.

 
Joe ONeil
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomahawk has <t:inputFileUpload>
 
mih ira
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String fileName=null;
com.sun.webui.jsf.model.UploadedFile uploadedFile = fuAttachment.getUploadedFile();
try {
if (uploadedFile == null) {
}
String uploadedFileName = uploadedFile.getOriginalName();
int index = uploadedFileName.lastIndexOf('/');

if (index >= 0) {
fileName = uploadedFileName.substring(index + 1);
} else {
index = uploadedFileName.lastIndexOf('\\');
if (index >= 0) {
fileName = uploadedFileName.substring(index + 1);
} else {
fileName = uploadedFileName;
}
}

} catch (Exception ex) {
ex.printStackTrace();
//Logger.getLogger(AddIssue.class.getName()).log(Level.SEVERE, null, ex);
}

try {
FacesContext fcontext = FacesContext.getCurrentInstance();
ServletContext scontext = (ServletContext) fcontext.getExternalContext().getContext();
String rootPath = scontext.getRealPath("/");
rootPath = rootPath + "resources\\uploaded";
File dir = new File(rootPath);

if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir.getCanonicalPath() + File.separatorChar + fileName);

//if (file.length()/1024*1024 <= 1024)
long fileSize = file.length();
uploadedFile.write(file);

}catch(Exception ex){
ex.printStackTrace();
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!