Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Sending POST parameters with RestTemplate requests

  1. #11
    Join Date
    Jul 2008
    Posts
    18

    Angry Sending Multipart File as POST parameters with RestTemplate requests

    Oakeye, thanks for your response again.

    That configuration is already on my application-context.xml.

    I have:

    Code:
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxUploadSize" value="${maxUploadSize}"/>
    </bean>
    Code:
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    		<constructor-arg ref="httpClientFactory" />
    		<property name="messageConverters">
    			<list>
    				<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
    				<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
    				<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
    			</list>
    		</property>
    </bean>
    But i still having no luck with the rest template. I will post an example of what a have in my project as soon as possible.

    Btw, what do you think that i missing?
    For the message of the exception that i getting:

    Code:
    Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.web.multipart.commons.CommonsMultipartFile]
    I think that i need an special converter. Something like an MultipartConverter...

  2. #12
    Join Date
    Nov 2009
    Posts
    15

    Default

    I do not know what is wrong with you.

    I will give you a demo later.

    I am so busy today.

  3. #13
    Join Date
    Jul 2008
    Posts
    18

    Red face Sending Multipart File as POST parameters with RestTemplate requests

    Hi Oakeye,

    I'm posting an (non working :P) example project with the configuration that i am using for post multifileparts through Rest Template.
    It would be nice if you can check what i am missing?

    To show the upload form, deploy the application and put this URL: http://localhost:8080/sites/postupload

    Thanks again!.
    Regards.
    Mauro.
    Attached Files Attached Files

  4. #14
    Join Date
    Nov 2009
    Posts
    15

    Default

    my applicationContext.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context-3.0.xsd
    		http://www.springframework.org/schema/aop
    		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    		http://www.springframework.org/schema/tx
    		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
    	<!-- 数据库配置,使用dbcp连接池 
    	<bean id="dataSource"
    		class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close">
    		<property name="driverClassName"
    			value="${jdbc.driverClassName}" />
    		<property name="url" value="${jdbc.url}" />
    		<property name="username" value="${jdbc.username}" />
    		<property name="password" value="${jdbc.password}" />
    	</bean>
    	
    	 事务管理器配置,单数据源事务 
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    		<property name="dataSource" ref="dataSource" />
    	</bean>
    
    	 使用annotation定义事务 
    	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
    	<context:property-placeholder location="/WEB-INF/jdbc.properties" />
    	<context:component-scan base-package="com.***.dao" />
    	-->
    	<context:component-scan base-package="com.***.service" />
    	
    	<!-- 文件上传 -->
    	<bean id="multipartResolver"  
            class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
            <property name="maxUploadSize">  
                <value>104857600</value>  
            </property>  
            <property name="maxInMemorySize">  
                <value>4096</value>  
            </property>      
    	</bean>  
    
    </beans>

  5. #15
    Join Date
    Nov 2009
    Posts
    15

    Default

    springmvc-servlet.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
    	
    	xsi:schemaLocation="
    		http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context-3.0.xsd
    		http://www.springframework.org/schema/mvc
    		http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    	<context:component-scan
    		base-package="com.***.controller" />
    		
    	<!-- Dispatches requests mapped to POJO @Controllers implementations -->
    	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
    		<property name="messageConverters">
    			<list>
    				<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
    				<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
    				<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
    				<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
    			</list>
    		</property>
    	</bean>
    	
    	<mvc:annotation-driven/>
    	
    </beans>

  6. #16
    Join Date
    Nov 2009
    Posts
    15

    Default

    controller

    Code:
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import javax.servlet.http.HttpServletResponse;
    
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import org.springframework.web.multipart.MultipartFile;
    
    @Controller
    @RequestMapping("/file")
    public class FileController {
    	
    	@RequestMapping(value = "/upload" , method = RequestMethod.POST)
    	@ResponseBody
    	public String upload(@RequestParam() MultipartFile file, HttpServletResponse response) throws Exception {
    		String path = FileController.class.getResource("/").getPath().split("WEB-INF")[0]
    				+ "upload/"; // 获取站点下的绝对磁盘路径
    		String fileName = file.getOriginalFilename().split("\\.")[0];
    //		String filePath = path + fileName + ".xls ";
    		String filePath = path + fileName;
    		System.out.println(path);
    		byte[] bytes;
    		FileOutputStream fos = null;
    		System.out.println("上传文件开始");
    		try {
    			bytes = file.getBytes();
    			fos = new FileOutputStream(filePath);
    			fos.write(bytes); 
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally{
    			fos.close();
    		}
    		JSONObject json = JsonMessage.loadSuccess();
    		
    		json.put("msg", fileName);
    		
    		return json.toString();
    	}
    }

  7. #17
    Join Date
    Nov 2009
    Posts
    15

    Default

    extjs
    Code:
    Ext.onReady(function(){
        var fp = new Ext.FormPanel({
        	title:'上传',
            renderTo: 'fi-form',
            fileUpload: true,
            width: 600,
            frame: true,
            autoHeight: true,
            bodyStyle: 'padding: 10px 10px 0 10px;',
            labelWidth: 50,
            defaults: {
                anchor: '95%',
                allowBlank: false,
                msgTarget: 'side'
            },
            items: [{
                xtype: 'fileuploadfield',
                id: 'file',
                emptyText: '请选择上传文件',
                fieldLabel: '文件',
                name: 'file',
                buttonText: '',
                buttonCfg: {
                    iconCls: 'upload-icon'
                }
            }],
            buttons: [{
                text: '上传',
                handler: function(){
                	var url = base+'file/upload';
                    if(fp.getForm().isValid()){
    	                fp.getForm().submit({
    	                    url:url,
    	                    waitMsg: '文件上传中...',
    	                    method:'POST',
    	                    success:function(form,action){
    	                    	var resp = Ext.util.JSON.decode(action.response.responseText);
    			                Ext.MessageBox.alert('警告','resp.msg');
    			            }
    	                });
                    }
                }
            },{
                text: 'Reset',
                handler: function(){
                    fp.getForm().reset();
                }
            }]
        });
    
    });

  8. #18
    Join Date
    Sep 2010
    Location
    Italy
    Posts
    1

    Default thanks

    thanks for information

  9. #19
    Join Date
    Jul 2008
    Posts
    18

    Smile Sending Multipart File as POST parameters with RestTemplate requests

    Hi guys,

    Well i think that were are talking about different thinks. It's easy and straightforward make an upload from a JSP page to a Spring Controller.-

    But what i trying to do is send Multipart files to a Rest Service using Rest Template.

    I mean the workflow, will be:

    1- Select the file on JSP and Submit.
    2- Spring Controller (App1) receives the File and make a Post For Object to a Rest Service with that file as parameter.
    3- Spring Controller - Rest Service (App2) receives the file through @RequestParam.
    4- The App2 controller saves the file on the filesystem.

    I was investigating an i think that its not an easy task to do due to Spring does not ship a converter for MultipartFiles.

    We have to write our own converter for this porpouse.

    BTW, Thank for your responses.
    M.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •