我已经使用
Spring MVC和java配置,我已经定义了一些休息服务.我使用Spring作为服务器后端,使用AngularJS作为WebFrontend.
我想从我的AngularJS网站上传一个或两个CSV文件到我的休息服务.我如何使用java配置配置spring以使其有效?我使用了Tomcat和一个Servlet 3容器.
我的上传休息服务如下所示:
@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."; } } }
我需要“StandardServletMultipartResolver”吗?或其他什么来上传工作?如果是这样,我如何使用java配置启用多部分上传?我的配置目前看起来像这样:
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}; } }
和WebConfig:
@Configuration @EnableWebMvc @ComponentScan(basePackages = { "controller" }) public class WebConfig extends WebMvcConfigurerAdapter { @Bean public MultipartResolver multipartResolver(){ return new StandardServletMultipartResolver(); }
}
和ApplicationConfig
@Configuration public class AppConfig { @Bean public Test getTest(){ return new Test(); } }
上传的示例来自此站点http://spring.io/guides/gs/uploading-files/,但他们使用的是MultiPartConfigFactory,这是来自spring boot;我能用这个吗?我目前没有使用弹簧启动..那么使用我的配置进行多部分上传的最简单方法是什么?
EDIT2:我已经为我的配置添加了两个东西上面新的getServletFilter现在看起来像这样,我添加了一个MultipartFilter():
@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,mpf}; }
在我的WebConfig中,我添加了一个StandardServletMultipartResolver:
@Bean public MultipartResolver multipartResolver(){ return new StandardServletMultipartResolver(); }
现在我想用一个简单的html网站测试它,如下所示:
<!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 – 必需的字符串参数’名称’不存在”.为什么名字不存在!?
如果您使用的是Servlet 3.0,则必须配置调度程序servlet以支持多部分.我遇到了与你完全相同的问题.我配置了多部分解析器bean,但我仍遇到问题.只需将以下行添加到应用程序初始化程序类(扩展WebApplicationInitializer的类):
原文链接:https://www.f2er.com/angularjs/141868.htmldispatcher.setMultipartConfig( new MultipartConfigElement("/tmp",25 * 1024 * 1024,125 * 1024 * 1024,1 * 1024 * 1024) );
dispatcher是ServletRegistration.Dynamic的一个实例.有关详细信息,请阅读我对问题here的回答.