Hello there,

Im new in JSP
Just want to ask, how I can force web browser to popup download box to download the .prn file that was generated by server?

Assume that

fileName.getAbsolutePath()

is the variable that hold the path for .prn file

*** Im using pure JSP

Recommended Answers

All 2 Replies

skycrew>that was generated by server?
No

Server prepare a response object - A response object consists headers and body. Headers are instructions for the browser and body contains the data to be render by the browser.

For downloading a file:

<%@ page language="java" import="java.io.*"%>

<%!
    void download(String filename,PageContext out) throws Exception {

       ServletContext sc=getServletContext();
       File file=new File(sc.getReadPath("/files");  // assume that your .prn is located under files folder.
       String path="";
       path=sc.getRealPath("files\\" + filename);
       InputStream fr=new FileInputStream(path);
       byte []b=new byte[fr.available()];
       fr.read(b,0,b.length);
       fs.close();
       
        HttpServletResponse resp=(HttpServletResponse)out.getResponse();

       resp.resetBuffer();
       resp.setHeader("Content-Type","application/octate-stream");
       resp.setHeader("Content-Length",String.valueOf(b.length));
       resp.setHeader("Content-Disposition","attachment; filename=" + filename);
      resp.getOutputStream().write(b,0,b.length);
      resp.flushBuffer();
   }
%>

<%
// call to a download() method
download("sample.prn");
%>

thank you very much adatapost for the strong explanation and code.

will trying this code.

actually I use the .prn file to print a bar code using bar code printer.

before this, the barcode printer is on client side and the system run locally.

now when the system run on network, client cannot print the barcode because the barcode printer is on client side.

commented: How is your.prn file generated on the server? +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.