我从一周开始搜索,找不到适合我的问题的解决方案。所以我问这里的专家。
我正在尝试使用FileUpload实现一个页面,以在同一页面上上传并显示上传后的图像。
我的问题是,如果我将我的托管Bean设置为@RequestScope
,将不会触发handleFileUpload
函数。如果我将其设置为 @ViewScope
,将触发该函数,但显示错误"流式动态资源中出错"。
这是我的文件:
-
网络.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>2097152</param-value> </init-param> <init-param> <param-name>uploadDirectory</param-name> <param-value>E:/uploadedImages</param-value> </init-param>
PrimeFaces FileUpload Filter 人脸奴役
-
豆
@ManagedBean @RequestScoped public class ImageManager implements Serializable { private final static int MAX_UPLOADED_FILES = 5; private final static String UPLOADED_FILES_PATH = "E:/uploadedImages"; private final Map<UUID, UploadedFile> uploadedFiles = new HashMap<>(); public List<String> getListImages() { final List<String> result = new ArrayList<>(); for (final UUID uuid : uploadedFiles.keySet()) result.add(uuid.toString()); return result; } public StreamedContent getImage() { final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); final String imageId = (String) externalContext.getRequestMap().get("imageId"); if (imageId != null) { final UUID imageIndex = UUID.fromString(imageId); return new DefaultStreamedContent(new ByteArrayInputStream(uploadedFiles.get(imageIndex).getContents()), "image/jpg"); } return null; } public void handleFileUpload(FileUploadEvent event) { if (uploadedFiles.size() < MAX_UPLOADED_FILES) { final UploadedFile uploadedFile = event.getFile(); if (uploadedFile != null) { uploadedFiles.put(UUID.randomUUID(), uploadedFile); } } } }
-
.xhtml
<p:fileUpload fileUploadListener="#{imageManager.handleFileUpload}" mode="advanced" multiple="true" sizeLimit="2097152" allowTypes="/(.|/)(gif|jpe?g|png)$/" uploadLabel="Hochladen" auto="false" cancelLabel="Abbrechen" invalidFileMessage="Die ausgewählte Datei ist kein zugelassene Bilddatei" invalidSizeMessage="Die maximale Bildgröße ist 2MB" label="Datei Auswählen" update="imageList" /> <ui:repeat value="#{imageManager.listImages}" var="imageId" id="imageList"> <h:outputText value="#{imageId}" /> <p:graphicImage value="#{imageManager.image}"> <f:param id="imageId" name="imageId" value="#{imageId}" /> </p:graphicImage> </ui:repeat>
@Daniel,相信我。在我在这里发布我的问题之前,我已经搜索了很多天。但是我已经自己解决了问题。
如果有人遇到同样的问题,这是解决方案:
@Controller
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class ImageManager implements Serializable {
private final static int MAX_UPLOADED_FILES = 5;
private final static String UPLOADED_FILES_PATH = "E:/uploadedImages";
private final static Map<UUID, UploadedFile> uploadedFiles = new HashMap<>();
private final static List<String> listImages = new ArrayList<>();
private StreamedContent image;
private static StreamedContent defaultImage = null;
static {
try {
defaultImage = new DefaultStreamedContent(new FileInputStream(new File("E:\uploadedImages\t.jpg")), "image/jpg");
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public List<String> getListImages() {
return listImages;
}
public StreamedContent getImage() {
final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
final String imageId = (String) externalContext.getRequestParameterMap().get("imageId");
if (imageId != null) {
final UUID imageIndex = UUID.fromString(imageId);
image = new DefaultStreamedContent(new ByteArrayInputStream(uploadedFiles.get(imageIndex).getContents()), "image/jpg");
return image;
}
return defaultImage;
}
public void handleFileUpload(FileUploadEvent event) {
if (uploadedFiles.size() < MAX_UPLOADED_FILES) {
final UploadedFile uploadedFile = event.getFile();
if (uploadedFile != null) {
final UUID uuid = UUID.randomUUID();
uploadedFiles.put(uuid, uploadedFile);
listImages.add(uuid.toString());
}
}
}
public void setImage(StreamedContent image) {
this.image = image;
}
}