0

画像ファイルを保存しようとしていますが、呼び出しが fileHandler メソッドに行きません。primefaces ドキュメントに記載されているように、既に web.xml にフィルターを追加しています。私が試しているこのデモ。commons-io と commons-fileupload もあります。どうすれば修正できますか?

XHTML ページ

<h:form enctype="multipart/form-data">
<p:fileUpload
fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced" update="messages" label="Choose a file"
auto="true" sizeLimit="10485760"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
invalidSizeMessage="The maximum file size allowed is 10 Megabytes !"
invalidFileMessage="You are allowed to upload only images !" />
<p:growl id="messages" showDetail="true" life="5000" />
</h:form>

@ManagedBean(name = "topicUploadBean")
@RequestScoped
public class TopicUploadBean {

    //Primitives
    private static final int BUFFER_SIZE = 6124;


    /** Create a new instance of UploadBean */
    public TopicUploadBean(){
        System.out.println("created FileUploadController instance");

    }

    public void handleFileUpload(FileUploadEvent event){
        ExternalContext extContext = FacesContext.getCurrentInstance().getExternalContext();

        File result = new File(extContext.getRealPath("//WEB-INF//files//"+event.getFile().getFileName()));
        System.out.println(extContext.getRealPath("//WEB-INF//files//"+event.getFile().getFileName()));
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(result);
            byte[] buffer = new byte[BUFFER_SIZE];
            int bulk;
            InputStream inputStream = event.getFile().getInputstream();
            while(true){
                bulk = inputStream.read(buffer);
                if (bulk < 0){
                    break;
                }
                fileOutputStream.write(buffer,0,bulk);
                fileOutputStream.flush();
            }
            fileOutputStream.close();
            inputStream.close();
            FacesMessage msg = new FacesMessage("File Description", "file name: " + event.getFile().getFileName() +" File size: "+ event.getFile().getSize()/1024 +" Kb Content type: "+ event.getFile().getContentType() + " the file was uploaded.");
        } catch(IOException e){
            e.printStackTrace();
            FacesMessage error = new FacesMessage(FacesMessage.SEVERITY_ERROR, " the files were not uploaded!", "");
            FacesContext.getCurrentInstance().addMessage(null, error);
        }
    }

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>emyed-web</display-name>
    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>1</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Production</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.SEPARATOR_CHAR</param-name>
        <param-value>_</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>#{userPreferences.theme}</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>com.sun.faces.allowTextChildren</param-name>
        <param-value>true</param-value>
    </context-param>
    <!-- Custom filter to check for user login -->
    <filter>
        <filter-name>EmyEdAccessFilter</filter-name>
        <filter-class>com.zreflect.emyed.filter.EmyEdAccessFilter</filter-class>
    </filter>
    <!-- Filter for uploading files -->
    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>thresholdSize</param-name>
            <param-value>10240</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <filter-mapping>
        <filter-name>EmyEdAccessFilter</filter-name>
        <url-pattern>/pages/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!--servlet> <servlet-name>Resource Servlet</servlet-name> <servlet-class>com.icesoft.faces.webapp.CompatResourceServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> </servlet -->
    <servlet>
        <servlet-name>ImageRenderServlet</servlet-name>
        <servlet-class>com.zreflect.emyed.servlets.ImageRenderServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>DownloadAttachmentServlet</servlet-name>
        <servlet-class>com.zreflect.emyed.servlets.DownloadAttachmentServlet</servlet-class>
    </servlet>

    <!-- Atmos newly added -->
    <servlet>
        <description>MeteorServlet</description>
        <servlet-name>MeteorServlet</servlet-name>
        <servlet-class>org.atmosphere.cpr.MeteorServlet</servlet-class>
        <init-param>
            <param-name>org.atmosphere.servlet</param-name>
            <param-value>com.zreflect.emyed.managedbean.circle.MeteorAtmosphereSharePostServlet</param-value>
        </init-param>
        <init-param>
            <param-name>org.atmosphere.useNative</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>MeteorServlet</servlet-name>
        <url-pattern>/meteor</url-pattern>
    </servlet-mapping>



    <!-- Atmos done -->

    <!--servlet> <servlet-name>Resource Servlet</servlet-name> <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class> 
        <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Resource 
        Servlet</servlet-name> <url-pattern>/primefaces_resource/*</url-pattern> 
        </servlet-mapping -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <!--url-pattern>*.iface</url-pattern -->
        <url-pattern>*.jspx</url-pattern>
        <url-pattern>/xmlhttp/*</url-pattern>
        <url-pattern>*.faces</url-pattern>
        <url-pattern>*.jsf</url-pattern>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ImageRenderServlet</servlet-name>
        <url-pattern>/photo/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>DownloadAttachmentServlet</servlet-name>
        <url-pattern>/download/attachment/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <error-code>500</error-code>
        <location>/errors/500.xhtml</location>
    </error-page>
    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/errors/500.xhtml</location>
    </error-page>
    <!-- error-page> <exception-type>javax.faces.application.ViewExpiredException</exception-type> 
        <location>/errors/viewExpired.xhtml</location> </error-page -->
</web-app>
4

1 に答える 1

0

管理対象Beanは、 「fileUploadController」ではなく「topicUploadBean」であるためです。Beanを確認するか、ある場合はfileUploadControllerBeanを投稿してください。

于 2013-02-16T14:08:07.337 に答える