0

I need to write a font file of format .ttf to response.I tried to use response.getoutputstream() and while calling the url.....it received a file of same size and content as far as i see.But it is not working as a font file.here is the code i used....i would really appreciate your help.

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();
}
hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
mahesh
  • 21
  • 1
  • 5

2 Answers2

1

The problem might be that that you are doing some implicit String transformations which ruin the binary data structure. Binary files should not be read line-by-line that is for text files.

You should give Files.readAllBytes a try. Like:

out.write(Files.readAllBytes(path));

And see if that works out for you. And don't write anything else.

For alternatives to the method check out File to byte[] in Java

Community
  • 1
  • 1
Matyas
  • 13,473
  • 3
  • 60
  • 73
  • This is another issue: check out alternative ways of reading binary files into an array: http://stackoverflow.com/questions/858980/file-to-byte-in-java – Matyas Aug 20 '14 at 11:47
1

First verify if this font is avaliable in your JDK. You can do it printing in your 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>

If it is ok, here is an example to generate an image with text written using our TTF font placed in WEB-INF/lib.

        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();

And for MIME type, you could try using in your response "font/opentype" if you are getting some message like:

Resource interpreted as font but transferred with MIME type font/'anything'
Bruno Franco
  • 2,028
  • 11
  • 20