酷酷的嵩 2022-01-19 11:15 采纳率: 62.5%
浏览 148
已结题

springboot项目部署到Linux服务器 进行文件上传

项目在服务器上文件上传想放到jar包所在文件夹lib的同级文件夹dist的upload文件夹中,该如何实现

@RestController
@RequestMapping("/api/bbb")
public class TimeController {
        @PostMapping("/upload")
        public BaseResult upload(MultipartFile file){
            //获取文件名
            String fileName = file.getOriginalFilename();
            //获取文件后缀名
           /* String suffixName = fileName.substring(fileName.lastIndexOf("."));
            //重新生成文件名
            fileName = UUID.randomUUID()+suffixName;*/
            //指定本地文件夹存储
            String filePath = ".../dist/upload/";
            try {
                //将图片保存到static文件夹里
                file.transferTo(new File(filePath+fileName));
                String tarpath = "tar -xvf"+ fileName+"-.../dist/upload";
                Process process = Runtime.getRuntime().exec(tarpath);

                return new BaseResult("200","success","");
            } catch (Exception e) {
                e.printStackTrace();
                return new BaseResult("400","failed","");
            }
        }
    }

  • 写回答

1条回答 默认 最新

  • 酒一杯。 2022-01-19 11:23
    关注
    
    /**
         * 保存文件工具类
         * @param file 文件
         * @param storagePath 保存地址
         * @param fileName 文件名称
         * @return
         */
        public static  Boolean saveFile( MultipartFile file, String storagePath,  String fileName) throws Exception{
            //检查当前有没有传入的目录 如果有则不创建 ,反之创建目录
            Path path = Paths.get(storagePath);
            Boolean isCreate = null;
            if (!Files.exists(path)) {
                //创建目录
                Path directories = Files.createDirectories(path);
                isCreate = createFile(file, fileName, directories);
            } else {
                isCreate = createFile(file, fileName, path);
            }
            return isCreate;
        }
    
    private static Boolean createFile(MultipartFile file, String fileName, Path directories) {
            try {
                //获取输入流
                InputStream in = file.getInputStream();
                FileOutputStream fileOut = new FileOutputStream(new File(directories + "\\" + fileName));
                byte[] buf = new byte[1024 * 8];
                while (true) {
                    int read = 0;
                    if (in != null) {
                        read = in.read(buf);
                    }
                    if (read == -1) {
                        break;
                    }
                    fileOut.write(buf, 0, read);
                }
                fileOut.flush();
                fileOut.close();
                in.close();
            } catch (IOException e) {
                return false;
            }
            return true;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 2月17日
  • 已采纳回答 2月9日
  • 创建了问题 1月19日

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型