getAbsolutePath() 类似 UploadedFile(org.apache.myfaces.custom.fileupload.UploadedFile;) 中的方法

发布于 2024-12-20 20:34:46 字数 1517 浏览 2 评论 0原文

我目前正在开发一个应用程序,用户可以选择浏览和上传 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九厘米的零° 2024-12-27 20:34:46

为什么需要绝对文件路径?您可以利用这些信息做什么?创建文件?抱歉,不,如果网络服务器运行在与网络浏览器物理上不同的机器上,那么这是绝对不可能的。再想一想。更重要的是,正确的网络浏览器不会发回有关绝对文件路径的信息。

您只需根据客户端已发送的上传文件的内容创建文件即可。

String prefix = FilenameUtils.getBaseName(inpFile.getName()); 
String suffix = FilenameUtils.getExtension(inpFile.getName());
File file = File.createTempFile(prefix + "-", "." + suffix, "/path/to/uploads");

InputStream input = inpFile.getInputStream();
OutputStream output = new FileOutputStream(file);

try {
    IOUtils.copy(input, output);
} finally {
    IOUtils.closeQuietly(output);
    IOUtils.closeQuietly(input);
}

// Now you can use File.

另请参阅:

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.

String prefix = FilenameUtils.getBaseName(inpFile.getName()); 
String suffix = FilenameUtils.getExtension(inpFile.getName());
File file = File.createTempFile(prefix + "-", "." + suffix, "/path/to/uploads");

InputStream input = inpFile.getInputStream();
OutputStream output = new FileOutputStream(file);

try {
    IOUtils.copy(input, output);
} finally {
    IOUtils.closeQuietly(output);
    IOUtils.closeQuietly(input);
}

// Now you can use File.

See also:

末骤雨初歇 2024-12-27 20:34:46

我记得过去也遇到过一些问题。如果我没记错的话,我想你在上传文件时无法获取完整的文件路径。我认为出于安全目的浏览器不会告诉你这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文