Announcement Announcement Module
Collapse
No announcement yet.
Sending POST parameters with RestTemplate requests Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    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();
    	}
    }

    Comment


    • #17
      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();
                  }
              }]
          });
      
      });

      Comment


      • #18
        thanks

        thanks for information

        Comment


        • #19
          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.

          Comment

          Working...
          X