Newer
Older
huludao / src / main / java / com / newfiber / modules / inspection / controller / FileUploadController.java
package com.newfiber.modules.inspection.controller;

import com.newfiber.common.utils.FilesUtils;
import com.newfiber.common.utils.R;
import com.newfiber.modules.inspection.entity.FileUploadEntity;
import com.newfiber.modules.inspection.service.impl.FileUploadServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author:zhangjun
 * @Project_name:newfiber_pump_lwz
 * @date:2020/9/716:35
 * @email:505724729@qq.com
 */
@RestController
@RequestMapping("file")
@Slf4j
@Api(tags = "文件上传")
public class FileUploadController {

    @Autowired
    private FileUploadServiceImpl fileUploadService;

    @Autowired
    private FilesUtils filesUtils;

    private static String getFilePath() {
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {
            return "C:/usr/file/pump_lwz/file";
        }
        return "/usr/file/pump_lwz/file";
    }

    /**
     * 上传单个文件
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ResponseBody
//    @ApiOperation("上传单个文件")
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败,请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String savePath = getFilePath();

        File savePathFile = new File(savePath);
        if (!savePathFile.exists() && !savePathFile.isDirectory()) {
            savePathFile.mkdir();
        }

        File dest = new File(savePath + fileName);
        try {
            file.transferTo(dest);
            log.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            log.error(e.toString(), e);
        }
        return "上传失败!";
    }

    /**
     * 上传多个文件
     *
     * @return
     */
    @PostMapping("/multiUpload")
    @ResponseBody
    public List<String> multiUpload(@RequestParam("file") MultipartFile[] file) throws IOException {
        if (file.length == 0) {
//            return "请选择要上传的文件";
        }

        List<FileUploadEntity> fileUploadEntities = new ArrayList<>();
        List<String> fileNos = new ArrayList<>();
        for (MultipartFile multipartFile : file) {

            if (multipartFile.isEmpty()) {
//                return "文件上传失败";
            }
            byte[] fileBytes = multipartFile.getBytes();
            String filePath = getFilePath();

            File savePathFile = new File(filePath);
            if (!savePathFile.exists() && !savePathFile.isDirectory()) {
                savePathFile.mkdir();
            }

            //取得当前上传文件的文件名称
            String originalFilename = multipartFile.getOriginalFilename();
            //生成文件名
            String fileName = UUID.randomUUID() + "&" + originalFilename;
            String fileNo = UUID.randomUUID().toString();
            FileUploadEntity fileUploadEntity = FileUploadEntity.builder()
                    .fileName(originalFilename)
                    .fileNo(fileNo)
                    .filePath(fileName).build();
            fileUploadEntities.add(fileUploadEntity);
            fileNos.add(fileNo);
            uploadFile(fileBytes, filePath, fileName);
        }

        fileUploadService.saveBatch(fileUploadEntities);
        return fileNos;
    }

    private static void uploadFile(byte[] file, String filePath, String fileName) throws IOException {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file);
        out.flush();
        out.close();
    }


    /**
     * 上传多个文件
     */
    @PostMapping("/save")
    @ResponseBody
    @ApiOperation("上传多个文件")
    public R save(@RequestParam("relationId") String relationId, @RequestParam("file") MultipartFile[] file) {
        if (file != null) {
            /**上传文件并保存*/
            try {
                List<FileUploadEntity> fileUploadEntities = filesUtils.filesUpload(relationId, file);
                fileUploadService.saveBatch(fileUploadEntities);

            } catch (IOException e) {
                e.printStackTrace();
                return R.error("上传失败");
            }
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("business:opsbusinessarchives:delete")
    @ApiOperation("删除文件")
    public R delete(@RequestBody Long[] ids) {
        fileUploadService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}