接受前台文件类型(MultipartFile)的参数,后台进行本地下载保存,返回文件存储路径

 @PostMapping("/uploadPictureOCR")
    @ApiOperation(value = "上传图片" , notes = "单张图片上传")
    public String uploadPictureOCR(@RequestParam("file") MultipartFile file)throws Exception{

        String filePath = UploadImgUtils.approvalFile(file);
        System.out.println(filePath);
        return filePath;
        // 以下代码 是获取到这个文件路径 进行上传到服务器,可以忽略
        /*File result = new File(filePath);
        //File file_path = FileUtil.mkdir(tempDir+file.getOriginalFilename());
        Map resultMap = new HashMap<>(1);
        //File tempFile = FileUtil.createTempFile(new File(tempDir));
        file.transferTo(result);

        HttpRequest post = HttpUtil.createPost(uploadUrl + "upload?output=json");
        post.form("file",new FileResource(result));
        String str = post.execute().body();
        String downloadPath = JSON.parseObject(str).getString("path");
        resultMap.put("filename", downloadPath);
        return  resultMap;*/
    }

图中是controller中对外暴露的接口,前端请求接口的时候,将文件传入后台接口。剩下来的事情就是将其文件进行本地下载,并保存到磁盘中。

package com.community.util;

import com.community.web.RandomNumberUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

/**
 * @Author: tubiao
 * @Date: 2019/4/10 0010 17:59
 * @describe        图片下载到本地
 */
public class UploadImgUtils {


    public static String approvalFile(MultipartFile filecontent){
        OutputStream os = null;
        InputStream inputStream = null;
        String fileName = RandomNumberUtils.getRandomString();
        String path = null;
        try {
            inputStream = filecontent.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            // Linux 文件保存的路径
             path ="/root/tmp/";
    
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流保存到本地文件
            File tempFile = new File(path);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            os = new FileOutputStream(tempFile.getPath()+ "/" + File.separator + fileName);
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕,关闭所有链接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return path +fileName;
    }

}
package com.community.web;

import java.time.LocalDateTime;
import java.util.UUID;

/**
 * @Author: tubiao
 * @Date: 2019/1/23 0023 9:11
 * @describe    随机生成唯一 code
 */
public class RandomNumberUtils {

    public static String getRandomString(){
        StringBuilder sb = new StringBuilder();
        sb.append(LocalDateTime.now().getYear());
        int month = LocalDateTime.now().getMonth().getValue();
        sb.append(month>=10?month:'0'+month);
        sb.append(LocalDateTime.now().getDayOfMonth());
        sb.append(Math.abs(UUID.randomUUID().getLeastSignificantBits()%1_00_00_000_000L));
        return sb.toString();
    }
}

 

你可能感兴趣的:(接受前台文件类型(MultipartFile)的参数,后台进行本地下载保存,返回文件存储路径)