primefaces上传没有做任何事情 [英] primefaces upload not doing anything
问题描述
我现在正在尝试使用撇号而不是自制的文件上传系统,我唯一的问题是似乎什么也没做,为什么有什么主意?
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
commons-io-1.4.jar commons-fileupload-1.2.1/jar
到我在netbeans中的库
to my libraries in netbeans
这是我的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
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());
}
}
}
我也尝试过以下示例: http://e-blog-java.blogspot.co.uk/2010/04/ppr-multi-file-upload-with-primefaces.html
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 ?
谢谢
推荐答案
OK解决了这个问题!,您必须使用撇号先放置过滤器,然后再使用其他过滤器(我以前上传的文件中确实有一个过滤器) ),因此我的web.xml看起来像:
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>
我唯一注意到的是,现在它们已以TMP文件的形式上传,我是否仍可以使用该文件例如稍后打印出来?
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 ?
这篇关于primefaces上传没有做任何事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!