I'm trying to implement primefaces fileupload with imageCropper for change user's avatar under https.
I upload the image and save it into application server's temp folder.
I served the uploaded image into primefaces imageCropper with a WebServlet from server's temp path
When I use http everything works fine, but when I turned into https I have error {0}: Conversion error occurred.
This is my code:
xhtml code
<p:imageCropper
id="avatarImage"
image="https://#{request.serverName}:#{request.serverPort}#{request.contextPath}/reports/#{UserpreferencesBean.imageFilePath}"
value="#{UserpreferencesBean.croppedImage}"
aspectRatio="1.0" initialCoords="225,75,300,125"
boxWidth="400"
boxHeight="400"
minSize="90,90"/>
<br/>
<p:commandButton id="cropButton"
value="Crop"
action="#{UserpreferencesBean.crop()}"
update="form:messages image avatarImage avatarForm"
icon="ui-icon-scissors"/>
Bean code
public void crop() throws IOException {
avatarImage = new DefaultStreamedContent(null);
avatarImage = new DefaultStreamedContent(new ByteArrayInputStream(croppedImage.getBytes()));
in = new ByteArrayInputStream(croppedImage.getBytes());
avatarByteArray = croppedImage.getBytes();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Success", "Cropping finished."));
}
WebServlet code
@WebServlet("/reports/*")
public class ImageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = request.getPathInfo().substring(1);
File file = new File(System.getProperty("jboss.server.temp.dir"), filename);
response.setHeader("Content-Type", getServletContext().getMimeType(filename));
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename="+File.separator + filename + File.separator );
Files.copy(file.toPath(), response.getOutputStream());
}
}
Some notes
-> My ssl is not valid. I use a self-sign certificate
-> My application server is Wildfly 16