本文介绍了使用jsf2.0上传到系统文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是jsf框架工作的新手.我在eclipse ide中使用jsf2.0.我尝试在jsf2.0中上传图像.我已上传图像但它存储在eclipse服务器上.问题是当我清理服务器上删除的图像.因此,我需要帮助,将照片上传到系统本地文件夹中.请找到并帮助我.
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);
错误发生的原因是:
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)
推荐答案
您在FileOutputStream
中指定了错误的文件路径.对其进行如下修复:
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.
您根本不需要ExternalContext#getRealPath()
.
这篇关于使用jsf2.0上传到系统文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!