42

Tomcat にデプロイされた Spring 3 MVC アプリケーションで、アップロードした画像を特定のフォルダーに保存したい

私の問題は、アップロードされた画像ファイルを、アプリケーションが実行されているホストに保存できないことです。

これが私が試したものです:

private void saveFile(MultipartFile multipartFile, int id) throws Exception {
    String destination = "/images/" + id + "/"  + multipartFile.getOriginalFilename();
    File file = new File(destination);
    multipartFile.transferTo(file);
}

結果: FileNotFoundException - はい、このファイルを作成します!?!

context.getRealPathまたはを使用して試しましたgetResources("destination")が、成功しませんでした。

マルチパート ファイルのコンテンツを使用して、アプリの特定のフォルダーに新しいファイルを作成するにはどうすればよいですか?

4

6 に答える 6

1

xml 構成を使用した spring 3 の例を見ました (これは spring 4.2.* では機能しないことに注意してください): http://www.devmanuals.com/tutorials/java/spring/spring3/mvc/spring3-mvc-upload-file. html `

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="100000" />
<property name="uploadTempDir" ref="uploadDirResource" />
</bean>

<bean id="uploadDirResource" class="org.springframework.core.io.FileSystemResource">
<constructor-arg>
<value>C:/test111</value>
</constructor-arg>
</bean>

于 2016-01-26T23:20:22.150 に答える
0
String ApplicationPath = 
        ContextLoader.getCurrentWebApplicationContext().getServletContext().getRealPath("");

これは、Springでアプリの実際のパスを取得する方法です(応答、セッションを使用せずに...)

于 2016-12-13T05:20:54.790 に答える