日期:2014-05-16  浏览次数:20436 次

Spring mvc extjs4文件上传

ExtJs4.0

?

js代码:

//文件上传与spring交互
Ext.onReady(function(){

    Ext.create('Ext.form.Panel', {
        title: 'File Uploader',
        width: 400,
        bodyPadding: 10,
        frame: true,
        renderTo: Ext.getBody(),   
        items: [{
            xtype: 'filefield',
            name: 'file',
            fieldLabel: 'File',
            labelWidth: 50,
            msgTarget: 'side',
            allowBlank: false,
            anchor: '100%',
            buttonText: 'Select a File...'
        }],

        buttons: [{
            text: 'Upload',
            handler: function() {
                var form = this.up('form').getForm();
                if(form.isValid()){
                    form.submit({
                        url: 'upload.htm',
                        waitMsg: 'Uploading your file...',
                        success: function(fp, o) {
                            Ext.Msg.alert('Success', o.result.mess);
                        }
                    });
                }
            }
        }]
    });

}); 

?

?

controller:

package com.kevin.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.kevin.model.ExtJSFormResult;
import com.kevin.model.FileUploadBean;
import com.kevin.util.GlobalService;
/**
 * 文件上传与下载Spring Mvc与ExtJs4
 * @author Administrator
 *
 */
@Controller
public class FileUploadController {
	
	@RequestMapping(value="/upload",method = RequestMethod.POST)
	public void upload1(FileUploadBean userVO,HttpServletResponse response) throws Exception{
		if(!userVO.getFile().isEmpty()){
			userVO.getFile().transferTo(new File("f:/upload/"+userVO.getFile().getOriginalFilename()));
		}
		//String json = "{success: true}";
		response.setContentType(GlobalService.CONTENT_TYPE);
		response.getWriter().write(GlobalService.UPLOAD_SUCCESS);
		response.flushBuffer();
	}
	   
		
}

?

?

GlobalService全局的变量类:

package com.kevin.util;
/**
 * 全局服务类
 * @author Administrator
 *
 */
public final class GlobalService {
	/** 
	 * @author:limingzhong 
	 * @desc:定义相关全局静态变量 
	 * @Date:2012-02-28 
	 */  
	public final static String UPLOAD_SUCCESS = "{success:true,mess:'文件上传成功!'}";  
	  
	public final static String UPLOAD_FAILURE = "{success:false,mess:'文件上传失败!'}";  
	  
	public final static String FILE_NO = "{success:false,mess:'文件不存在!'}";  
	  
	public final static String FILE_YES = "{success:true,mess:'文件存在!'}";  
	  
	public final static String CONTENT_TYPE = "text/html";  
	  
	public final static String APPLICATION = "application/octet-stream";
	
	public final static String PATH="F:\\upload";
}

?

FileUploadBean类,封闭file

?

package com.kevin.model;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * Represents file uploaded from extjs form
 * 
 * @author Loiane Groner
 * http://loiane.com
 * http://loianegroner.com
 */
public class FileUploadBean {

	private CommonsMultipartFile file;

	public CommonsM