我已经在实体类里获取到了服务器上的绝对路径 如:
public String getImageFile() {
String path=FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/img/"); //获得上传文件的服务器上的绝对路径
path+="\\";
this.imageFile=path+imageFile;
System.out.println("imageFile="+imageFile);
return this.imageFile;
}
public void setImageFile(String imageFile) {
this.imageFile = imageFile;
}输出了:
imageFile=D:\workspance\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\rechfaces01\resources\img\b0005.jpg页面:
<rich:dataTable value="#{bookbean.books}" var="b" id="mytable" rowKeyVar="r" rows="5">
<rich:column>
<f:facet name="header">
<input type="checkbox" title="请选择"></input>
</f:facet>
<h:selectBooleanCheckbox value="#{b.check}"></h:selectBooleanCheckbox>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="序号"/>
</f:facet>
<h:outputText value="#{r+1}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="图书编号"/>
</f:facet>
<h:outputText value="#{b.bookId}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="图书名"/>
</f:facet>
<h:outputText value="#{b.bookName}"/>
<rich:tooltip>
<h:graphicImage name="#{b.imageFile}" height="100" width="100"></h:graphicImage>
</rich:tooltip>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="作者"/>
</f:facet>
<h:outputText value="#{b.author}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="出版社"/>
</f:facet>
<h:outputText value="#{b.publisher}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="出版日期"/>
</f:facet>
<h:outputText value="#{b.publishingTime}"/>
</rich:column>
<rich:column>
<f:facet name="header">
<h:outputText value="价格"/>
</f:facet>
<h:outputText value="#{b.price}"/>
</rich:column>
<f:facet name="footer">
<rich:dataScroller page="3" maxPages="5">
<f:facet name="first">
<h:outputText value="首页"/>
</f:facet>
<f:facet name="last">
<h:outputText value="末页"/>
</f:facet>
<f:facet name=""></f:facet>
</rich:dataScroller>
</f:facet>
</rich:dataTable> private DataModel<Book> books;
         public DataModel<Book> getBooks() {
if(books==null){
books=new ListDataModel<Book>();
books.setWrappedData(new BookDao().findAll());
}
return books;
}
public void setBooks(DataModel<Book> books) {
this.books = books;
}

解决方案 »

  1.   

    哥们,你这方法错误了,图片是相对于WEB路径的,不是想对于物理路径的,D://**/**.jps这样的如果包含中文的话,在网页上是取不到的,你应该获取web路径:比如:web跟路径是这样的:http://localhost/,然后你用这个路径再加上你的图片路径,类似这样:http://localhost/+你的图片路径,这样就行了。
      

  2.   

    呵呵。。我是初学者不太懂 因为我上传的时候,上传图片的路径是我获取的path,所以我读取的时候还是用了那个路径,那个http://localhost/+图片路径 我好像获取不到图片,不知道我是不是放错地方了,请详细指教
    <!-- 上传封面面板 -->
    <rich:popupPanel id="uploadPanel" autosized="true">
    <f:facet name="header">
    <h:outputText value="上传图书封面图片"/>
    </f:facet>
    <f:facet name="controls">
    <h:graphicImage name="/img/close.png" onclick="#{rich:component('uploadPanel')}.hide()"/>
    </f:facet>
    <h:form>
    <rich:fileUpload acceptedTypes="jpg,png,gif,bmp" 
    deleteLabel="删除"
    addLabel="添加"
    clearAllLabel="删除所有"
    uploadLabel="上传"
    fileUploadListener="#{bookModel.uplaodFile}"
    render="myTable"
    onuploadcomplete="#{rich:component('uploadPanel')}.hide()"/>
    </h:form>
    </rich:popupPanel>
    public void uplaodFile(FileUploadEvent event){
    UploadedFile upload = event.getUploadedFile();
    try{
    //URL url = getClass().getResource("/");
    //获取项目绝对路径
    String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/");
    System.out.println("path="+path);
    //path = path.substring(0,path.indexOf("/WEB-INF"));
    //创建文件对象
    File file = new File(path+"/resources/img/",upload.getName());
    //文件输出流对象
    FileOutputStream out = new FileOutputStream(file);
    //写入文件
    out.write(upload.getData());
    //刷新
    out.flush();
    out.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    }