3

我有基于 Spring MVC 的 REST 应用程序。正在尝试上传文件。这是代码片段。

webmvc-config.xml

<bean
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
    id="multipartResolver">
    <property name="maxUploadSize" value="2000000" />
</bean>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="0" />
    <property name="defaultContentType">
        <ref bean="htmlMediaType" />
    </property>
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
            <entry key="file" value="multipart/form-data"/>
        </map>
    </property>
    <property name="defaultViews">
        <list>
            <bean
                class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
            <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                <property name="marshaller">
                    <bean class="org.springframework.oxm.castor.CastorMarshaller" />
                </property>
            </bean>
        </list>
    </property>

文件上传控制器.java

@Controller
@RequestMapping("/appfiles")
public class FileUploadController {

private String saveDirectory = "C:/Downloads/"; 
@RequestMapping(value = "/appfiles", method = RequestMethod.POST, consumes="multipart/form-data", produces="application/json")
public ResponseEntity<List<Map<String, String>>> save(
        @RequestParam("file") MultipartFile file) {
    System.out.println("received file with original filename: " + file.getOriginalFilename());

    // List<MultipartFile> files = uploadForm.getFiles();
    List<Map<String, String>> response = new ArrayList<Map<String, String>>();
    Map<String, String> responseMap = new HashMap<String, String>();

    List<String> fileNames = new ArrayList<String>();

    if (null != file) {
        // for (MultipartFile multipartFile : files) {

        String fileName = file.getOriginalFilename();
        fileNames.add(fileName);

        try {
            file.transferTo(new File(saveDirectory
                    + file.getOriginalFilename()));
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    responseMap.put("displayText", file.getOriginalFilename());
    responseMap.put("fileSize", ""+file.getSize());
    response.add(responseMap);

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add("Accept", "application/json");
    return new ResponseEntity<List<Map<String, String>>>(response, httpHeaders, HttpStatus.OK);
}
}

当我使用 chrome 扩展“邮递员”时出现以下错误

Could not parse multipart servlet request; nested exception is     org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:163)
org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:139)
org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1020)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:883)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

postname 提交是通过以下详细信息完成的:

Headers
conent-type: multipart/form-data
Form-data:
file=<filename.txt>
Method=POST

你能告诉我我的代码有什么问题吗?

感谢和问候

4

1 回答 1

4

我也遇到了同样的问题,但是当我在没有任何 Header 值的情况下尝试它时它工作正常。

于 2013-12-04T03:14:24.290 回答