Java文件操作与MD5加密实用技巧
下载需积分: 31 | PPT格式 | 57KB |
更新于2024-08-18
| 85 浏览量 | 举报
"这篇文档主要介绍了Java在处理文件读取和保存时的一些实用技巧,包括MD5加密、ResourceBundle的使用、对象实例化、文件操作以及时间工具的使用。"
在Java开发中,文件操作是必不可少的一部分。文章提到了一些关于文件读取和保存的小技巧。首先,MD5加密是一种常见的数据安全处理方式,用于生成文件或数据的唯一标识。在Java中,可以使用`java.security.MessageDigest`类来实现MD5加密,例如:
```java
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public String getMd5(String input) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] messageDigest = md.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : messageDigest) {
hexString.append(String.format("%02x", 0xFF & b));
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
```
接着,文章提到了`ResourceBundle`的使用,这是一个用于国际化和本地化的工具,可以从指定的资源配置文件中获取字符串。例如:
```java
ResourceBundle bundle = ResourceBundle.getBundle("sysConfig");
String domain = bundle.getString("domain");
```
对于类的实例化,文章中的代码展示了如何根据类名动态创建并初始化对象:
```java
String className = wec.getClassname();
KeyServiceI keyService = (KeyServiceI) Class.forName(className).newInstance();
```
文件操作方面,文档中提及了`FileUtil`类的一个方法`validateType()`,用于过滤图片文件类型,确保上传的文件是允许的图片格式:
```java
public boolean validateType(MultipartFile file) {
List<String> allowTypes = Arrays.asList("image/bmp", "image/gif", "image/jpeg", "image/pjpeg", "image/png");
return allowTypes.contains(file.getContentType().toLowerCase());
}
```
此外,还提到了读取`.properties`配置文件的方法,通常我们会在`src/main/resources`目录下放置此类文件,然后通过`Properties`类进行读取:
```java
Properties props = new Properties();
props.load(new FileInputStream("siteUrl.properties"));
String siteUrl = props.getProperty("siteUrl");
```
文件读写方面,文档中提到的一个jQuery技巧是清除`<input type="file">`元素的选择,以便用户可以重新选择文件:
```javascript
var file = $("#file");
file.after(file.clone().val(""));
file.remove();
```
最后,文章提到了一个时间工具`DateUtil`,可能包含获取当前日期时间的方法,如:
```java
public static String getCurDateTime(String formatStr) {
SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
return sdf.format(new Date());
}
```
这些技巧涵盖了文件操作、数据安全、对象实例化和时间处理等多个方面,对于日常的Java开发工作具有一定的参考价值。
相关推荐
![filetype](https://img-home.csdnimg.cn/images/20241231045053.png)
![filetype](https://img-home.csdnimg.cn/images/20241231044955.png)
![filetype](https://img-home.csdnimg.cn/images/20241231044955.png)
![filetype](https://img-home.csdnimg.cn/images/20241231044955.png)
![filetype](https://img-home.csdnimg.cn/images/20241231044833.png)
![filetype](https://img-home.csdnimg.cn/images/20210720083606.png)
![filetype](https://img-home.csdnimg.cn/images/20241226111658.png)
![filetype](https://img-home.csdnimg.cn/images/20241226111658.png)
![filetype](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://profile-avatar.csdnimg.cn/bc729d378e924857857fa9334e467b9b_weixin_42183453.jpg!1)
巴黎巨星岬太郎
- 粉丝: 19
最新资源
- Windows CE开发与嵌入式Linux资料概览
- Borland PME模型:属性、方法和事件
- Oracle全文检索技术深度解析
- 使用PHP接口实现与Google搜索引擎交互
- .Net框架中的Socket编程基础
- C#编程进阶指南:对象思考与核心技术
- Visual C# 中的MDI编程实践
- C语言数值计算:经典教程与源码解析
- TCP/IP协议下的Socket基础与进程通信解决策略
- Java学习经验分享:动态加载与类查找原理探索
- Oracle 1z0-031 认证考试试题与学习指南
- EJB3基础教程:元数据批注与EntityBean解析
- 深入理解Hibernate 3.x过滤器:参数化与灵活性提升
- Eclipse+MyEclipse集成:Struts+Spring+Hibernate开发用户信息查询示例
- Visual C#数据库编程基础:浏览、修改、删除与插入
- 基于小波变换的图像边缘检测Matlab代码实现