我试图实现这个目标: 在Primefaces向导内:
小面包和背面豆的相关部分。 谢谢!
Facelet代码:
<p:tab id="imageUploader" title="Envio Imagem">
<p:panel header="Imagem">
<h:panelGrid columns="1" columnClasses="label, value">
<h:outputText
value="Enviar imagem: (selecione ou arraste ficheiro)" />
<p:fileUpload
value="#{newVoucherMB.newVoucher.voucherImageFilePath}"
label="Ficheiro" uploadLabel="Enviar" cancelLabel="Cancelar"
fileUploadListener="#{newVoucherMB.handleFileUpload}"
mode="advanced" dragDropSupport="true" update="growl"
sizeLimit="100000"
invalidFileMessage="Tipo de ficheiro não permitido!"
invalidSizeMessage="Ficheiro excede o tamanho permitido!"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" onstart="showStatus()"
oncomplete="hideStatus()" />
</p:dialog>
</h:panelGrid>
</p:panel>
</p:tab>
<p:tab id="imageProcessor" title="Tratamento Imagem">
<p:panel header="Selecione área da imagem">
<h:panelGrid columns="2"
rendered="#{newVoucherMB.newVoucher.voucherImageFilePath != null}">
<p:imageCropper value="#{newVoucherMB.croppedImage}"
image="/imagesUploads/#{newVoucherMB.newVoucher.voucherImageFilePath}"
initialCoords="225,75,300,125" />
<p:graphicImage id="localCroppedImage"
value="#{newVoucherMB.graphicText}" />
<p:commandButton value="Crop" actionListener="#{newVoucherMB.crop}" update="growl localCroppedImage" />
</h:panelGrid>
</p:panel>
</p:tab>
支持Bean:
@ManagedBean(name = "newVoucherMB")
@SessionScoped
public class NewVoucherMB implements Serializable {
private final String BASE_PATH = "/var/webapp/imagesUploads/";
private Voucher newVoucher;
private UploadedFile uploadedFile;
private CroppedImage croppedImage;
private String newImageName;
private StreamedContent graphicText;
public void setUploadedFile(UploadedFile uploadedFile) throws IOException {
String filename = getRandomImageName() + "_"
+ FilenameUtils.getName(uploadedFile.getFileName());
InputStream is = uploadedFile.getInputstream();
OutputStream os = new FileOutputStream(new File(
BASE_PATH, filename));
try {
IOUtils.copy(is, os);
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
this.uploadedFile = uploadedFile;
this.newVoucher.setVoucherImageFilePath(filename);
System.out.println("Nome ficheiro do upload->"
+ this.newVoucher.getVoucherImageFilePath());
}
public void handleFileUpload(FileUploadEvent event) throws IOException {
this.setUploadedFile(event.getFile());
FacesMessage msg = new FacesMessage("Ficheiro", event.getFile()
.getFileName() + " enviado com sucesso.");
// newVoucher.setImageFile(event.getFile().getContents());// as an array
// of bytes is dangerous...consumes much memory
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public String crop() {
if (croppedImage == null) {
return null;
}
setNewImageName(getRandomImageName());
String newFileName = this.newImageName + getNewImageName();
FileImageOutputStream imageOutput;
try {
imageOutput = new FileImageOutputStream(new File(newFileName));
imageOutput.write(croppedImage.getBytes(), 0,
croppedImage.getBytes().length);
imageOutput.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}