SpringBoot2.x文件上传

首先在static目录下写一个upload.html





Insert title here
 
	

    



   
文件: 姓名:

SpringBoot2.x文件上传_第1张图片
写一个FileController.java

package net.xdclass.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;


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

import net.xdclass.domain.JsonData;

@Controller // 可以引用resources下的文件内容
public class FileController {
	@RequestMapping(value = "/api/v1/gapage") // http://localhost:8080/api/v1/gapage页面访问这个地址
	public Object index() {
		return "index";
	}

	private static final String filepath = "E:/workspace/eclipseWordpace/xdclass.springbootdemo1/src/main/resources/static/images/"; //待上传图片的路径

	@RequestMapping(value = "upload") // 这里upload要和ipload.html的body一样
	@ResponseBody
	public JsonData upload(@RequestParam("head_img") MultipartFile file, HttpServletRequest request) { // request其他的表单字段
		String name = request.getParameter("name");
		System.out.println("用户名" + name);
		// 获取文件名
		String filename = file.getOriginalFilename();
		System.out.println("上传的文件名" + filename);

		// 获取文件的后缀名,因为文件名不能是文件的唯一标识,比如1.png 1.jpg
		String suffixName = filename.substring(filename.lastIndexOf("."));
		System.out.println("文件的后缀名" + suffixName);

		// 文件上传后的路径
		filename = UUID.randomUUID() + suffixName; // 拼回完整的文件名  randomUUID随机生成一个字符数
		System.out.println("转换后的名称" + filename);
		
		File dest = new File(filepath + filename);
		
		try {  //MultipartFile 对象的transferTo方法,用于文件保存(效率和操作比原先用FileOutStream方便和高效)
			file.transferTo(dest);
			return new JsonData(0, filename);  //返回路径
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}catch (IOException e) {
			e.printStackTrace();
		}
		return new JsonData(-1, "fail to save file",null);

	}

}

SpringBoot2.x文件上传_第2张图片
再写一个用于包装结果的类

package net.xdclass.domain;

import java.io.Serializable;

//用于包装结果
public class JsonData  implements Serializable {
	
	private static final long serialVersionUID = 1L;

	//状态码 0标识成功 -1表示失败
	private int code;
	
	//结果
	private Object data;
	
	//处理消息
	private String msg;

	private int getCode() {
		return code;
	}

	private void setCode(int code) {
		this.code = code;
	}

	private Object getData() {
		return data;
	}

	private void setData(Object data) {
		this.data = data;
	}

	private static long getSerialversionuid() {
		return serialVersionUID;
	}

	private String getMsg() {
		return msg;
	}

	private void setMsg(String msg) {
		this.msg = msg;
	}

	public JsonData(int code, Object data) {
		super();
		this.code = code;
		this.data = data;
	}

	public JsonData(int code, String msg,Object data) {
		super();
		this.code = code;
		this.msg = msg;
		this.data = data; //输入文件名之后返回的json也跟着改变
	}
	
	
	

}

SpringBoot2.x文件上传_第3张图片
访问:http://localhost:8080/upload.html
在这里插入图片描述
如果设置文件上传的大小,需要在启动类里面配置

package net.xdclass;

import javax.servlet.MultipartConfigElement;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;

@SpringBootApplication
public class XdclassApplication {

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

	@Bean  
    public MultipartConfigElement multipartConfigElement() {  
        MultipartConfigFactory factory = new MultipartConfigFactory();  
        //单个文件最大  
        factory.setMaxFileSize("10240KB"); //KB,MB  
        /// 设置总上传数据总大小  
        factory.setMaxRequestSize("1024000KB");  
        return factory.createMultipartConfig();  
    }  
}

你可能感兴趣的:(SpringBoot)