Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Primefaces:ImageCropper-发生转换错误-croppedImage为空_Java_Jsf_Jakarta Ee_Primefaces - Fatal编程技术网

Java Primefaces:ImageCropper-发生转换错误-croppedImage为空

Java Primefaces:ImageCropper-发生转换错误-croppedImage为空,java,jsf,jakarta-ee,primefaces,Java,Jsf,Jakarta Ee,Primefaces,首先,我正在使用 Mojarra 2.0.4 Glassfish v.3.0.1 Primeface primefaces-2.2-1.jar 因此,我有一个简单的页面,它将尝试裁剪图像,当我尝试单击commandButton调用裁剪操作时,当我更新我的咆哮消息时,会出现此转换错误。这是我的密码 <p:growl id="msgs" showDetail="true"/> <h:form> <table cellspacing="10"> &l

首先,我正在使用

Mojarra 2.0.4
Glassfish v.3.0.1
Primeface primefaces-2.2-1.jar
因此,我有一个简单的页面,它将尝试裁剪图像,当我尝试单击commandButton调用裁剪操作时,当我更新我的
咆哮
消息时,会出现此
转换错误。这是我的密码

<p:growl id="msgs" showDetail="true"/>
<h:form>
  <table cellspacing="10">
     <tr>
         <td>
             <p:imageCropper value="#{CropImage.croppedImage}" image="#{CropImage.me.profilePic}"
                                initialCoords="225,75,500" aspectRatio="1.25" />
         </td>
         <td style="vertical-align: top;">
             <h:outputText value="My Thumb Nail" styleClass="labelText"/><br/>
             <p:graphicImage value="#{CropImage.imageName}" styleClass="icon"/><br/><br/>
             <p:commandButton value="Crop" actionListener="#{CropImage.crop}" update="msgs"/>
         </td>
     </tr>
  </table>
</h:form>

你要裁剪的图像显示出来了吗

的图像属性必须是图像的相对路径

从PrimeFaces文档:

对于本地图像,ImageCropper始终 要求图像路径为上下文 相对的。所以要做到这一点很简单 只需添加斜杠(“/path/to/image.png”) imagecropper将在 %WEBAPP_ROOT%/path/to/image.png。 操作url相对本地映像是 不支持


你要裁剪的图像显示出来了吗

的图像属性必须是图像的相对路径

从PrimeFaces文档:

对于本地图像,ImageCropper始终 要求图像路径为上下文 相对的。所以要做到这一点很简单 只需添加斜杠(“/path/to/image.png”) imagecropper将在 %WEBAPP_ROOT%/path/to/image.png。 操作url相对本地映像是 不支持


我也遇到了同样的问题,并意识到这只是在用于我的
imagecrapper.image
属性的字符串中使用了
File.separator
。我用它创建了一个路径来放置上传的图像,然后重用了相同的字符串

因此,问题是:

String uploadedPhotoPath = "uploads"  + File.separator + uploadedFile.getFileName();
生成
上传\filename.jpg
p:imagecrapper显示的
我的图像很好,但我收到了
{0}转换错误,在尝试裁剪
时出现错误

我将其更改为以下内容以解决此问题:

String uploadedPhotoPath = "uploads/"+ uploadedFile.getFileName();

您还可能在ImageCropper中找到其他字符,如空格。image属性将导致此错误

我也遇到了同样的问题,并意识到这只是在用于我的
imagecrapper.image
属性的字符串中使用了
File.separator
。我用它创建了一个路径来放置上传的图像,然后重用了相同的字符串

use folder name as resources to store images and use this  code and you must use folder     name as "resources" because it will store cropped image in build and will not be able to fetch from it
index.xhtml
<h:form>
<p:growl id="msgs" showDetail="true"/>
<h:panelGrid columns="2">  
<p:imageCropper value="#{imageCropperBean.croppedImage}" image="/resources/barca/bus.jpg"          initialCoords="225,75,300,125"/>  
<p:graphicImage id="local" value="/resources/barca/#{imageCropperBean.newImageName}.jpg"/>  
</h:panelGrid> 
<p:commandButton id="xfg" value="Crop"  action="#{imageCropperBean.crop()}" update="local" />
</h:form>
and bean code is 
package com.mangium;
import org.primefaces.model.CroppedImage;
import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;  
import javax.imageio.stream.FileImageOutputStream;  
import javax.servlet.ServletContext;
@ManagedBean
@RequestScoped
public class ImageCropperBean {

@ManagedProperty(value = "#{croppedImage}")
private CroppedImage croppedImage;  

private String newImageName;  

public CroppedImage getCroppedImage() {  
    return croppedImage;  
}  

public void setCroppedImage(CroppedImage croppedImage) {  
    this.croppedImage = croppedImage;  
}  

public String crop() {  

if(croppedImage == null)  
        return null;  


setNewImageName(getRandomImageName());  

ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContex();

String newFileName = servletContext.getRealPath("")+ File.separator + "resources" +  File.separator  + "barca" + File.separator + getNewImageName() +".jpg";

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;  
}  
private String getRandomImageName() {  
int i = (int) (Math.random() * 100000);  
return String.valueOf(i);  
}  
public String getNewImageName() {  
return newImageName;  
}  
public void setNewImageName(String newImageName) {  
this.newImageName = newImageName;  
}  
}
因此,问题是:

