getAbsolutePath() 类似 UploadedFile(org.apache.myfaces.custom.fileupload.UploadedFile;) 中的方法
我目前正在开发一个应用程序,用户可以选择浏览和上传 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 方法。
SampleInterface实现文件
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 中。
I am currently working on an application, where users are given an option to browse and upload excel file, I am badly stuck to get the absolute path of the file being browsed. As location could be anything (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;
}
we are using jsf 2.0 for UI development and Tomahawk library for browse button.
Sample code for browse button
t:inputFileUpload id="file" value="#{sampleInterface.inpFile}"
valueChangeListener="#{sampleInterface.inpFile}" />
Sample code for upload button
<t:commandButton action="#{sampleInterface.readExcelFile}" id="upload" value="upload"></t:commandButton>
Logic here
Browse button -> user will select the file by browsing the location
Upload button -> on Clicking upload button, it will trigger a method readExcelFile in SampleInterface.
SampleInterface Implementation File
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
......
}
File name : abc.xls
prefix : abc
suffix: xls
Please help me in getting the full path ( as in c:.....) of the file being browsed, this absolute path would then be passed to excelapachepoi class where it will get parsed and contents would be displayed/stored in ArrayList.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么需要绝对文件路径?您可以利用这些信息做什么?创建
文件
?抱歉,不,如果网络服务器运行在与网络浏览器物理上不同的机器上,那么这是绝对不可能的。再想一想。更重要的是,正确的网络浏览器不会发回有关绝对文件路径的信息。您只需根据客户端已发送的上传文件的内容创建
文件
即可。另请参阅:
Why do you need the absolute file path? What can you do with this information? Creating a
File
? Sorry no, that is absolutely not possible if the webserver runs at a physically different machine than the webbrowser. Think once again about it. Even more, a proper webbrowser doesn't send information about the absolute file path back.You just need to create the
File
based on the uploaded file's content which the client has already sent.See also:
我记得过去也遇到过一些问题。如果我没记错的话,我想你在上传文件时无法获取完整的文件路径。我认为出于安全目的浏览器不会告诉你这一点。
I remember to have some problem with this in the past too. If I am not mistaken, I think you cannot get the full file path when uploading a file. I think the browser won't tell you it for security purposes.