12

我有一个jsp带有此代码段的内容。

<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>

这是我的控制器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");
}

我无法检索文件的名称,它显示null. 我知道我可以使用 JQuery 或 javascript 检索名称,但我不想同时使用它们。我想用纯java来做。谁能帮帮我吗?

4

5 回答 5

18

当您上传文件时,requestorg.springframework.web.multipart.MultipartHttpServletRequest. 因此,您可以将其转换为您的方法convertFile()。见下文 :

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
}

但是我无法检索(接收空值)我在 JSP 页面中选择的文件的名称。

---> 要获取文件名,您可以将其获取为:

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

为了使文件上传工作,您需要确保您正在创建多部分解析器 bean,如下所示:

<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>

参考:Spring 文档

于 2012-12-19T06:54:29.980 回答
3

使用 MultipartFile 实用程序并尝试这个

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

或者您可以尝试 apache commons 文件上传。

检查此链接:http ://commons.apache.org/fileupload/using.html

于 2012-12-19T06:53:08.783 回答
2

无法直接检索文件名。您可以使用 Apache Commons Fileupload API -

// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();

    if (item.isFormField()) {
       // Process a normal field
       String name = item.getFieldName();
       String value = item.getString();

    } else {
        // Process a file upload field 
    String fileName = item.getName();
    // DO further processing

    }
}

更多细节 -

http://commons.apache.org/fileupload/using.html

它也可以只用 Java 来完成,但显然需要更多的代码。

于 2012-12-19T06:53:44.313 回答
1

但是,首先,您将需要Commons Fileupload API,它将帮助您使用file.getFieldName()显示表单字段名称file.getContentType()显示文件类型file.getName()显示文件名

public String convertFile(HttpServletRequest request, HttpSession session) {
  boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if(!isMultipart) {
            out.println("File Not Uploaded");
    }
    else{
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List items = null;
        try {
            items = upload.parseRequest(request);
          } catch (FileUploadException e) {
            e.printStackTrace();
        }
        try {
            FileItem file = (FileItem) items.get(0);
            out.print("Field Name :"+file.getFieldName()); // Display the field name
            out.print("Content Type :"+file.getContentType()); // Display Type of File
            out.print("File Name :"+file.getName()); // Display File Name
        } catch (Exception e) {
            out.print(e);
        }
    }
 }
于 2012-12-19T06:58:12.250 回答
0
Iterator iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();

    if (item.isFormField()) {
       // Process a normal field
       String name = item.getFieldName();
       String value = item.getString();

    } else {
        // Process a file upload field 
    String fileName = item.getName();
    // DO further processing

    }
}
于 2017-07-13T11:53:37.213 回答