Spring文件上传三种形式演示v1.0

Spring上传文件

工作中用的最多的就是上传,目前上传大概有三种形式:单文件,多文件,Base64三种形态,spring借助于commons-fileupload来实现以上三种形态,是非常简单的一件事情。

添加依赖



commons-fileupload
commons-fileupload
1.4

Spring是通过MultipartFile file 来接收文件,通过MultipartFile[] files接收多个文件

实操

编写静态页面

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




Insert title here


上传Deme





单文件上传:






批量上传Demo



action="/uploads/upload2">





文件2:









Base64文件上传





BASE64编码:





增加展示视图

优雅的显示页面

 public void addViewControllers(ViewControllerRegistry registry) {
// TODO Auto-generated method stub
registry.addViewController("/upload").setViewName("upload");
WebMvcConfigurer.super.addViewControllers(registry);
}

修改默认上传限制

 @Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(10000000);
return multipartResolver;
}

编写控制器

package com.haojishu.demo.web;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.util.FileCopyUtils;
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;


/**

  • @ClassName: UploadController
  • @Description:演示上传文件的各种操作
  • @author: 心肝宝贝
  • @date: 2019年5月25日 下午5:51:10
  • @Copyright: 2019 www.haojishu.com Inc. All rights reserved.
  • 注意:本内容仅限于公司内部传阅,禁止外泄以及用于其他的商业目
    */
    @Controller
    public class UploadController {

@Autowired
private HttpServletRequest request;

/**

  • @Title: oneUpload
  • @Description: 单文件上传演示
  • @param: @param file
  • @param: @return
  • @param: @throws IllegalStateException
  • @param: @throws IOException
  • @return: String
  • @throws
    */
    @RequestMapping(value ="/upload/oneUpload",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String oneUpload(@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException{
    if (file.isEmpty()) {
    System.out.println("上传文件为空");
    return "上传文件不能为空";
    }else {
    System.out.println("开始上传");
    String path = request.getServletContext().getRealPath("/upload");
    File isFile = new File(path);
    if (!isFile.exists()) {
    isFile.mkdirs();
    }
    file.transferTo(new File(path, new String(file.getOriginalFilename().getBytes("ISO-8859-1"), "UTF-8")));
    return "上传成功" + path;
    }
    }

/**

  • @Title: multipleUpload
  • @Description: 多文件上传
  • @param: @param files
  • @param: @return
  • @param: @throws IllegalStateException
  • @param: @throws IOException
  • @return: String
  • @throws
    */
    @RequestMapping(value ="/upload/multiple",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String multipleUpload(@RequestParam("file") MultipartFile[] files) throws IllegalStateException, IOException{
    if (files.length<1) {
    System.out.println("上传文件为空");
    return "上传失败";
    }else {
    System.out.println("开始上传");
    String path = request.getServletContext().getRealPath("/upload");
    File isFile = new File(path);
    if (!isFile.exists()) {
    isFile.mkdirs();
    }
    for (MultipartFile file : files) {
    file.transferTo(new File(path, new String(file.getOriginalFilename().getBytes("ISO-8859-1"), "UTF-8")));
    }
    return "上传成功" + path;
    }
    }

@RequestMapping(value ="/upload/base64",produces = "application/json;charset=utf-8")
@ResponseBody
public String base64Upload(String base64) throws Exception {
String[] fileArray = base64.split("base64,");
final byte[] bytes = Base64Utils.decodeFromString(fileArray.length > 1 ? fileArray[1] : fileArray[0]);
String path = request.getServletContext().getRealPath("/upload");
File isFile = new File(path);
if (!isFile.exists()) {
isFile.mkdirs();
}

String suffix = "";
if("data:image/jpg;".equalsIgnoreCase(fileArray[0])){
suffix = ".jpg";
}else{
throw new Exception("上传图片格式不合法" + fileArray[0]);
}
File tempFile = new File(path + "/" + new Date().getTime() + suffix);
FileCopyUtils.copy(bytes, tempFile);
return "文件上传成功"+ path;
}

}

你可能感兴趣的:(Spring文件上传三种形式演示v1.0)