Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the file name while using file type input

I have a jsp with this code snippet in it.

<form name="AudioFileConversionForm" enctype="multipart/form-data" method="post" >
Choose File: <input type="file" id="audioFile" name="audioFile"><br>
<input type="submit" value="upload">
</form>

This is my controller in spring.

public String convertFile(HttpServletRequest request, HttpSession session) {

    String audioFile = request.getParameter("audioFile");
    System.out.println(request.getParameter("audioFile"));
    System.out.println("Audio File Conversion Successful");
}

I am unable to retrieve the name of the file, it shows null. I know that I can retrieve the name using JQuery or javascript, but I don't want to use them both. I want to do it using pure java. Can anyone please help me?

like image 997
Freakyuser Avatar asked Dec 19 '12 06:12

Freakyuser


2 Answers

When you upload the file, request is instance of org.springframework.web.multipart.MultipartHttpServletRequest. So you can cast it in your method convertFile(). See below :

public String convertFile(HttpServletRequest request, HttpSession session) {
    // cast request
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    // You can get your file from request
    CommonsMultipartFile multipartFile =  null; // multipart file class depends on which class you use assuming you are using org.springframework.web.multipart.commons.CommonsMultipartFile

    Iterator<String> iterator = multipartRequest.getFileNames();

    while (iterator.hasNext()) {
        String key = (String) iterator.next();
        // create multipartFile array if you upload multiple files
        multipartFile = (CommonsMultipartFile) multipartRequest.getFile(key);
    }

    // logic for conversion
}

However I am unable to retrieve (receiving null value) the name of the file that I chose in the JSP page.

---> To get file name you can get it as :

multipartFile.getOriginalFilename();  // get filename on client's machine
multipartFile.getContentType();       // get content type, you can recognize which kind of file is, pdf or image or doc etc
multipartFile.getSize()          // get file size in bytes

To make file upload work, you need to make sure you are creating multipart resolver bean as below :

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="100000"/>
</bean>

Reference : Spring documentation

like image 120
Nandkumar Tekale Avatar answered Sep 25 '22 05:09

Nandkumar Tekale


use MultipartFile utility and try this

MultipartRequest multipartRequest = (MultipartRequest) request;
                Map map = multipartRequest.getFileMap();
                MultipartFile mfile = null;
                for (Iterator iter = map.values().iterator(); iter.hasNext();) {
                    mfile = (MultipartFile) iter.next();
                                String fileName = mfile.getOriginalFilename()
    }

Or you can try apache commons file upload.

Check this link : http://commons.apache.org/fileupload/using.html

like image 31
Renjith Avatar answered Sep 25 '22 05:09

Renjith