2014-12-03 71 views
0

我想知道当我们使用primefaces上传文件并使用apache tomcat服务器时会发生什么情况。据我所知,在上传到系统之前,tomcat暂时在某处存储。如果上传成功,我们可以在临时文件夹中看到吗?如果文件大小较大,则会抛出像这样的错误。如何使用primefaces在apache tomcat中上传文件?

SEVERE: Servlet.service() for servlet [Faces Servlet] in context with path [/maintenance] threw     exception 
java.io.IOException: Processing of multipart/form-data request failed. No space left on device 

任何帮助? 在此先感谢。

PS我使用的Unix

+0

“左设备上没有空间” – Stefan 2014-12-03 07:55:41

回答

1

我使用Tomcat和Apache能正常工作对我来说:

的形式必须是enctype="multipart/form-dataweb.xml

<context-param> 
<param-name>primefaces.UPLOADER</param-name> 
<param-value>commons</param-value> 
</context-param> 

<filter> 
    <filter-name>PrimeFaces FileUpload Filter</filter-name> 
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>PrimeFaces FileUpload Filter</filter-name> 
    <servlet-name>Faces Servlet</servlet-name> 
    <dispatcher>FORWARD</dispatcher> 
</filter-mapping> 

添加这些在XHTML文件:

<h:form enctype="multipart/form-data" id="upload"> 
<p:fileUpload id="fileUpload" fileUploadListener="#{uploadParcelBean.handleFileUpload}" mode="advanced" 
     allowTypes="/(\.|\/)(gif|jpe?g|png|bmp|pdf|doc|docx|xls|xlsx|txt)$/"   
     description="Select File"   
     label="Select File" uploadLabel="Upload" cancelLabel="Cancel"   
     validatorMessage="Invalid Format."  
     dragDropSupport="true"  
     multiple="true"  
     update="growl fileList" 
     disabled="false"/> 
</h:form> 

在bean方面,你可以处理上传的fil ES:

public void handleFileUpload(FileUploadEvent event) {  
    try {   
    copyFile(event.getFile().getFileName(), event.getFile().getInputstream());    
    //other logics 
    } 
    catch(IOException e){ 
    e.printStackTrace();   
    } 

和复制的方法是这样的:

public void copyFile(String fileName, InputStream in) { 

     String destination="C:\\uploads\\"; 
     try { 

       // write the inputStream to a FileOutputStream 

      File theDir = new File(destination);    
      if(!theDir.exists()) 
      { 
       try { 
        theDir.mkdir(); 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

      }    


    OutputStream out = new FileOutputStream(new File(destination + fileName)); 


       int read = 0; 
       byte[] bytes = new byte[1024]; 

       while ((read = in.read(bytes)) != -1) { 
        out.write(bytes, 0, read); 
       } 

       in.close(); 
       out.flush(); 
       out.close();   

       } catch (IOException e) { 
       System.out.println(e.getMessage()); 
       } 
    } 
+0

感谢将制定出! – murthi 2014-12-10 07:52:43

相关问题