Get file extension from uploaded file

后端 未结 4 843
陌清茗
陌清茗 2021-01-22 16:07

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 clic

4条回答
  •  走了就别回头了
    2021-01-22 16:49

    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();
    

    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() gives me the uploaded file name with extension. It solves my problem.

    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.

提交回复
热议问题