【发布时间】:2011-12-09 09:05:55
【问题描述】:
我目前正在开发一个应用程序,在该应用程序中,用户可以选择浏览和上传 excel 文件,但我很难获得所浏览文件的绝对路径。因为位置可以是任何东西(Windows/Linux)。
import org.apache.myfaces.custom.fileupload.UploadedFile;
-----
-----
private UploadedFile inpFile;
-----
getters and setters
public UploadedFile getInpFile() {
return inpFile;
}
@Override
public void setInpFile(final UploadedFile inpFile) {
this.inpFile = inpFile;
}
我们使用 jsf 2.0 进行 UI 开发,使用 Tomahawk 库进行浏览按钮。
浏览按钮的示例代码
t:inputFileUpload id="file" value="#{sampleInterface.inpFile}"
valueChangeListener="#{sampleInterface.inpFile}" />
上传按钮的示例代码
<t:commandButton action="#{sampleInterface.readExcelFile}" id="upload" value="upload"></t:commandButton>
这里的逻辑
浏览按钮 -> 用户将通过浏览位置来选择文件 上传按钮 -> 点击上传按钮,会触发 SampleInterface 中的 readExcelFile 方法。
示例接口实现文件
public void readExcelFile() throws IOException {
System.out.println("File name: " + inpFile.getName());
String prefix = FilenameUtils.getBaseName(inpFile.getName());
String suffix = FilenameUtils.getExtension(inpFile.getName());
...rest of the code
......
}
文件名:abc.xls
前缀:abc
后缀:xls
请帮助我获取正在浏览的文件的完整路径(如 c:.....),然后将此绝对路径传递给 excelapachepoi 类,在那里它会被解析并显示/存储内容在 ArrayList 中。
【问题讨论】: