2014-02-18 66 views
0

我有要求從文件系統獲取圖像並在瀏覽器中顯示它的要求。我曾試圖到現在的代碼如下所示:上傳圖像並在瀏覽器中彈出webflow

的.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包中的類,所以你可能需要添加公地io.jar classpath中。 如果你的項目Maven的,你可以添加在POM以下依賴性:

 <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-io</artifactId> 
     <version>a valid version</version> <!-- 2.4 worked for me --> 
    </dependency>