springboot+jquery实现文件异步上传——浅谈SOA

关于springBoot就不做介绍了,个让你觉得是个不错的框架,要学习或者了解springBoot,应该对spring的一些基本配置有一定的了解,不要一蹴而就。

这次的博文主要是介绍 springboot+jquery实现文件异步上传,分一下几点介绍:

第一、springBoot的配置文件的配置:

	
## 数据源配置
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

## Mybatis 配置
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml

#启用shutdown
endpoints.shutdown.enabled=true
#禁用密码验证
endpoints.shutdown.sensitive=false

#开启shutdown的安全验证
endpoints.shutdown.sensitive=true

#验证用户名
security.user.name=admin

#验证密码
security.user.password=admin
#角色
management.security.role=SUPERUSER

#指定shutdown endpoint的路径
#endpoints.shutdown.path=/stop
#也可以统一指定所有endpoints的路径`management.context-path=/manage`
#指定管理端口和IP
server.port=8081
management.port=8081
management.address=127.0.0.1

#忽略权限拦截
management.security.enabled=false

 
  
第二、构建的是maven工程,pom.xml文件如下:
com.oracle

  4.0.0
  com.springboot
  myspringboot
  0.0.1-SNAPSHOT
  
    
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.1.RELEASE
    

    
        1.2.0
        5.1.39
    

    

        
		
			org.apache.tomcat.embed
			tomcat-embed-jasper
			required
		


        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            ${mybatis-spring-boot}
        

         
		 
		    org.springframework.boot
		    spring-boot-starter-actuator
		

        
        
            mysql
            mysql-connector-java
            ${mysql-connector}
        

        
        
            junit
            junit
            4.12
            test
        
        
		
		
			com.oracle
			ojdbc6
			11.2.0
		
        
	    
		
			com.jolbox
			bonecp-spring
			0.8.0.RELEASE
		
    



 注:Oracle的一些依赖Apache的中央仓库可能没有,自己想办法弄到自己的我本地仓库,这里不错介绍,自己Google或百度。 
  


第三、spring的Controller:

package com.springboot.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class DemoController {

	@RequestMapping(value="to_login",method = RequestMethod.GET)
	@ResponseBody
	public Map select(){
		Map map = new HashMap();
		map.put("status", "ok");
		return map;
	} 
	
    /**
     * 实现文件上传
     * */
    @RequestMapping(value="fileUpload",method = RequestMethod.POST)
    @ResponseBody 
    public String fileUpload(MultipartFile file){
    	
        if(file.isEmpty()){
            return "false";
        }
        String fileName = file.getOriginalFilename();
        
        String path = System.getProperty("user.dir") + "/uploadFile" ;
        File dest = new File(path + "/" + fileName);
        if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            file.transferTo(dest); //保存文件
            return "true";
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        }
    }
}

第四、springBoot的main函数入口:

package com.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//Spring Boot 应用的标识
@SpringBootApplication
//mapper 接口类扫描包配置
public class Application {

 public static void main(String[] args) {
     // 程序启动入口
     // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
     SpringApplication.run(Application.class,args);
 }
}
注意:这里端口有改变,不是8080端口,在配置文件Application.properties有做修改,不了解的可以百度或者Google。


第五、jsp的代码,通过jquery的异步实现:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




file upload

<%-- 
 --%>




结束语:springBoot主要的目的是SOA化,然而SOA概念提出,个人觉得就是编程思想中一个很古老的思想:解耦合。这里的话是通过文件异步上传来做一个简单的Demo,因为文件的异步上传,可以做到跨接口上传文件,这里的跨接口目的是解耦。就是,个干各地,看似不相关,其实是可以让他们相关。或许可以用很官方的说法来解释:万事万物是联系的、统一的。看来马克思还是很伟大的。





你可能感兴趣的:(springboot+jquery实现文件异步上传——浅谈SOA)