String uploadedPhotoPath = "uploads"  + File.separator + uploadedFile.getFileName();
生成
上传\filename.jpg
p:imagecrapper显示的
我的图像很好,但我收到了
{0}转换错误,在尝试裁剪
时出现错误

我将其更改为以下内容以解决此问题:

String uploadedPhotoPath = "uploads/"+ uploadedFile.getFileName();
您还可能在ImageCropper中找到其他字符,如空格。image属性将导致此错误

将文件夹名称用作存储图像的资源,并使用此代码,您必须将文件夹名称用作“资源”,因为它将在生成中存储裁剪后的图像,并且无法从中提取
use folder name as resources to store images and use this  code and you must use folder     name as "resources" because it will store cropped image in build and will not be able to fetch from it
index.xhtml
<h:form>
<p:growl id="msgs" showDetail="true"/>
<h:panelGrid columns="2">  
<p:imageCropper value="#{imageCropperBean.croppedImage}" image="/resources/barca/bus.jpg"          initialCoords="225,75,300,125"/>  
<p:graphicImage id="local" value="/resources/barca/#{imageCropperBean.newImageName}.jpg"/>  
</h:panelGrid> 
<p:commandButton id="xfg" value="Crop"  action="#{imageCropperBean.crop()}" update="local" />
</h:form>
and bean code is 
package com.mangium;
import org.primefaces.model.CroppedImage;
import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;  
import javax.imageio.stream.FileImageOutputStream;  
import javax.servlet.ServletContext;
@ManagedBean
@RequestScoped
public class ImageCropperBean {

@ManagedProperty(value = "#{croppedImage}")
private CroppedImage croppedImage;  

private String newImageName;  

public CroppedImage getCroppedImage() {  
    return croppedImage;  
}  

public void setCroppedImage(CroppedImage croppedImage) {  
    this.croppedImage = croppedImage;  
}  

public String crop() {  

if(croppedImage == null)  
        return null;  


setNewImageName(getRandomImageName());  

ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContex();

String newFileName = servletContext.getRealPath("")+ File.separator + "resources" +  File.separator  + "barca" + File.separator + getNewImageName() +".jpg";

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;  
}  
private String getRandomImageName() {  
int i = (int) (Math.random() * 100000);  
return String.valueOf(i);  
}  
public String getNewImageName() {  
return newImageName;  
}  
public void setNewImageName(String newImageName) {  
this.newImageName = newImageName;  
}  
}
index.xhtml bean代码是 包com.mangium; 导入org.primefaces.model.crappedimage; 导入java.io.File; 导入java.io.FileNotFoundException; 导入java.io.IOException; 导入javax.faces.bean.ManagedBean; 导入javax.faces.bean.ManagedProperty; 导入javax.faces.bean.RequestScope; 导入javax.faces.context.FacesContext; 导入javax.imageio.stream.FileImageOutputStream; 导入javax.servlet.ServletContext; @ManagedBean @请求范围 公共类ImageCropperBean{ @ManagedProperty(value=“#{crappedimage}”) 私人种植图像种植图像; 私有字符串newImageName; 公共CroppedImage getCroppedImage(){ 返回裁剪图像; } 公共无效setCroppedImage(CroppedImage CroppedImage){ this.crappedimage=crappedimage; } 公共字符串裁剪(){ 如果(裁剪图像==null) 返回null; setNewImageName(getRandomImageName()); ServletContext ServletContext=(ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContex(); 字符串newFileName=servletContext.getRealPath(“”+File.separator+“resources”+File.separator+“barca”+File.separator+getNewImageName()+“.jpg”; FileImageOutputStreamImageOutput; 试试{ imageOutput=新文件ImageOutputStream(新文件(新文件名)); 写入(crappedimage.getBytes(),0,crappedimage.getBytes().length); imageOutput.close(); }catch(filenotfound异常){ e、 printStackTrace(); }捕获(IOE){ e、 printStackTrace(); } 返回null; } 私有字符串getRandomImageName(){ int i=(int)(Math.random()*100000); 返回字符串.valueOf(i); } 公共字符串getNewImageName(){ 返回newImageName; } public void setNewImageName(字符串newImageName){ this.newImageName=newImageName; } }
将文件夹名称用作存储图像的资源,并使用此代码,您必须将文件夹名称用作“资源”,因为它将在生成中存储裁剪后的图像,并且无法从中提取
index.xhtml
bean代码是
包com.mangium;
导入org.primefaces.model.crappedimage;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入javax.faces.bean.ManagedBean;
导入javax.faces.bean.ManagedProperty;
导入javax.faces.bean.RequestScope;
导入javax.faces.context.FacesContext;
导入javax.imageio.stream.FileImageOutputStream;
导入javax.servlet.ServletContext;
@ManagedBean
@请求范围
公共类ImageCropperBean{
@ManagedProperty(value=“#{crappedimage}”)
私人种植图像种植图像;
私有字符串newImageName;
公共CroppedImage getCroppedImage(){
返回裁剪图像;
}  
公共无效setCroppedImage(CroppedImage CroppedImage){
this.crappedimage=crappedimage;
}  
公共字符串裁剪(){
如果(裁剪图像==null)
返回null;
setNewImageName(getRandomImageName());
ServletContext ServletContext=(ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContex();
字符串newFileName=servletContext.getRealPath(“”+File.separator+“resources”+File.separator+“barca”+File.separator+getN