尝试用邮递员上传MultipartFile

尝试用邮递员上传MultipartFile

本文介绍了尝试用邮递员上传MultipartFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PostMan上传分段文件并出现错误.这是代码和屏幕截图:

I am trying to upload a Multipart File using PostMan and getting errors. Here is the code and screenshots:

http://imgur.com/pZ5cXrh

http://imgur.com/NaWQceO

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void uploadFileHandler(@RequestParam("name") String name,
        @RequestParam("name") MultipartFile file) {

    if (!file.isEmpty()) {
        try {
            byte[] bytes = file.getBytes();

            // Creating the directory to store file
            //String rootPath = System.getProperty("catalina.home");
            String rootPath = "C:\\Desktop\\uploads";
            File dir = new File(rootPath + File.separator + "tmpFiles");
            if (!dir.exists())
                dir.mkdirs();

            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath()
                    + File.separator + name);
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();

            System.out.println("Server File Location="
                    + serverFile.getAbsolutePath());

            System.out.println("You successfully uploaded file=" + name);
        } catch (Exception e) {
            System.out.println("You failed to upload " + name + " => " + e.getMessage());
        }
    } else {
        System.out.println("You failed to upload " + name
                + " because the file was empty.");
    }
}

推荐答案

您应该有这样的东西:

@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = "multipart/form-data")
    public void uploadFileHandler(@RequestParam("name") String name,
                                  @RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();

                // Creating the directory to store file
                //String rootPath = System.getProperty("catalina.home");
                String rootPath = "C:\\Users\\mworkman02\\Desktop\\uploads";
                File dir = new File(rootPath + File.separator + "tmpFiles");
                if (!dir.exists())
                    dir.mkdirs();

                // Create the file on server
                File serverFile = new File(dir.getAbsolutePath()
                        + File.separator + name);
                BufferedOutputStream stream = new BufferedOutputStream(
                        new FileOutputStream(serverFile));
                stream.write(bytes);
                stream.close();

                System.out.println("Server File Location="
                        + serverFile.getAbsolutePath());

                System.out.println("You successfully uploaded file=" + name);
            } catch (Exception e) {
                System.out.println("You failed to upload " + name + " => " + e.getMessage());
            }
        } else {
            System.out.println("You failed to upload " + name
                    + " because the file was empty.");
        }
    }

请注意consumes = "multipart/form-data".您上传的文件很有必要,因为您应该进行多部分通话.您应该使用@RequestParam("file") MultipartFile file而不是@RequestParam("name") MultipartFile file).

Please pay attention to consumes = "multipart/form-data". It is necessary for your uploaded file because you should have a multipart call. You should have @RequestParam("file") MultipartFile file instead of @RequestParam("name") MultipartFile file).

当然,您应该已经为multipartview解析器配置了对apache-commons文件上载和本机servlet 3的内置支持.

Of course you should have configured a multipartview resolver the built-in support for apache-commons file upload and native servlet 3.

这篇关于尝试用邮递员上传MultipartFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 20:01