1

I am letting the user upload a file and am storing it as so

List<Meetingsattachment> mal= new ArrayList<Meetingsattachment>();
    meeting.setMeetingsattachments(mal);

    Random rand=new Random();
    int num=Math.abs(rand.nextInt());
    String dirname="/path/to/files/"+meeting.getDescription()+"-"+num;

    File dir= new File(dirname);
    System.out.println(dir.getAbsolutePath());
     if(! dir.exists()) 
          dir.mkdir();

     for(UploadedFile uf:ufl){
         Meetingsattachment ma=new Meetingsattachment();
         ma.setAttachment(dirname+"/"+uf.getFileName());
         File file = new File(dirname+"/"+uf.getFileName());


            try (FileOutputStream fop = new FileOutputStream(file)) {

                // if file doesn't exists, then create it
                if (!file.exists()) {
                    file.createNewFile();
                }

                // get the content in bytes

                fop.write(IOUtils.toByteArray(uf.getInputstream()));
                fop.flush();
                fop.close();
                meeting.addMeetingsattachment(ma);
                System.out.println("Done");

            } catch (IOException e) {
                e.printStackTrace();
            }


     }

    int meetingid=mrm.addMeeting(meeting);


    return "meetings?faces-redirect=true";

What I am basically doing is taking the file storing it at /path/to/files/ and then storing the location in my database, so value in db is as so

/path/to/files/t2-672409104/1.pdf

I am able to download the file as so

Meetingsattachment ma=(Meetingsattachment) actionEvent.getComponent().getAttributes().get("attachment");
        //System.out.println("here  "+form.getRqid());
        try{

        InputStream stream = new FileInputStream(new File(ma.getAttachment()));
        String fl=ma.getAttachment(); 
        System.out.println(fl);
        String filename=fl.split("/")[5];

        //System.out.println(ra.getRaid());
        setFile(new DefaultStreamedContent(stream,"application/pdf",filename));

in the jsf

<ui:repeat var="meetattachment" value="#{meet.meetingsattachments}">
                <h:outputText value="#{meetattachment.attachment}" />

                <p:commandButton icon="ui-icon-arrowthickstop-1-s"

                        onclick="PrimeFaces.monitorDownload(showStatus, hideStatus)"
                        actionListener="#{meetBean.buttonAction}">
                        <f:attribute name="attachment" value="#{meetattachment}"></f:attribute>
                        <p:fileDownload value="#{meetBean.file}" />
                    </p:commandButton>

But when I try to display the file in an iframe I just get an iframe showing resource not found

<ui:repeat var="meetattachment" value="#{meet.meetingsattachments}">
                <h:outputText value="#{meetattachment.attachment}" />                   
                    <p:lightBox iframe="true">
                    <h:outputLink value="#{meetattachment.getAttachment()}">
                    preview2
                    </h:outputLink>
                    </p:lightBox>


                    <br />
            </ui:repeat>

The value that the h:outputlink is getting is /path/to/files/t2-672409104/1.pdf

In the developer webconsole I can see the following error

GET 
http://localhost:8080/path/to/files/t2-672409104/1.pdf not found

so I think the problem is the path it is trying to use to find the file, from my understanding if I store the file using the relative path /path/to/files the actual path for the file is C:\path\to\files

I tried hardcoding the value as such just to test

<h:outputLink value="C:\path\to\files\t2-672409104\1.pdf">

I get a message saying it cannot understand the protocol C

So my question basically is how do I provide the path to these files to open them in a frame

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
yahh
  • 1,175
  • 3
  • 14
  • 41

1 Answers1

-1

The way jsf work out the links is different. That is the reason you get the file not found error. If you want to open the file inside an iframe then you need to do some extra works. There is pdfjs library which you can download and use it to show the pdf inside an iframe. In primefaces there is a component to do that. You can use the primefaces source to see how the same is accomplished. If you want more info or complete hand holding please do let me know.

Shiva
  • 32
  • 4
  • Sorry, this answer is not at all helpful. It's overly broad and vague mumble jumble of obviousness. If you can't concretely answer but can only do some shoots in the dark, please post a comment instead of an answer. – BalusC Mar 29 '16 at 09:18
  • Sorry...will keep that in mind. – Shiva Mar 29 '16 at 09:22