I am trying to upload the MultiPart file in spring mvc rest services with the application secured with spring security.
The problem is I am able to upload the file succesfully but the uploaded file is corrupted(Not able to open that file),This happens only If I secured the application with spring security.
Here is the web.xml
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">Загрузка многочастного файл в Spring MVC, который защищен с пружинной безопасностью
<!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/appServlet/servlet-context.xml /WEB-INF/spring/appServlet/security-config.xml /WEB-INF/spring/appServlet/mail-config.xml </param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Spring Security filter <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> --> <filter> <filter-name>MultipartFilter</filter-name> <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class> </filter> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>MultipartFilter</filter-name> <servlet-name>/*</servlet-name> </filter-mapping> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> <dispatcher>ERROR</dispatcher> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping> </web-app>
Below is the code on the server side to accept the file
пакет com.upload.controller; Это пример кода, который я использую в Spring MVC контроллер для загрузки файлов на сервер теперь я использую в моей локальный хост
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.nio.file.Files; import java.nio.file.Paths; import javax.servlet.annotation.MultipartConfig; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.util.FileCopyUtils; import org.springframework.validation.BindingResult; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartResolver; //import com.journaldev.spring.controller.FileUploadController; @RestController @RequestMapping("/upload") // @MultipartConfig(maxFileSize=5242880,maxRequestSize=20971520,location="/home/java-root/Desktop/temp",fileSizeThreshold=0) public class FileUploadController { private static final Logger logger = LoggerFactory .getLogger(FileUploadController.class); // private static String UPLOAD_LOCATION = "/home/java-root/Desktop/temp/"; @RequestMapping(value = "/data", method = RequestMethod.POST) public @ResponseBody String multiFileUpload(@RequestParam("files") MultipartFile file, HttpServletRequest request) throws IOException { List<MultipartFile> files = Arrays.asList(file); List<String> fileNames = new ArrayList<String>(); String UPLOAD_LOCATION = "/home/java-root/Desktop/temp/"; if (null != files && files.size() > 0) { for (MultipartFile multipartFile : files) { String fileName = multipartFile.getOriginalFilename(); Files.copy( multipartFile.getInputStream(), Paths.get(UPLOAD_LOCATION, multipartFile.getOriginalFilename())); // Files.co fileNames.add(fileName); // Handle file content - multipartFile.getInputStream() } } return "successfull"; } }
Как выглядит ваша конфигурация безопасности? Каковы ваши журналы? Какую версию Spring Security вы используете? –
Это последние несколько строк журналов, которые я получил, когда я бегу в режиме отладки –