0

I want to download a .txt/.log file saved in hard disk in JSF, am not getting any error but the issue is am not able to download the file, need some help..

note : am trying to zip the file first and then download.

I have tried :

response.setContentType("text/html");
response.setContentType("text/plain");

Code in page.xhtml:

 <h:form>     
<a4j:outputPanel id="downloadPanel">
                   <table><tr>
                    <td>
                        <h:commandButton id="dldFiles" title="Download File" image="/images/download.png"  
                                            style="width:20px; height:20px;"/>
                    </td>
                    <td>
                        <h:outputText value="Download log file" style="font-size: 11px; color:#56ADF8; font-weight: bold; cursor:pointer;"/>
                   </td>
                    </tr></table>
                    <a4j:support event="onclick" action="#{sqlLoaderAction.downloadFile}" reRender="uploadForm"></a4j:support>
                </a4j:outputPanel>

            </rich:panel> 
</h:form>

In Actin Bean Methods:

public String downloadFile(){
        System.out.println("--inside exportGoogleFeed--");
        FacesContext fc = FacesContext.getCurrentInstance();
        try{

            User user = getUserBean();
            Object sp = getServiceProxy(user);


            HttpServletResponse response = ((HttpServletResponse)fc.getExternalContext().getResponse());
            fc.responseComplete();
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition","attachment;filename=downloadname.zip");

            OutputStream  respOs = response.getOutputStream();

            String dldFileName = "SQLLDR_28.txt";
            PrintWriter pw1 = new PrintWriter(new FileWriter(dldFileName , false));

            BufferedReader readbuffer = new BufferedReader(new FileReader("D:/Sqlldr_Container/downloadFile.txt"));
            String strRead;
               while((strRead=readbuffer.readLine())!=null){
                    pw1.println(strRead);
               }
            pw1.close();
            File fil = new File(dldFileName);
            ZipUploadStatusFile(dldFileName, respOs); 
            boolean bool = fil.delete();
            System.out.println("-------Temp file Created deleted - "+bool+" ------------");
            readbuffer.close();

        }
        catch (UnAuthenticatedException e) {
            e.printStackTrace();
        } /*catch (UnAuthorizedAccessException e) {
            e.printStackTrace();
        }*/ catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }


    public static void ZipUploadStatusFile(String fileName, OutputStream respOs){
        try{
            ZipOutputStream out = new ZipOutputStream(respOs);
            byte[] data = new byte[1000]; 
            BufferedInputStream in = new BufferedInputStream
            (new FileInputStream(fileName));
            int count;
            out.putNextEntry(new ZipEntry(fileName));
            while((count = in.read(data,0,1000)) != -1){  
                out.write(data, 0, count);
            }
            in.close();
            out.flush();
            out.close();
            System.out.println("Your file is zipped");  
        }catch(Exception e){
            e.printStackTrace();
        } 
    }

After executing the above method am getting below screen: enter image description here

Thank you.....

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Warrior
  • 3,184
  • 12
  • 44
  • 53

3 Answers3

2

You can't download files by ajax. JavaScript has due to security reasons no facilities to force a Save As dialogue. The best it could do in your particular case is to display the response inline.

Get rid of the <a4j:support> and make it a fullworthy synchronous request by putting the action method straight in the <h:commandButton>.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you Balus... it worked i was messing wit this from past 24 hrs.. i replace my ajax code in page.xhtml with this n its wrking fine now : – Warrior Jan 16 '12 at 12:30
0

This is the code that will fulfill the needs of downloading text file in to client system.

 public String downloadFileText() {
    File file = new File(GlobalPath);
    HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  

    response.setHeader("Content-Disposition", "attachment;filename=file.txt");  
    response.setContentLength((int) file.length());  
    ServletOutputStream out = null;  
    try {  
        FileInputStream input = new FileInputStream(file);  
        byte[] buffer = new byte[1024];  
        out = response.getOutputStream();  
        int i = 0;  
        while ((i = input.read(buffer)) != -1) {  
            out.write(buffer);  
            out.flush();  
        }  
        FacesContext.getCurrentInstance().getResponseComplete();  
    } catch (IOException err) {  
        err.printStackTrace();  
    } finally {  
        try {  
            if (out != null) {  
                out.close();  
            }  
        } catch (IOException err) {  
            err.printStackTrace();  
        }  
    }  
    return null;
}
DanteVoronoi
  • 1,133
  • 1
  • 13
  • 20
Kashif
  • 1
-2

I dont think that the code will run fine because the code is in JSF MAnaged beans and it is running at server side, so file will be downloaded at the system where application server is running, now what you need to do is to check , use two pcs, In one pc deploy the web and try to download file from other pc, then check the behaviour of code, if the file could be downloaded in client pc then thats fine other wise you need to find alternatives

Kashif
  • 1
  • The code is indeed a terrible mess, but if you read carefully (or copypaste and rename/refactor/cleanup it), then you'll discover that it actually writes to the response body after being written to local disk file system. – BalusC Mar 17 '15 at 10:09