问题
I am new to jsf frame work.I am using jsf2.0 in eclipse ide.I have try to upload a images in jsf2.0.I have uploaded the image but it stored on eclipse server.The problem is when i clean the server the images are deleted.So i need a help,to upload photo into system local folder.please find and help me.
public void processFileUpload(FileUploadEvent event) throws AbortProcessingException {
System.out.println("Uploaded: " +event.getFile().getFileName());
UploadedFile file = event.getFile();
byte[] readData=file.getContents();
dosyaisim=file.getFileName();
System.out.println("GHFGHGH"+dosyaisim);
imgSave(readData,dosyaisim);
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void imgSave( byte[] readData,String dosyaisim )
{
try {
ExternalContext extContext=FacesContext.getCurrentInstance().getExternalContext();
FileOutputStream fos = new FileOutputStream("images"+dosyaisim);
System.out.println("IMAGE PATH="+fos);
String relativeWebPath = "e:/Images";
String absoluteDiskPath = extContext.getRealPath(relativeWebPath);
ERROR OCCURING IS:
java.io.IOException: The filename, directory name, or volume label syntax is incorrect
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.checkAndCreate(File.java:1704)
at java.io.File.createTempFile(File.java:1792)
at image.MyBean.imgSave(MyBean.java:59)
at image.MyBean.processFileUpload(MyBean.java:41)
回答1:
You were specifying a wrong file path in FileOutputStream. Fix it as follows:
FileOutputStream fos = new FileOutputStream("e:/Images/images" + dosyaisim);
// Now write InputStream to it.
You don't need ExternalContext#getRealPath() at all.
来源:https://stackoverflow.com/questions/8020156/upload-into-system-folder-using-jsf2-0