10

ブラウザからサーバーにファイルをアップロードする必要があります。Web フレームワークとして spring 3.2 を使用しています。

だから私は自分のアプリをこのように設定しました。

1 - 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 - 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
@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

...<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>...

フォームを送信すると、例外が発生しました

org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file-file' is not present
    org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:202)

理由がわかりません。@RequestParam アノテーションを削除すると、メソッドが呼び出されましたが、ファイル パラメーター = null です。私の問題は何ですか?

4

4 に答える 4

1

@ user64141は正しいですが、xmlの代わりにJava構成を使用している場合は、試してください

@Bean
public MultipartResolver multipartResolver() {
    return new CommonsMultipartResolver();
}
于 2016-05-17T18:23:59.820 に答える
0

MultipartFilterまた、webapp 用に構成する必要があります。Javadoc によると、MultipartResolver を使用してマルチパート リクエストを解決します (ただし、既に構成されています)。ファイルのアップロードを処理するコントローラーの要求パス (の一部) にマップする必要があります。

まず、web.xml に を追加MultipartFilterします。

<filter>
    <filter-name>multipartFilter</filter-name>
    <filter-class>org.springframework.web.multipart.support.MultipartFilter</filter-class>
</filter>

次に、ファイルのアップロードを受け入れる必要がある URL (の一部) にフィルターをマップします。

<filter-mapping>
    <filter-name>multipartFilter</filter-name>
    <url-pattern>/admin/library/upload_file</url-pattern>
</filter-mapping>
于 2013-04-16T13:08:51.220 に答える