2016-03-30 17 views
0

Spring Security、Spring MVCアプリケーションでファイルアップロード機能を使用しています。ここでSpring RequestMethod "POST"はファイルアップロード中にサポートされていません

は私のJSPページです:

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST) 
    public @ResponseBody 
    String uploadFileHandler(@RequestParam("name") String name, 
      @RequestParam("file") MultipartFile file) { 

     if (!file.isEmpty()) { 
      try { 
       byte[] bytes = file.getBytes(); 

       // Creating the directory to store file 
       String rootPath = System.getProperty("catalina.home"); 
       File dir = new File(rootPath + File.separator + "tmpFiles"); 
       if (!dir.exists()) 
        dir.mkdirs(); 

       // Create the file on server 
       File serverFile = new File(dir.getAbsolutePath() 
         + File.separator + name); 
       BufferedOutputStream stream = new BufferedOutputStream(
         new FileOutputStream(serverFile)); 
       stream.write(bytes); 
       stream.close(); 

       System.out.println("Server File Location=" 
         + serverFile.getAbsolutePath()); 

       return "You successfully uploaded file=" + name; 
      } catch (Exception e) { 
       return "You failed to upload " + name + " => " + e.getMessage(); 
      } 
     } else { 
      return "You failed to upload " + name 
        + " because the file was empty."; 
     } 
    } 

以下に示すようにMultipartResolverビーンが宣言されています:

@Bean(name = "multipartResolver") 
public CommonsMultipartResolver createMultipartResolver() { 
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(); 
    //resolver.setDefaultEncoding("utf-8"); 
    resolver.setMaxUploadSize(1000000); 
    resolver.setMaxInMemorySize(1000000); 
    return resolver; 
} 

そして、私の春のセキュリティここで

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ page session="false" %> 
<html> 
<head> 
<title>Upload File Request Page</title> 
</head> 
<body> 

    <form method="POST" action="uploadFile" enctype="multipart/form-data"> 
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" /> 
     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> 

は私のコントローラメソッドでありますHttpSecurity設定:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http.authorizeRequests().antMatchers("/css/**").permitAll() 
      .antMatchers("/testDecorator").permitAll() 
      .antMatchers("/uploadFile").permitAll() 
      .antMatchers("/home") 
      .authenticated().and().formLogin().loginPage("/login") 
      .failureUrl("/login?error") 
      .successHandler(customSuccessHandler) 
      .usernameParameter("username").passwordParameter("password") 
      .and().logout().logoutSuccessUrl("/login?logout").and().csrf() 
      .and().exceptionHandling().accessDeniedPage("/Access_Denied"); 


} 

ファイルを選択してフォームを送信したとき。以下のエラーが表示されます:

org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported 
WARNING: Request method 'POST' not supported 

私は別のプロジェクトで同じコードを使用しています。それは正常に動作しています。私はどこに間違っていたのか分からない。 誰かが私をガイドして問題の原因となりますか?

よろしく、
Manju Raghavendra。

+0

=「$ {pageContext.request.contextPath}/uploadFile」 –

+0

@Raghavendraあなたは確認する必要があります:それはこのようになります。このフォームが投稿されるURL。サポートされていないリクエストメソッドは警告のみです。 –

+0

いいえ@levgen Pianovフォームを送信すると正しいURL「http:// localhost:8080/spring-security/uploadFile」にリダイレクトされます – Raghavendra

答えて

0

設定ファイルにmultipartResolverを設定しましたか? 、アクションを使用しようと、私はあなたがあなたのjspページにURLを記述に問題があると思い

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <property name="maxUploadSize" value="78643200"/> 
    <property name="maxInMemorySize" value="78643200"/> 
</bean> 
+0

はい。私はそれを設定しました。 – Raghavendra

関連する問題