绝对路径文件上传


    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestBody JSONObject blobPath) throws Exception
    {
        String blobPath1 = blobPath.getStr("blob");
        MultipartFile file = convertToMultipartFile(blobPath1);
        if(file==null){
            return AjaxResult.error("网络异常,请稍后尝试");
        }
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("fileName", fileName);
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
public MultipartFile convertToMultipartFile(String filePath) throws IOException {
        File file = new File(filePath);
        String s = Files.probeContentType(file.toPath());
        System.out.println("s = " + s);
        String name = file.getName();
        System.out.println("name = " + name);
        int length = (int) file.length();
        File parentFile = file.getParentFile();
        FileItem fileItem = new DiskFileItem("copyfile.txt", s,false, name, length, parentFile);
        byte[] buffer = new byte[4096];
        int n;
        MultipartFile multipartFile = null;
        try (InputStream inputStream = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()){
            while ( (n = inputStream.read(buffer,0,4096)) != -1){
                os.write(buffer,0,n);
            }
            multipartFile = new CommonsMultipartFile(fileItem);
            System.out.println(multipartFile.getName());


        }catch (IOException e){
            e.printStackTrace();
        }
        return multipartFile;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值