• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

File Upload in JSF

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,
Is there anyway to upload a file using JSF only.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tomahawk has <t:inputFileUpload>
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic