我正在努力实现这一目标: 在 Primefaces 向导中:
- 在一个面板上,我有一个文件上传器
- 图像保存在本地文件系统的 /var/webapp/imageUploads 中
- 此目录通过 glassfish 虚拟服务器提供 (http)
- 在下面的面板上,有一个 imageCropper,超过了上传的图像(这是使用图形图像显示的)
- 当我在托管 bean 上调用crop() 时,我试图让裁剪后的图像显示在另一个图形图像上,并且可以存储在 /var/webapp/imageUploads 中。
- 调用crop(),总是显示转换器错误!
波纹管 facelet 和 backig bean 的相关部分。谢谢!
面码代码:
<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>
支持豆:
@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;
}
}