我要求从文件系统获取图像并在浏览器中显示它。我迄今为止尝试过的代码如下所示:
.jsp代码:
<formspring:form enctype="multipart/form-data" modelAttribute="onlinePurchase">
Please select a file to upload : <input type="file" name="multipartFileUpload" id="multipartFileUpload" />
<div class="submit">
<input type="submit" id="upload" name="_eventId_upload"
value="Upload Image" />
</div>
</formspring:form>
控制器代码:
public void saveImage(OnlinePurchase onlinepurchase,
MultipartFile multipartFile) {
try {
onlinepurchase.setContent(multipartFile.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
onlinepurchase.setContentSize(new Long(multipartFile.getSize())
.intValue());
onlinepurchase.setContentType(multipartFile.getContentType());
onlinepurchase.setFilename(multipartFile.getOriginalFilename());
}
flow.xml代码:
<view-state id="onlinePurchaseStep1" view="onlinepurchase/onlinepurchaserequest-step-1"
model="onlinePurchase">
<on-render>
<evaluate expression="oPAction.initialize" />
</on-render>
<transition on="submit" to="onlinePurchaseStep1" validate="false">
<evaluate expression="oPAction.cropImage" />
</transition>
<transition on="upload" to="onlinePurchaseStep1" validate="false">
<evaluate
expression="oPAction.saveImage(onlinePurchase,requestParameters.multipartFileUpload)"
result="" result-type="" />
</transition>
<transition on="success" to="onlinePurchaseStep2">
<evaluate expression="oPAction.addMedia" />
</transition>
<transition on="cancel" to="login" validate="false" />
</view-state>
问题是没有调用saveImage(OnlinePurchase onlinepurchase,String multipartFile)方法。 任何建议都表示赞赏。
答案 0 :(得分:0)
你需要一个MultipartResolver: 在spring的webmvc xml配置文件中添加此行。
<bean id ="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
请注意,CommonsMultipartResolver使用org.apache.commons.io包中的类,因此您可能需要在类路径中添加commons-io.jar。 如果你的项目是maven,你可以在pom中添加以下依赖项:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>a valid version</version> <!-- 2.4 worked for me -->
</dependency>