mui+spring boot 文件上传

阅读更多

1:首先设置文件上传的配置信息

package com.yw.appi;

import com.yw.appi.interceptor.APPInterceptor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.boot.context.embedded.MultipartConfigFactory;

import javax.servlet.MultipartConfigElement;

/**
 * Created by xiaoji on 2015/3/17.
 */
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    /**
     * 配置拦截器
     * @param registry
     */
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new APPInterceptor()).addPathPatterns("/userControl/**","/orderControl/**","/payControl/**","/consigneeControl/**");
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("12800KB");
        factory.setMaxRequestSize("12800KB");
        return factory.createMultipartConfig();
    }
}

 

2:创建上传后台controller

package com.yw.appi.controller;

import com.yw.appi.util.SYS;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by xiaoji on 2015/9/24.
 * 图片上传DEMO
 */
@Controller
@RequestMapping(value="/fileUploaderControl")
public class FileUploaderController {
    /**
     * 上传DEMO
     */
    @RequestMapping(value="/upload", method = {RequestMethod.POST})
    public void images(ModelMap modelMap,HttpServletRequest request,
                       @RequestParam("file") MultipartFile file){

        String path = "D:\\upload\\"+file.getOriginalFilename();
        File targetFile = new File(path);
        try {
            file.transferTo(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        modelMap.addAllAttributes(SYS.SUCCESS.Msg);
    }

}

 

3:创建一个h5+demo,更改服务器地址为http://192.168.1.35:8080/fileUploaderControl/upload,ip自己设置

,更改文件的key为后台对应的参数file,这里我只测试了单个文件上传,多个自由发挥,文档说的多个key不能相同,这个没测试过,有需要的自己测试。

代码如下:



	
		
		
		
		
		Hello H5+
		
		
		
	
	
		
Uploader

上传文件列表:

    无上传文件

拍照
相册选取

上 传

Uploader用于管理文件的上传,用于从本地上传各种文件到网络服务器,支持跨域访问操作。

 

 

 

你可能感兴趣的:(spring,mui)