Java上传文件到服务器返回url

private String saveImageReturnPath(MultipartFile multiFile) throws IllegalStateException, IOException {
     
		
		String dateName = PicFileUtil.randomFileName()+multiFile.getOriginalFilename();
		
        String path = PicFileUtil.getImgUploadPath(Constants.UPLOAD_NAME); // /upload

        String fileName = String.format("%s/%s", PicFileUtil.dateFolder(), dateName);

        String filePathName = String.format("/%s/%s", Constants.UPLOAD_NAME, fileName);
        
        String filePath = String.format("/%s/%s", path, fileName);

        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
     
            file.getParentFile().mkdirs();
        }
        multiFile.transferTo(file);
        return filePathName;
    }
	// 上传文件

	@Override
	public String uploadFile(MultipartFile file) {
     
			
		try {
     
			return "/ba/"+saveImageReturnPath(file);
		} catch (Exception e) {
     
			e.printStackTrace();
		}
		return null;
		
	}
}

文件上传地址:

public static String getImgUploadPath(String folderName) {
     
		
		// /home/yishang_data/project/lib/yishang_data_main.jar!/BOOT-INF/classes!/../../upload
		String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "../../../../" + folderName;
		path = path.replace("file:", "");
		
		log.info("path={}", path);
		
		File file = new File(path);
		if( ! file.exists() ) {
     
			file.mkdirs();
		}
		path = getRealPath(path);
		return path;
	}

你可能感兴趣的:(Java,java,上传文件)