Primefaces ImageCropper上传的图像不起作用

时间:2013-06-18 17:17:09

标签: jsf-2 primefaces

我试图实现这个目标: 在Primefaces向导内:

  • 在一个面板上我有一个fileuploader
  • 图像保存在本地文件系统上的/ var / webapp / imageUploads
  • 此目录通过glassfish虚拟服务器
  • 提供(http)
  • 在以下面板上,有一个imageCropper,已上传 图像(使用graphicImage显示)
  • 当我在托管bean上调用crop()时,我试图获得裁剪 图像显示在另一个graphicImage上,可以存储 在/ var / webapp / imageUploads。
  • 调用crop(),始终显示转换器错误!

小面包和背面豆的相关部分。 谢谢!

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;
}

}

0 个答案:

没有答案