Announcement Announcement Module
Collapse
No announcement yet.
How to upload to Spring from Flash using AS3 FileReference. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to upload to Spring from Flash using AS3 FileReference.

    Hello, I am trying to make a simple spring setup that will allow me to upload a file from Flash using ActionScript 3's FileReference class which sends the file in a format like this:

    Code:
    POST /handler.cfm HTTP/1.1 
      Accept: text/*
      Content-Type: multipart/form-data; 
      boundary=----------Ij5ae0ae0KM7GI3KM7 
      User-Agent: Shockwave Flash 
      Host: www.example.com 
      Content-Length: 421 
      Connection: Keep-Alive 
      Cache-Control: no-cache
      
      ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7
      Content-Disposition: form-data; name="Filename"
      
      MyFile.jpg
      ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7
      Content-Disposition: form-data; name="Filedata"; filename="MyFile.jpg"
      Content-Type: application/octet-stream
      
      FileDataHere
      ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7
      Content-Disposition: form-data; name="Upload"
      
      Submit Query
      ------------Ij5GI3GI3ei4GI3ei4KM7GI3KM7KM7--
    My application-servlet looks like this:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/flex 
    		http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">
    
    	<bean id="urlMapping"
    		class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    		<property name="alwaysUseFullPath" value="true" />
    		<property name="mappings">
    			<props>
    				<prop key="/upload/*">fileUploadController</prop>
    				<prop key="/spring/messagebroker/*">_messageBroker</prop>
    			</props>
    		</property>
    	</bean>
    
    	<bean id="multipartResolver"
    		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    		<property name="maxUploadSize" value="1000000000" />
    		<property name="uploadTempDir" ref="uploadDirResource" />
    	</bean>
    
    	<bean id="uploadDirResource" class="org.springframework.core.io.FileSystemResource">
    		<constructor-arg>
    			<value>/temp</value>
    		</constructor-arg>
    	</bean>
    
    
    	<bean id="fileUploadController" class="fileUtils.FileUploadAndSaveController">
    		<property name="commandClass" value="fileUtils.FileUploadBean" />
    		<property name="successView" value="confirmation" />
    	</bean>
    
    	<bean id="uploadHandlerAdapter"
    		class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
    
    	<bean id="confirmation" class="org.springframework.web.servlet.view.JstlView">
    		<property name="url">
    			<value>/WEB-INF/jsp/confirmation.jsp</value>
    		</property>
    	</bean>
    	
    
    	<flex:message-broker>
    		<flex:message-service default-channels="my-secure-amf, my-amf, my-polling-amf" />
    		<flex:secured per-client-authentication="true" />
    	</flex:message-broker>
    
    	<!-- Expose the productService bean for BlazeDS remoting -->
    	<flex:remoting-destination ref="myService"
    		channels="my-secure-amf" />
    
    </beans>
    Now request to www.mydomain/upload/ get's routed correctly to fileUploadController, which looks like:

    Code:
    package fileUtils;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.validation.BindException;
    import org.springframework.web.multipart.MultipartFile;
    import org.springframework.web.multipart.MultipartHttpServletRequest;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.SimpleFormController;
    
    public class FileUploadAndSaveController extends SimpleFormController {
    	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws ServletException, IOException {
    		
    		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    		
    		MultipartFile filea = multipartRequest.getFile("upload");
    		
    		InputStream inputStream = null;
    		OutputStream outputStream = null;
    		if (filea.getSize() > 0) {
    
    
    		inputStream = filea.getInputStream();
    		outputStream = new FileOutputStream("C:/temp/"+filea.getOriginalFilename());
    		System.out.println("====22=========");
    		System.out.println(filea.getOriginalFilename());
    		System.out.println("=============");
    		int readBytes = 0;
    		byte[] buffer = new byte[8192];
    		while ((readBytes = inputStream.read(buffer, 0 , 8192))!=-1){
    		System.out.println("===ddd=======");
    		outputStream.write(buffer, 0, readBytes);
    		}
    		outputStream.close();
    		inputStream.close();
    		}
    
    		return new ModelAndView("successView");
    		
    	}
    }
    And fileuploadbean.java:

    Code:
    package fileUtils;
    
    public class FileUploadBean {
    	private byte[] file;
    	
    	public void setFile(byte[] file) {
    		this.file = file;
    	}
    	
    	public byte[] getFile() {
    		return file;
    	}
    }
    But I get this error:

    Code:
    javax.servlet.ServletException: Circular view path []: would dispatch back to the current handler URL [/LoginDemo/upload/] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
    	org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:275)
    	org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:212)
    	org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:257)
    	org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1183)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:902)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    	org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    	org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    	org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    	org.springframework.security.ui.SessionFixationProtectionFilter.doFilterHttp(SessionFixationProtectionFilter.java:67)
    	org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    	org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    	org.springframework.security.ui.ExceptionTranslationFilter.doFilterHttp(ExceptionTranslationFilter.java:101)
    	org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    	org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    	org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter.doFilterHttp(SecurityContextHolderAwareRequestFilter.java:91)
    	org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    	org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    	org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)
    	org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
    	org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    	org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:83)
    	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    	org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
    	org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)
    	org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:236)
    	org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    Is there a basic example somewhere of upload from flash to a spring bases uploadhandler?
Working...
X