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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.springboot</groupId>
  <artifactId>myspringboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
    <!-- Spring Boot 启动父依赖 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <properties>
        <mybatis-spring-boot>1.2.0</mybatis-spring-boot>
        <mysql-connector>5.1.39</mysql-connector>
    </properties>

    <dependencies>

        <!-- 本地启动tomcat -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>required</scope>
		</dependency>


        <!-- Spring Boot Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Boot Test 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Spring Boot Mybatis 依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>

         <!-- spring Boot 安全停止 -->
		 <dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

        <!-- MySQL 连接驱动依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector}</version>
        </dependency>

        <!-- Junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        
		<!-- 数据库 -->
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc6</artifactId>
			<version>11.2.0</version>
		</dependency>
        
	    <!-- 连接池 -->
		<dependency>
			<groupId>com.jolbox</groupId>
			<artifactId>bonecp-spring</artifactId>
			<version>0.8.0.RELEASE</version>
		</dependency>
    </dependencies>
</project>


 注: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<String, Object> select(){
		Map<String, Object> map = new HashMap<String, Object>();
		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"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>file upload</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/jquery-1.12.4.min.js"></script>
<%-- <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/jquery.ajaxfileupload.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/ajaxfileupload.js"></script> --%>
</head>
<body>

<div id="uploadForm">
    <input id="file" type="file" name="file"/>
    <button id="upload" type="button" onclick="fileUpload()">upload</button>
</div>



<script type="text/javascript">

	function fileUpload(){
		
		var formData = new FormData();
		formData.append('file', $('#file')[0].files[0]);
		$.ajax({
		    url: 'http://localhost:8083/fileUpload',
		    type: 'POST',
		    cache: false,
		    data: formData,
		    processData: false,
		    contentType: false
		}).done(function(res) {
		}).fail(function(res) {});
	}

</script>
</body>
</html>

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





猜你喜欢

转载自blog.csdn.net/helloworldyangsong/article/details/78448931