springBoot上传文件服务

312 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

Controller

    //上传文件路径
    @Value("${upload.path}")
    private String UPLOAD_PATH;
  
    @ApiOperation("上传Excel")
    @ResponseBody
    @RequestMapping(value = "/uploadExcel", method = RequestMethod.POST)
    public ResponseEntity uploadExcel(MultipartFile file, HttpServletRequest request) throws IOException {

        if(file==null){
            return new ResponseEntity(ResponseCode.FILE_NULL);
        }
        return applyService.doUploadExcel(file);

    }
package com.esint.fzjlcxdwjlcx.entity.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@ApiModel("文件属性信息")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FilePro {
    @ApiModelProperty("文件名")
    private String fileName;

    @ApiModelProperty("文件尾缀")
    private String suffix;

    @ApiModelProperty("文件路径")
    private String filePath;

    @ApiModelProperty("带尾缀文件名")
    private String fileFullName;

    @ApiModelProperty("带路径的全文件名")
    private String filePahtFullName;

}

applyService的实现类:

  @Override
    public ResponseEntity doUploadExcel(MultipartFile file) {

        //文件保存路径
//        String targetFilePath = "F:\\javaCode\\jz-fzjlcx\\File\\excel\\";
        String targetFilePath = UPLOAD_PATH;
        //上传文件名
        String fileName = file.getOriginalFilename();

        //文件尾缀
        int lastNameKey = fileName.lastIndexOf(".");
        String[] fileNames = new String[]{fileName.substring(0,lastNameKey),fileName.substring(lastNameKey+1)};
        int endKey = fileNames.length - 1;
        String lastName = fileNames[endKey];
        //文件保存
        String newName = UUID.randomUUID().toString();
        File targetFile = new File(targetFilePath+ File.separator + newName + "." + lastName);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(targetFile);
             IOUtils.copy(file.getInputStream(),fileOutputStream);
        }catch (IOException e){
            e.printStackTrace();
            System.out.println("IO异常1");
            return new ResponseEntity(ResponseCode.FILE_IO_ERROR1);
        }finally {
            try {
                fileOutputStream.close();
            }catch (IOException e){
                System.out.println(e);
                return new ResponseEntity(ResponseCode.FILE_IO_ERROR2);
            }
        }
        //封装文件属性回执信息
        FilePro filePro = new FilePro();
        filePro.setSuffix(lastName);
        filePro.setFilePath(UPLOAD_PATH);
        filePro.setFileName(newName);
        filePro.setFileFullName(newName +"." + lastName);
        filePro.setFilePahtFullName(UPLOAD_PATH+newName +"." + lastName);
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.addData(filePro);
        return responseEntity;
    }

FilePro类:

package com.esint.fzjlcxdwjlcx.entity.dto;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@ApiModel("文件属性信息")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FilePro {
    @ApiModelProperty("文件名")
    private String fileName;

    @ApiModelProperty("文件尾缀")
    private String suffix;

    @ApiModelProperty("文件路径")
    private String filePath;

    @ApiModelProperty("带尾缀文件名")
    private String fileFullName;

    @ApiModelProperty("带路径的全文件名")
    private String filePahtFullName;

}

ResponseEntity类:

package com.esint.fzjlcxdwjlcx.entity.response;

import com.esint.fzjlcxdwjlcx.constants.ResponseCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("通用返回")
public class ResponseEntity<T> {

    @ApiModelProperty("返回代码")
    private Integer code;
    @ApiModelProperty("返回消息")
    private String message;
    @ApiModelProperty("返回对象")
    private T data;


    public ResponseEntity(ResponseCode responseCode) {
        this.code = responseCode.getCode();
        this.message = responseCode.getMessageCh();

    }
    public ResponseEntity(Integer code,String message) {
        this.code = code;
        this.message = message;

    }

    public ResponseEntity(ResponseCode responseCode, T data) {
        this.code = responseCode.getCode();
        this.message = responseCode.getMessageCh();
        this.data = data;
    }

    public ResponseEntity<T> addData(T data) {
        this.data = data;
        return this;
    }

    public ResponseEntity<T> success(T data) {
        return new ResponseEntity<T>(ResponseCode.SUCCESS).addData(data);
    }
    public ResponseEntity<T> success(Integer code,String message,T data) {
        return new ResponseEntity<T>(code,message).addData(data);
    }
    public ResponseEntity<T> error(String erorrs) {
        return new ResponseEntity<T>(81000,erorrs);
    }
    public ResponseEntity<T> error(Integer code,String erorrs) {
        return new ResponseEntity<T>(code,erorrs);
    }
}