问题
i am now trying to use primefaces instead of a home-brewed file upload system, only problem i am having is it seems not to do anything, any ideas why ?
i am using glassfish, and i have added
commons-io-1.4.jar commons-fileupload-1.2.1/jar
to my libraries in netbeans
heres my xhtml
<p:fileUpload fileUploadListener="#{fileUploadController.upload}"
mode="advanced"
update="messages"
sizeLimit="100000000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>
web.xml,
<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>51200</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>C:\Users\Richard\printing~subversion\fileupload\web\Uploaded</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
FileUploadController.java
@ManagedBean(name="fileUploadController")
public class FileUploadController {
private String destination="C:/Users/Richard/printing~subversion/fileupload/web/Uploaded";
public void upload(FileUploadEvent event) {
FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
// Do what you want with the file
try {
copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void copyFile(String fileName, InputStream in) {
try {
// write the inputStream to a FileOutputStream
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();
System.out.println("New file created!");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
i have also tried this example : http://e-blog-java.blogspot.co.uk/2010/04/ppr-multi-file-upload-with-primefaces.html
but the issue is when i copy the code i can not get the upload to display ?
anyone got any ideas whats going on ?
Thanks
回答1:
Have you included the necessary imports in your project? If I remember correctly I once used primefaces upload and I needed to include a specific maven dependency for that upload thing.
After a quick google:
Commons IO
Commons File Upload
Or maven style:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
Combined with the answer above :)
fileUploadListener="#{uploaderBB.handleFileUpload}"
Resource: Resource
回答2:
OK solved the issue !, with primefaces you must put the filter first, before any other filters you might have ( i did have a filter from a previous file upload) so now my web.xml looks like :
<!-- <filter>
<filter-name>Upload Filter</filter-name>
<filter-class>richard.fileupload.UploadFilter</filter-class>
<init-param>
<param-name>sizeThreshold</param-name>
<param-value>1024</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Upload Filter</filter-name>
<url-pattern>/upload/*</url-pattern>
</filter-mapping> -->
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>server</param-value>
</context-param>
<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>20480</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/GUI/index.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/WEB-INF/corejsf.taglib.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
<param-value>true</param-value>
</context-param>
only thing i have noticed is now they are uploaded as a TMP file, can i still use this file for example to print out later on ?
回答3:
EDIT
This question was from January 2013 when there was only PrimeFaces 3.5. For 3.5, the default was commons-fileupload. So for 3.5, the context-param below is not required.
If you are using PrimeFaces 4 and have this problem:
The default for PrimeFaces 4 is 'native'. If you want to use commons-fileupload you need to include this parameter in your web.xml:
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
With WebLogic I was using the default but when attempting to upload, nothing happened. There was no error. With Tomcat it was fine either way. I have only tried it with Tomcat ant WebLogic.
来源:https://stackoverflow.com/questions/14562267/primefaces-upload-not-doing-anything