Tôi cần tải lên tệp từ trình duyệt đến máy chủ. Tôi sử dụng mùa xuân 3.2 làm khung web của tôi.Sự cố tải lên tệp mùa xuân
Vì vậy, tôi đã định cấu hình ứng dụng của mình như thế này.
1 - Tôi đã nhận 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 - lớp 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 - Bộ điều khiển để xử lý cập nhật nhiều phần dữ liệu
@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 nơi mà tôi đặt của tôi biểu mẫu để gửi tệp
...<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>...
Khi tôi nộp mẫu đơn của tôi có một ngoại lệ
org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file-file' is not present
org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:202)
tôi không biết tại sao. Khi tôi loại bỏ chú thích @RequestParam, tôi đã nhận được phương thức được gọi nhưng tham số tệp = null. vấn đề của tôi là gì?
Tôi không chắc cách cấu hình. Tôi có cần thực hiện các thay đổi đối với tệp web.xml của mình không? Hoặc tôi phải tạo bộ lọc MultipartFilter mở rộng của riêng mình và ánh xạ nó tới /admin/library/upload_file ?? – user2160696
Xem câu trả lời được cập nhật để biết chi tiết cấu hình. – mthmulders
Đáng buồn thay, nó không giúp ích gì. Tôi đã thêm hai phần đó, nhưng vẫn có tham số null. – user2160696