Get file extension from uploaded file

后端 未结 4 844
陌清茗
陌清茗 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:42

    To get the extension of a file from servlet upload, your form enctype should be "multipart/form-data" Example upload form:

        
    Upload your file

    On the server-side, the parameter name of the file will correspond to the "name" attribute of the input.

            //...other request handling code 
        Part filePart = request.getPart("file");/*"name" attribute of your input*/
            String fileName = filePart.getSubmittedFileName();//full name of file submitted
            String[] fileNameSplit = fileName.split(".");//split between name and extension
            String extension = fileNameSplit[fileNameSplit.length-1];//get the extension part of the file name
    

提交回复
热议问题