springboot文件上传保存到数据库

package com.aol.sym.upload;

import com.aol.api.sym.domain.upload.FileUploadInfo;
import com.aol.api.sym.service.upload.IFileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = “/api/upload”,method = RequestMethod.POST)
public class UploadController {

@Value("${honeywell.sme.attach_file_dir}")//**yml文件的跟路径**
private String ATTACH_FILE_DIR;

@Autowired
private IFileUploadService iFileUploadService;
@PostMapping(“/file”)
public FileUploadInfo uploadFile(@RequestParam(“file”) MultipartFile file) {
LocalDate now=LocalDate.now();
DateTimeFormatter df=DateTimeFormatter.ofPattern(“yyyyMMdd”);
String dateDir =now.format(df);
FileUploadInfo fileUploadInfo=new FileUploadInfo();

try {
    String fileCode = String.valueOf(System.nanoTime());
    // 获取文件名
    String oriFileName = file.getOriginalFilename();
    // 获取文件的后缀名
    String suffix = oriFileName.substring(oriFileName.lastIndexOf("."), oriFileName.length());
   //文件上传的路径
    String dirPath = ATTACH_FILE_DIR + dateDir + "/";

// 获取文件上传的全路径(防止文件名称相同)
String fullFilePath = dirPath + fileCode+ suffix;
log.info(“上传文件存储路径:”+fullFilePath);
mkDir(new File(dirPath));

    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(fullFilePath));
    outputStream.write(file.getBytes());
    outputStream.flush();
    outputStream.close();
    //保存数据
    fileUploadInfo.setName(oriFileName.substring(0, oriFileName.lastIndexOf(".")));
    fileUploadInfo.setSuffix(suffix);
    fileUploadInfo.setSize(file.getSize());
    fileUploadInfo.setCode(fileCode);
    fileUploadInfo.setUrl(fullFilePath);
    iFileUploadService.saveInfo(fileUploadInfo);//调用saveInfo保存到数据库的方法
   }catch (Exception e){
       e.printStackTrace();
   }

    return fileUploadInfo;
}

@PostMapping("/doubleFile")
public  FileUploadInfo doubleFile(HttpServletRequest request){
    //用list集合接受上传的多个文件
    List<MultipartFile> files=((MultipartHttpServletRequest) request).getFiles("file");
    MultipartFile file = null;
    BufferedOutputStream stream = null;
    FileUploadInfo fileUploadInfo=null;
    for (int i=0;i<files.size();i++){
     file=files.get(i);
     if(!file.isEmpty()){
         LocalDate now2=LocalDate.now();
         DateTimeFormatter      dfm=DateTimeFormatter.ofPattern("yyyyMMdd");
         String dateDir =now2.format(dfm);
         fileUploadInfo=new FileUploadInfo();
         try {
             String fileCode = String.valueOf(System.nanoTime());
             // 获取文件名
             String oriFileName = file.getOriginalFilename();
             // 获取文件的后缀名
             String suffix = oriFileName.substring(oriFileName.lastIndexOf("."), oriFileName.length());
             //文件上传的路径
             String dirPath = ATTACH_FILE_DIR + dateDir + "/";

// 获取文件上传的全路径(防止文件名称相同)
String fullFilePath = dirPath + fileCode+ suffix;
log.info(“上传文件存储路径:”+fullFilePath);
mkDir(new File(dirPath));

             stream = new BufferedOutputStream(new FileOutputStream(fullFilePath));
             stream.write(file.getBytes());
             stream.flush();
             stream.close();
             //保存数据
             fileUploadInfo.setName(oriFileName.substring(0, oriFileName.lastIndexOf(".")));
             fileUploadInfo.setSuffix(suffix);
             fileUploadInfo.setSize(file.getSize());
             fileUploadInfo.setCode(fileCode);
             fileUploadInfo.setUrl(fullFilePath);
             iFileUploadService.saveInfo(fileUploadInfo);
         }catch (Exception e){
             e.printStackTrace();
         }
     }
    }
    return fileUploadInfo;
}


/**--------------------------------------      wancy          ------------------------------------------**/

/**
 * 判断文件夹是否存在
 * @param file
 */
public static void mkDir(File file) {
    if (file.getParentFile().exists()) {
        file.mkdir();
    } else {
        mkDir(file.getParentFile());
        file.mkdir();
    }
}

}

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

twinkle||cll

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值