Ho bisogno di caricare il file dal browser al server. Io uso la molla 3.2 come struttura del mio sito web.Problemi di caricamento file Spring
Così ho configurato la mia app in questo modo.
1 - Ho web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>racoonsoft.chaos.settings</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>admin/library</welcome-file>
</welcome-file-list>
</web-app>
2 - classe MainConfig
@Configuration
@Import({WebConfig.class })
public class MainConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public static ScheduledAnnotationBeanPostProcessor scheduledAnnotationBeanPostProcessor() {
return new ScheduledAnnotationBeanPostProcessor();
}
@Bean
public static StandardServletMultipartResolver multipartResolver() {
StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
return resolver;
}
}
3 - Controller per gestire il caricamento più parti
@Controller
@MultipartConfig(fileSizeThreshold=1024*1024*2, // 2MB
maxFileSize=1024*1024*10, // 10MB
maxRequestSize=1024*1024*50)
public class FileUpload
{
public static final int UPLOAD_RESULT_OK = 100000;
@Autowired
BookDao book_dao;
@RequestMapping(value = "/admin/library/upload_file", method = RequestMethod.POST)
public String saveFiles(@RequestParam("file-file") MultipartFile file) throws IOException
{
if (!file.isEmpty())
{
byte[] bytes = file.getBytes();
return "redirect:caps/total_fail";
}
else
{
return "redirect:caps/total_fail";
}
}
}
4 - jsp dove ho messo la mia modulo per inviare file
...<form method="post" action="/admin/library/upload_file" enctype="multipart/form-data">
<input type="text" name="name"/>
<input type="file" name="file-file"/>
<input type="submit"/>
</form>...
Quando presento la mia forma ho ottenuto un'eccezione
org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file-file' is not present
org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:202)
Ho idea del perché. Quando rimuovo @RequestParam annotaion ho ottenuto il mio metodo invocato ma il parametro file = null. Qual è il mio problema?
Non sono sicuro di come lo configuro. Devo apportare modifiche al mio web.xml? Oppure devo creare il mio filtro estendendo MultipartFilter e mapparlo a /admin/library/upload_file ?? – user2160696
Vedere la risposta aggiornata per i dettagli di configurazione. – mthmulders
Purtroppo non ha aiutato. Ho aggiunto queste due parti, ma ho ancora il valore null come parametro. – user2160696