java通过IO流实现文件上传

 /**
     * 佐证材料上传
     *
     * @param file
     * @param indexCode
     */
    @Override
    public Map<String, String> uploadSupportingMaterial(MultipartFile file, String indexCode) {
        // 获取文件上传盘
        String disk = "D:/";
        try {
		//存储,目录即为:D:/a/b
            String dicPath = disk +
                    "/" + "a" + "/" + "b";
            // 上传并返回新文件名称
            String filePath = FileUploadUtils.upload(dicPath, file);
            String originalFilename = file.getOriginalFilename();
            HashMap<String, String> map = new HashMap<>();
            //文件路径
            map.put("filePath", filePath);
            //原始文件名
            map.put("originalFilename", originalFilename);
            return map;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

//上传核心代码

    public static final String upload(String baseDir, MultipartFile file)
            throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException,
            InvalidExtensionException
    {
		String fileName = file.getOriginalFilename();
        String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
        file.transferTo(Paths.get(absPath));
        return getPathFileName(baseDir, fileName);
    }
	
	//创建文件目录
	   public static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
    {
        File desc = new File(uploadDir + File.separator + fileName);

        if (!desc.exists())
        {
            if (!desc.getParentFile().exists())
            {
                desc.getParentFile().mkdirs();
            }
        }
        return desc;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值