文件上传

文件上传

UploadUtils:

package com.lyc.utils;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 文件上传工具类
 */
public class UploadUtils {
    private static final int MAX_FILE_ZIE = 10* 1024*- 1024; // 文件最大size
    public static Map upload(HttpServletRequest request, MultipartFile file) {
        // 获取文件名
        String fileName = file.getOriginalFilename();
        // 获取文件后缀
        int pos = fileName.lastIndexOf('.');
        // 文件后缀
        String ext = fileName.substring(pos);
        // 生成新文件名
        String newFileName = UUID.randomUUID().toString()+ext;
        // 获取服务器的根路径
        String root = request.getServletContext().getRealPath("/");
        // 根据附件上传目录 生成文件
        String tmp = "attachment/"+DateUtils.getYmd()+"/"; //临时目录
        File dir = new File(root,tmp);
        if (!dir.exists()){
            // 创建目录
            dir.mkdirs();
        }
        // 创建目标文件
        File newFile = new File(dir, newFileName);
        // 文件拷贝
        try {
            copyFile(file.getInputStream(), newFile);
        }catch (IOException e){
            e.printStackTrace();
        }

        Map map = new HashMap();
        map.put("name", fileName);
        map.put("attType", file.getContentType());
        map.put("attUrl", tmp+newFileName);
        return map;
    }

    /**
     *
     * @param src  上传的文件 二进制流
     * @param dist 服务器上存储 的文件路径
     */
        static void copyFile(InputStream src,File dist){
            // 缓存流
            InputStream in;
            BufferedInputStream bis = new BufferedInputStream(src);
            // 生成文件输出流
            FileOutputStream fos = null;
            try {
                File file;
                fos = new FileOutputStream(dist);
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            // 设置缓存大小 每次读取1024字节
            byte[] buffer = new byte[1024];
            try{
                while (bis.read(buffer)!=-1){
                    bos.write(buffer);
                }
                bos.flush();
                if (null!=bis){
                    bis.close();
                }
                if (null!=bos){
                    bos.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }

}

DateUtils:

package com.lyc.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public static String getYmd(){
        Calendar calendar = Calendar.getInstance();
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
}

UploadController:

package com.lyc.controller.common;

import com.lyc.utils.UploadUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/file")
public class UploadController extends BaseController{
    @RequestMapping("/upload")
    public String upload(HttpServletRequest request, @RequestPart MultipartFile file){
        Map map = UploadUtils.upload(request, file);
        return success("文件上传",map);
    }

}

原文地址:https://www.cnblogs.com/liuyunche/p/14918387.html