从上传的文件中获取文件扩展名 [英] Get file extension from uploaded file

查看:973
本文介绍了从上传的文件中获取文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我的要求是上传文件并将其存储在磁盘中.我将它存储在磁盘中没有问题,但是得到了文件的扩展名.问题是,当我单击上传并处理要存储在磁盘中的文件时,该文件另存为临时文件,其名称为

Here my requirement is to upload the file and to store it in the disk. I have no problem in storing it in disk, but getting the extension of the file. The problem is when I click on upload and processing the file to store in disk, it is saved as a temp file with following name

"/tmp/multipartBody6238081076014199817asTemporaryFile"

"/tmp/multipartBody6238081076014199817asTemporaryFile"

此处文件没有扩展名.因此,以下任何一个库都无法帮助我获取文件的扩展名.

here the file doesn't have an extension. So any of the following libraries doesn't help me get the extension of the file.

FileNameUtils.getExtension()

or

Files.getFileExtension(path)

我什至试图通过它的属性来获取它,但是它没有获取文件扩展名的选项.

I've even tried to get it through it's attributes, but it doesn't have an option to get the file extension.

Path path = Paths.get("/**/**/filepath");
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);

HTML代码:

<input type="file" name="fileInput" class="filestyle"  data-classIcon="icon-plus" data-classButton="btn btn-primary" >

从Play框架获取文件对象:

Get file object from Play framework:

MultipartFormData body = request().body().asMultipartFormData();
FilePart fileInput = body.getFile("fileInput");
File file = fileInput.getFile();

我们非常感谢您对提取文件扩展名的任何帮助.

Any help to extract file extension is highly appreciated.

谢谢

推荐答案

我已经找到了解决方案.实际上,这是Play框架.我使用以下代码获取了文件.

I have found the solution for this. Actually this is of Play framework. I got the file using following code.

MultipartFormData body = request().body().asMultipartFormData();
FilePart fileInput = body.getFile("fileInput");
File file = fileInput.getFile();

我尝试使用此 File 对象(用于存储在 tmp 位置)获取文件名.但是我没有注意到 FilePart 对象包含所有已上传的文件详细信息.然后我想通了.

I tried to get the name of file using this File object (which is used to store in a tmp location). But I missed to notice that FilePart object contains all file details that was uploaded. Then I figured it out.

fileInput.getFilename()给了我带有扩展名的上载文件名.它解决了我的问题.

fileInput.getFilename() gives me the uploaded file name with extension. It solves my problem.

感谢大灾变对我的帮助.他给出的答案肯定是其他框架(例如Struts/Spring或核心servlet)的最佳答案.

Thanks for Cataclysm for helping me out. Surely the one he gave is the best answer for other framework like Struts/Spring or core servlets.

这篇关于从上传的文件中获取文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