Я использую PrimeFaces <p:fileUpload>
. Он не вызывает метод слушателя. Если я добавлю FileUploadFilter
, то получаю исключение.PrimeFaces uploadFile не работает
Вид:
<h:form enctype="multipart/form-data">
<p:fileUpload mode="advanced"
fileUploadListener="#{fileUploadController.upload()}"
allowTypes="/(\.|\/)(gif|jpg|jpeg|gif|png|PNG|GIF|JPG|JPEG)$/"
auto="false" />
</h:form>
Bean:
public class fileUploadController {
private String destination = "c:\test";
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());
}
}
}
web.xml
<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>
Оформить заказ этого руководства: http://www.java-tutorial.ch/java-server-faces/file-upload -with-primefaces – Ioan
Зачем вы получаете? Является ли «Faces Servlet» именем вашего сервлета jsf? В противном случае вы должны настроить следующую строку: <фильтр-отображение> PrimeFaces FileUpload Фильтр фильтр-имя> <имя сервлета> Faces Servlet <имя сервлета /> фильтр-отображение> в <фильтр-картографирования> PrimeFaces FileUpload фильтр <имя-фильтра /> <имя сервлета> иМЯ сервлет ЗДЕСЬ <имя сервлета /> фильтр-отображение> –
mooonli
Faces Servlet не имя для моего сервлета jsf, – PyaePhyoAung