我讓用戶上傳文件,我把它作爲這樣無法在iframe JSF
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";
什麼我基本上做的是文件在/路徑存放/到/文件顯示PDF /然後存儲在我的數據庫的位置,所以在DB值是如此
/path/to/files/t2-672409104/1.pdf
我能夠下載文件作爲這樣
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));
在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>
但是,當我嘗試顯示在iframe的文件我只想找到沒有找到
<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>
,在H值的iframe顯示資源:outputLink的越來越是/path/to/files/t2-672409104/1.pdf
在開發Web控制檯,我可以看到下面的錯誤
GET
http://localhost:8080/path/to/files/t2-672409104/1.pdf not found
所以我認爲問題是它試圖用來找到文件的路徑,從我的理解,如果我使用相對路徑/路徑/到/文件存儲該文件該文件的實際路徑是C:\路徑\到\文件
我試圖硬編碼值,這樣只是爲了測試
<h:outputLink value="C:\path\to\files\t2-672409104\1.pdf">
我得到一個消息說,它不能瞭解協議的C
所以我的問題基本上是我怎麼提供這些文件的路徑在一個框架中打開它們
對不起,這個答案根本沒有幫助。它過於寬泛和含糊地咕of着顯而易見的混亂。如果你不能具體回答,但只能在黑暗中進行拍攝,請發表評論,而不是回答。 – BalusC
對不起......請記住這一點。 – Shiva