Spring: Upload pliku w Spring Web Flow

xhtml:

1
2
3
4
<h:form enctype="multipart/form-data">
   <input type="file" name="file" />
   <h:commandButton action="upload" value="Ok" />
</h:form>

beans.xml:

1
2
3
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="100000"/>
</bean>

flow.xml:

1
2
3
4
5
6
7
8
9
10
11
<var name="uploadBean" class="beans.UploadBean"/>
<view-state id="upload">
	<transition on="upload" to="upload-action"/>
</view-state>
<action-state id="upload-action">
	<evaluate expression="uploadBean.upload(flowRequestContext)"/>
	<transition on="success" to="upload">
		<!-- do something with uploadBean.getFile() -->
	</transition>
	<transition on="error" to="upload-action"/>
</action-state>

UploadBean.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package beans;
 
import java.io.Serializable;
import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.webflow.execution.RequestContext;
 
public class UploadBean implements Serializable {
 
	private static final long serialVersionUID = 1L;
	private transient final Logger log = Logger.getLogger(getClass());
	private byte[] file;
 
	public byte[] getFile() {
		return file;
	}
 
	public String upload(RequestContext context) {
		try {
			MultipartFile file = context.getRequestParameters().getRequiredMultipartFile("file");
			log.debug("UPLOADED: "+file.getOriginalFilename());
			this.file = file.getBytes();
			return "success";
 
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
			return "error";
		}
	}
}

2 thoughts on “Spring: Upload pliku w Spring Web Flow

  • JSF 1.2 – prz lini:

    MultipartFile file = context.getRequestParameters().getRequiredMultipartFile(“file”);

    dostaje błąd:

    Map key ‘file’ has value [Gif-Exercise.gif] that is not of expected type [interface org.springframework.web.multipart.MultipartFile], instead it is of type [java.lang.String]

    Czy miałeś może coś takiego?

Leave a Reply