使用AngularJS(Java配置)上传SpringREST多部分文件?

使用AngularJS(Java配置)上传SpringREST多部分文件?,spring,angularjs,rest,upload,multipartform-data,Spring,Angularjs,Rest,Upload,Multipartform Data,我将SpringMVC与java配置一起使用,并定义了一些rest服务。我使用Spring作为服务器后端,AngularJS作为WebFrontend 我想从我的AngularJS站点上传一两个CSV文件到我的rest服务。我必须如何用java配置配置spring才能工作?我使用了Tomcat和Servlet3容器 我的上载rest服务如下所示: @Controller @RequestMapping("Upload") @MultipartConfig(fileSizeThreshold=10

我将SpringMVC与java配置一起使用,并定义了一些rest服务。我使用Spring作为服务器后端,AngularJS作为WebFrontend

我想从我的AngularJS站点上传一两个CSV文件到我的rest服务。我必须如何用java配置配置spring才能工作?我使用了Tomcat和Servlet3容器

我的上载rest服务如下所示:

@Controller
@RequestMapping("Upload")
@MultipartConfig(fileSizeThreshold=1024*1024*10,    // 10 MB
maxFileSize=1024*1024*50,          // 50 MB
maxRequestSize=1024*1024*100)      // 100 MB
public class UploadController {

    @RequestMapping(value="/upload", method=RequestMethod.GET)
    public @ResponseBody String provideUploadInfo() {
        return "You can upload a file by posting to this same URL.";
    }

    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload(@RequestParam("name") String name, 
            @RequestParam("file") MultipartFile file){
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream = 
                        new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + " into " + name + "-uploaded !";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
    }

}
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{ PersistenceContext.class,AppConfig.class,SecurityConfig.class }; 
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/Spring/*" }; 
    }

    @Override
    protected Filter[] getServletFilters() {

        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");

        CorsFilter cf=new CorsFilter();
            MultipartFilter mpf=new MultipartFilter(); //MultipartFilter support
        return new Filter[] {characterEncodingFilter,cf,mpf};

    }




}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form method="POST" enctype="multipart/form-data"
        action="http://localhost:8081/project/Spring/Upload/upload">
        File to upload: <input type="file" name="file"><br /> Name: <input
            type="text" name="name"><br /> <br /> <input type="submit"
            value="Upload"> Press here to upload the file!
    </form>

</body>
</html>
我需要“StandardServletMultipartResolver”吗?或者其他什么让上传工作?如果是这样,我如何使用java配置启用多部分上传?我目前的配置如下所示:

@Controller
@RequestMapping("Upload")
@MultipartConfig(fileSizeThreshold=1024*1024*10,    // 10 MB
maxFileSize=1024*1024*50,          // 50 MB
maxRequestSize=1024*1024*100)      // 100 MB
public class UploadController {

    @RequestMapping(value="/upload", method=RequestMethod.GET)
    public @ResponseBody String provideUploadInfo() {
        return "You can upload a file by posting to this same URL.";
    }

    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload(@RequestParam("name") String name, 
            @RequestParam("file") MultipartFile file){
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream = 
                        new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + " into " + name + "-uploaded !";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
    }

}
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{ PersistenceContext.class,AppConfig.class,SecurityConfig.class }; 
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/Spring/*" }; 
    }

    @Override
    protected Filter[] getServletFilters() {

        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");

        CorsFilter cf=new CorsFilter();
            MultipartFilter mpf=new MultipartFilter(); //MultipartFilter support
        return new Filter[] {characterEncodingFilter,cf,mpf};

    }




}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form method="POST" enctype="multipart/form-data"
        action="http://localhost:8081/project/Spring/Upload/upload">
        File to upload: <input type="file" name="file"><br /> Name: <input
            type="text" name="name"><br /> <br /> <input type="submit"
            value="Upload"> Press here to upload the file!
    </form>

</body>
</html>
}

和应用程序配置

@Configuration
public class AppConfig {

    @Bean
    public Test getTest(){


        return new Test();
    }
}
上传的例子来自这个站点,但他们使用了MultiPartConfigFactory,这是从SpringBoot;我能用这个来做手术吗?我当时没有用弹簧靴。。那么,使用我的配置实现多部分上传的最简单方法是什么呢

EDIT2:我在配置中添加了两个新的getServletFilter,现在看起来像这样,我添加了一个MultipartFilter():

在我的网络配置中,我添加了一个标准ServletMultipartResolver:

@Bean
public MultipartResolver multipartResolver(){

        return new StandardServletMultipartResolver();
    }
现在我想用一个简单的html站点来测试它,如下所示:

@Controller
@RequestMapping("Upload")
@MultipartConfig(fileSizeThreshold=1024*1024*10,    // 10 MB
maxFileSize=1024*1024*50,          // 50 MB
maxRequestSize=1024*1024*100)      // 100 MB
public class UploadController {

    @RequestMapping(value="/upload", method=RequestMethod.GET)
    public @ResponseBody String provideUploadInfo() {
        return "You can upload a file by posting to this same URL.";
    }

    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public @ResponseBody String handleFileUpload(@RequestParam("name") String name, 
            @RequestParam("file") MultipartFile file){
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream = 
                        new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + " into " + name + "-uploaded !";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
    }

}
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{ PersistenceContext.class,AppConfig.class,SecurityConfig.class }; 
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/Spring/*" }; 
    }

    @Override
    protected Filter[] getServletFilters() {

        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");

        CorsFilter cf=new CorsFilter();
            MultipartFilter mpf=new MultipartFilter(); //MultipartFilter support
        return new Filter[] {characterEncodingFilter,cf,mpf};

    }




}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form method="POST" enctype="multipart/form-data"
        action="http://localhost:8081/project/Spring/Upload/upload">
        File to upload: <input type="file" name="file"><br /> Name: <input
            type="text" name="name"><br /> <br /> <input type="submit"
            value="Upload"> Press here to upload the file!
    </form>

</body>
</html>

在此处插入标题
要上载的文件:
名称:

按此处上载文件!

但它表示“HTTP状态400-所需的字符串参数'name'不存在”。为什么名字不存在

如果您使用的是Servlet 3.0,则必须将dispatcher Servlet配置为支持多部分。我遇到了和你完全相同的问题。我配置了多部分解析器bean,但仍然遇到问题。只需将以下行添加到应用程序初始值设定项类(扩展
WebApplicationInitializer
)即可:

dispatcher
ServletRegistration.Dynamic
的一个实例。有关更多细节,请阅读我对问题的回答

  • 我也遇到了同样的问题,结果证明这是由于StandardServletMultipartResolver造成的。将其更改为org.springframework.web.multipart.commons.commons多部分解析器

  • 这可能与本文中提出的问题无关,但可能会有所帮助。 启用调试后,我可以在日志中看到此调试消息:- 请求已经是MultipartTTpServletRequest-如果不在转发中,这通常是由web.xml中的额外MultipartFilter导致的

  • 通过从
    getServletFilters()
    中的过滤器数组中删除
    MultipartFilter
    来解决此问题。
    MultipartFilter mpf=new MultipartFilter()

    检查@但这对我的配置没有帮助,我已经更新了我的第一篇帖子!!并添加了一个新的MultipartFilter和一个StandardServletMultipartResolver,对吗?但我还是犯了一个错误。。见我的第一篇帖子中的Edit2,我有完全相同的问题。你能修好它吗?