使用jsf2.0上传到系统文件夹

时间:2011-11-05 12:24:45

标签: jsf jsf-2

我是jsf框架工作的新手。我在eclipse ide中使用jsf2.0。我试图在jsf2.0中上传图像。我已经上传了图像但它存储在eclipse服务器上。问题是当我清理服务器图像被删除。所以我需要帮助,将照片上传到系统本地文件夹。请找到并帮助我。

    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)

1 个答案:

答案 0 :(得分:0)

您在FileOutputStream中指定了错误的文件路径。修复如下:

FileOutputStream fos = new FileOutputStream("e:/Images/images" + dosyaisim);
// Now write InputStream to it.

您根本不需要ExternalContext#getRealPath()