gpt4 book ai didi

jsf - 在 JSF 中创建并显示缩略图 (byte[])

转载 作者:行者123 更新时间:2023-12-02 08:17:02 25 4
gpt4 key购买 nike

我正在将图像上传到服务器,当图像上传时,它应该显示上传图像的缩略图。缩略图未保存在硬盘上我使用InputStream 和OutputStream。对于上传,我是 ustig tomahawk。

我的index.jsp:

<h:form id="uploadForm" enctype="multipart/form-data">
<t:inputFileUpload id="fileupload"
accept="image/*"
storage="file"
value="#{fileUpload.uploadedFile}"
styleClass="fileUploadInput"
required="true"
validator="epacient.FileUploadValidator"
requiredMessage="Obvezna izbira datoteke."
/>
<br />
<h:message for="fileupload" infoStyle="color: green;"
errorStyle="color: red;" />
<br />
<h:commandButton value="Upload" id="fileUploadButton"
action="#{fileUpload.upload}" />
<h:message for="uploadForm" style="color: red;" />
<h:graphicImage value="#{fileUpload.thumb}"
rendered="#{fileUpload.uploaded}" />

</h:form>

fileUpload.upload调用函数String preview()

private  String thumb ;
public String preview() throws IOException{
HttpServletResponse response = (HttpServletResponse)FacesContext
.getCurrentInstance().getExternalContext().getResponse();
try {
FacesContext context = FacesContext.getCurrentInstance();
Map requestMap = context.getExternalContext().getApplicationMap();
byte[] bytes = (byte[])(requestMap.get("fileupload_bytes"));
// returns byte[]
byte[] testByte = createThumbnail(bytes, 200);
// here thumbnail is created
} catch (Exception ex) {
ex.printStackTrace();
}
}

创建缩略图:

public static byte[] createThumbnail( byte[] orig, int maxDim) {
try {
ImageIcon imageIcon = new ImageIcon(orig);
Image inImage = imageIcon.getImage();
double scale = (double) maxDim / (double) inImage.getWidth(null);

int scaledW = (int) (scale * inImage.getWidth(null));
int scaledH = (int) (scale * inImage.getHeight(null));

BufferedImage outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
AffineTransform tx = new AffineTransform();

if (scale < 1.0d) {
tx.scale(scale, scale);
}

Graphics2D g2d = outImage.createGraphics();
g2d.drawImage(inImage, tx, null);
g2d.dispose();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(outImage, "JPG", baos);
byte[] bytesOut = baos.toByteArray();

return bytesOut;
} catch (IOException e) {
System.out.println("Erro: " + e.getMessage());
e.printStackTrace();
}
return null;
}

现在我有了缩略图,但它在 byte[] 中谁能告诉我如何用 <h:graphicImage> 显示我的拇指标签?或者任何其他方式。

谢谢!

最佳答案

每个图像都被视为单独的请求。您不能在单个请求/响应周期中同时处理(JSF 的)HTML 响应和图像。您需要将图像/拇指存储在数据存储中的某个位置,该数据存储的生命周期比请求长,例如服务器的本地磁盘文件系统(临时文件夹?webcontent 文件夹?),或者数据库(临时表?),或者在 session 中。

首先,替换

<h:graphicImage value="#{fileUpload.thumb}" ...

<h:graphicImage value="thumbs/#{fileUpload.thumbId}" ...

使其生成为

<img src="thumbs/123" ...

(src 应指向有效 URL)

然后,创建一个映射到 /thumbs/*url-patternHttpServlet 并实现 doGet() 大致如下:

Long thumbId = Long.valueOf(request.getPathInfo().substring(1)); // 123
byte[] thumb = getItFromDiskOrDatabaseOrSessionByThumbId(thumbId);
String filename = getOriginalFilenameOfUploadedImageOrInventOne();

response.setContentType(getServletContext().getMimeType(filename));
response.setContentLength(thumb.length);
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");

BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
input = new BufferedInputStream(new ByteArrayInputStream(thumb));
output = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[8192];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}

就是这样。更多servlet提示可以在this article中找到.

关于jsf - 在 JSF 中创建并显示缩略图 (byte[]),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2288399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com