黑马程序员---切割文件



------- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

文件切割,将MP3切成相等大小的碎片,最后一片的大小不一定相等,也可以将其他类型的文件切割

但是在切割是要注意不要把字节数组定义成你要切割成的碎片大小,因为可能你文件太大,分成的文件也很大,

定义的字节数组超过了jvm的内存的话,程序会挂掉

可以在程序内添加一个控制大小的机制。

以下是代码:

//文件切割,将MP3切成相等大小的碎片,最后一片的大小不一定相等
	public static void splitFile()throws IOException
	{
		File file = new File("F:\\Study\\day20\\02\\泡沫 - G.E.M. 邓紫棋.mp3");
		FileInputStream fis = new FileInputStream(file);
		FileOutputStream fos = null;
		//定义一个缓冲字节数组
		byte[] buf = new byte[1024*1024];
		int len = 0;
		int count = 0;//控制知否要创建一个碎片文件存储数据
		int num = 1;
		while ((len=fis.read(buf))!=-1)
		{
			//将count和2取模,这个是控制需要切割的文件的大小。如果和3取模就是每个文件3M,
			//如果需要可以设置传值,这里我就没有弄了
			if (count%2==0)
			{
				fos = new FileOutputStream("F:\\Study\\day20\\02\\"+(num++)+".part");
				fos.write(buf,0,len);
				fos.flush();
				count++;
			}
			else
			{
				fos.write(buf,0,len);
				fos.flush();
				count++;
			}
			
		}
		fos.close();
		fis.close();
	}

### Spring Security入门案例与黑马程序员教程 Spring Security 是一个强大的安全框架,用于保护基于 Java 的应用程序。它提供了认证、授权和防止常见攻击的功能。以下是一个简单的入门案例,结合了 Spring Boot 和 Spring Security 的基本配置。 #### 1. 引入依赖 在 `pom.xml` 文件中添加 Spring Security 的启动器依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 引入此依赖后,Spring Security 将自动为项目提供基础的安全保护[^2]。 #### 2. 配置安全规则 创建一个配置类来定义访问规则。例如: ```java import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/public/**").permitAll() // 允许所有用户访问 /public 路径 .anyRequest().authenticated() // 其他路径需要认证 .and() .formLogin() .loginPage("/login") // 自定义登录页面 .permitAll() .and() .logout() .permitAll(); } } ``` 上述代码定义了访问规则:`/public/**` 路径对所有用户开放,而其他路径需要经过身份验证才能访问[^2]。 #### 3. 创建用户认证信息 可以通过内存中的用户数据进行简单认证。例如: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; @Configuration public class AuthConfig { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password(passwordEncoder().encode("password")).roles("USER") .and() .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN"); } } ``` 这里定义了两个用户:`user` 和 `admin`,分别拥有不同的角色权限[^2]。 #### 4. 创建控制器 为了测试安全规则,可以创建一个简单的控制器: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/public/hello") public String publicHello() { return "Hello, this is a public resource!"; } @GetMapping("/private/hello") public String privateHello() { return "Hello, this is a private resource!"; } } ``` 访问 `/public/hello` 不需要认证,而访问 `/private/hello` 则需要认证。 #### 5. 测试应用 运行 Spring Boot 应用程序后,尝试访问以下 URL: - `/public/hello`:可以直接访问。 - `/private/hello`:将被重定向到登录页面,输入用户名和密码后可以访问。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值