我需要将一个格式为.ttf的字体文件写入response.I尝试使用response.getoutputstream()
并在调用url时.....它收到了一个相同大小和内容的文件,就我所见。但它不能用作字体文件。我使用的代码就是....我非常感谢你的帮助。
File file = new File(SubsettedSavedPath);
if (file.exists()) {
response.addHeader("19.file", "exist");
// out.println("file exist<br><br><br><br><br><br>");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
StringBuilder stringBuilder = new StringBuilder();
String ls = System.getProperty("19.line.separator");
response.addHeader("20.reading File", "Started");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
String str = stringBuilder.toString();
response.setContentType("font/ttf");
OutputStream out = response.getOutputStream();
out.write(str.getBytes());
// out.write(str.getBytes());
// out.flush();
response.addHeader("21.byte returned", "successful");
out.close();
}
答案 0 :(得分:1)
问题可能在于您正在进行一些破坏二进制数据结构的隐式String
转换。不应逐行读取文件文件。
您应该尝试Files.readAllBytes
。喜欢:
out.write(Files.readAllBytes(path));
看看这是否适合你。不要写任何其他内容。
有关方法的替代方法,请查看File to byte[] in Java
答案 1 :(得分:1)
首先验证JDK中是否可以使用此字体。您可以在JSP中进行打印:
<%@page pageEncoding="UTF-8" language="java" import="java.awt.*"%>
<%@page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<META http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
<% GraphicsEnvironment e =
GraphicsEnvironment.getLocalGraphicsEnvironment(); Font[] fonts =
e.getAllFonts(); // Get the fonts for (Font f : fonts) {
out.println(f.getFontName()); out.println("
<br />"); } %>
</body>
</html>
如果没问题,这是一个生成图像的示例,该图像使用我们放在WEB-INF / lib中的TTF字体编写的文本。
String text = "Custom font test";
String font_file = "verdana.ttf";
font_file = request.getRealPath("WEB-INF/lib/" + font_file);
Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(
font_file));
font = font.deriveFont(25.0f);
// create temporary 1x1 image to get FontRenderingContext needed to
// calculate image size
BufferedImage buffer = new BufferedImage(1, 1,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = buffer.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
FontRenderContext fc = g2.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(text, fc);
// calculate the size of the text
int width = (int) bounds.getWidth();
int height = (int) bounds.getHeight();
// prepare final image with proper dimensions
buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2 = buffer.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(font);
// actually do the drawing
g2.setColor(Color.white);
g2.fillRect(0, 0, width, height);
g2.setColor(Color.blue);
g2.drawString(text, 0, (int) -bounds.getY());
// set the content type, get the output stream and print image as PNG
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
ImageIO.write(buffer, "png", os);
os.close();
对于MIME类型,如果您收到以下消息,可以尝试在响应中使用“font / opentype”:
Resource interpreted as font but transferred with MIME type font/'anything'