gpt4 book ai didi

spring - 文件上传在 Primefaces 中不起作用

转载 作者:IT老高 更新时间:2023-10-28 13:47:34 29 4
gpt4 key购买 nike

我对 <p:fileUpload> 有一些问题在 Primefaces 中。起初我尝试了 Documentation of Fileupload 中的片段从 2018 页开始。第一个问题是,如果我使用代码段,则不会调用上传函数:

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。

如果我从 <h:form> 中删除 enctype , 函数 FileUploadView.upload()被调用,但文件始终为 NULL。

我提到了上面的问题,因为这也不应该起作用,因为我在这个论坛上阅读了所有可能的解决方案。我还添加了所有建议的 dependenciescontext-paramsfilter ,但它不起作用。

在这里我将发布我的 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>

web.xml

<?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 项目,并尝试了所有可能的制作 <p:fileUpload> ,但它们都不起作用。但是我查看了浏览器的流量,发现了一些有趣的东西: Traffic after I uploaded the File with advanced . POST 语句在我按下上传按钮后立即出现,其他 GET 语句在 1-2 秒后出现。

最佳答案

文档说

Advanced File Upload

FileUploadListener is the way to access the uploaded files in this mode, when a file is uploadeddefined fileUploadListener is processed with a FileUploadEvent as the parameter.

使用 FileUploadEvent 参数添加 fileUploadListener。使按钮 AJAXical。丢失编码类型。不要在意 fileUpload 的值。

关于spring - 文件上传在 Primefaces 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43804766/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com