복붙노트

[SPRING] Fileupload가 Primefaces에서 작동하지 않습니다.

SPRING

Fileupload가 Primefaces에서 작동하지 않습니다.

Primefaces의

에 몇 가지 문제가 있습니다. 처음에는 2018 페이지의 Fileupload 문서에서 스 니펫을 시도했습니다. 첫 번째 문제는 스 니펫을 사용하면 업로드 기능이 호출되지 않는다는 것입니다.

public void upload() {
    System.out.println("This is never shown");

    if(file != null) {
            System.out.println("This is never shown");
    }
}

html :

<h:form  enctype="multipart/form-data">
    <p:fileUpload value="#{fileUploadView.file}" mode="simple" />
    <p:commandButton value="Submit" action="#{fileUploadView.upload}" ajax="false"/>
</h:form>

나는 많은 다른 스 니펫과 솔루션을 시도했다. 고급 모드에서 fileUploadListener를 사용하여 h : form, ...에 enctype을 삭제했습니다.

mode = "simple"을 선택하고 ajax = "true"와 결합하면 FileUploadView.upload () 함수가 호출되지만 파일은 항상 NULL입니다.

에서 enctype을 삭제하면 FileUploadView.upload () 함수가 호출되지만 파일은 항상 NULL입니다.

이 포럼에서 가능한 모든 솔루션을 읽었 기 때문에 위의 문제를 언급 할 수 있습니다. 또한 모든 제안 된 종속성 및 컨텍스트 매개 변수와 필터를 추가했지만 작동하지 않습니다.

여기에 내 pom.xml, web.xml 및 전체 .java 코드를 게시합니다. pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>at.qe</groupId>
    <artifactId>ASN_Application</artifactId>
    <version>1.0.0</version>
    <packaging>war</packaging>


    <name>ASN_Application</name>
    <description>
    </description>

    <repositories>
        <repository>
            <id>prime-repo</id>
            <name>Prime Repo</name>
            <url>http://repository.primefaces.org</url>
        </repository>


        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>

    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <properties>
        <start-class>at.qe.sepm.asn_app.Main</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>

        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces.themes</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.10</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>el-impl</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>2.0-PFD2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.14</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.13</version>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>6.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.16</version>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>

        <!-- special test dependencies -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>nl.jqno.equalsverifier</groupId>
            <artifactId>equalsverifier</artifactId>
            <version>2.1.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- Enable jacoco analysis -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <forkMode>once</forkMode>
                    <argLine>
                        ${coverageAgent}
                    </argLine>
                    <useSystemClassLoader>true</useSystemClassLoader>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.6.201602180812</version>
                <configuration>
                    <propertyName>coverageAgent</propertyName>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

을 포함한다.

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd"
         version="2.5">

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <error-page>
        <exception-type>org.springframework.security.access.AccessDeniedException</exception-type>
        <location>/error/access_denied.xhtml</location>
    </error-page>


    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</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>primefaces.FONT_AWESOME</param-name>
    <param-value>true</param-value>
</context-param>
 <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>
</filter-mapping>
    <mime-mapping>
    <extension>eot</extension>
    <mime-type>application/vnd.ms-fontobject</mime-type>
</mime-mapping>
<mime-mapping>  
    <extension>otf</extension>  
    <mime-type>font/opentype</mime-type>  
</mime-mapping>      
<mime-mapping>  
    <extension>ttf</extension>  
    <mime-type>application/x-font-ttf</mime-type>  
</mime-mapping>      
<mime-mapping>  
    <extension>woff</extension>  
    <mime-type>application/x-font-woff</mime-type>  
</mime-mapping>
<mime-mapping>  
    <extension>svg</extension>  
    <mime-type>image/svg+xml</mime-type>  
</mime-mapping>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>login.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

FileUploadView.java

@Component
@Scope("view")    
public class FileUploadView{
        @Autowired
        private PictureService pictureService;
        @Autowired
        private UserRepository userRepository;
        @Autowired
        private AuditLogRepository auditLogRepository;
        private Picture picture;
        private UploadedFile file;

        public Picture getPicture(){
            return picture;
        }

        public void setPicture(Picture picture){
            this.picture = picture;
        }


        public UploadedFile getFile() {
            return file;
        }

        public void setFile(UploadedFile file) {
            this.file = file;
        }
        @PostConstruct
        public void init(){
            file= new DefaultUploadedFile();
        }

        public void upload() {

            if(file != null) {
                try{
                    System.out.println("This is never shown");

                    AuditLog log = new AuditLog(getAuthenticatedUser().getUsername(),"PICTURE UPLOADED: " + getAuthenticatedUser().getUsername() + " [" + getAuthenticatedUser().getUserRole() + "] ", new Date());
                    auditLogRepository.save(log);
                    System.out.println(file.getFileName());
                    picture = new Picture(file.getFileName(), userRepository.findFirstByUsername(auth.getName()), new Date(), file.getFileName());
                    pictureService.savePicture(picture);
                FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
                FacesContext.getCurrentInstance().addMessage(null, message);
                String filename = FilenameUtils.getName(file.getFileName());
                InputStream input = file.getInputstream();
                OutputStream output = new FileOutputStream(new File( filename));
                try {
                    IOUtils.copy(input, output);
                } finally {
                    IOUtils.closeQuietly(input);
                    IOUtils.closeQuietly(output);
                }
                }catch(Exception e)
                {

                }
            }
        }

누군가가 내가 문제를 찾는 것을 도울 수 있으면 고맙겠습니다.

미리 감사드립니다.

[편집] 깨끗한 Spring 프로젝트를 만들려고 노력했으며

를 만들 수있는 모든 가능성을 시도했지만 그 중 아무 것도 작동하지 않았습니다. 그러나 나는 브라우저의 트래픽을 살펴 보았고 흥미로운 것을 발견했습니다. . 업로드 버튼을 누르면 POST 문이 즉시 나타나고 다른 GET은 1-2 초 후에 나타납니다.

해결법

  1. ==============================

    1.설명서에 나온다.

    설명서에 나온다.

    FileUploadEvent 매개 변수와 함께 fileUploadListener를 추가하십시오. AJAXical 버튼을 만듭니다. enctype을 잃어 버리십시오. fileUpload의 값으로 귀찮게하지 마십시오.

  2. from https://stackoverflow.com/questions/43804766/fileupload-does-not-work-in-primefaces by cc-by-sa and MIT license