java中常用的工具类(二)
1、FtpUtil
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
package com.itjh.javaUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
/**
* 用来操作ftp的综合类。<br/>
* 主要依赖jar包commons-net-3.1.jar。
*
* @author 宋立君
* @date 2014年06月25日
*/
public class FtpUtil {
// ftp 地址
private String url;
// ftp端口
private int port;
// 用户名
private String userName;
// 密码
private String password;
/**
* 构造函数
*
* @param url
* ftp地址
* @param port
* ftp端口
* @param userName
* 用户名
* @param password
* 密码
* @author 宋立君
* @date 2014年06月25日
*
*/
public FtpUtil(String url, int port, String userName, String password) {
this.url = url;
this.port = port;
this.userName = userName;
this.password = password;
}
/**
* 从FTP服务器下载指定文件名的文件。
*
* @param remotePath
* FTP服务器上的相对路径
* @param fileName
* 要下载的文件名
* @param localPath
* 下载后保存到本地的路径
* @return 成功下载返回true,否则返回false。
* @throws IOException
* @author 宋立君
* @date 2014年06月25日
*/
public boolean downFile(String remotePath, String fileName, String localPath)
throws IOException {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(userName, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
FTPFile ff;
for (int i = 0; i < fs.length; i++) {
ff = fs[i];
if (null != ff && null != ff.getName()
&& ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* 从FTP服务器列出指定文件夹下文件名列表。
*
* @param remotePath
* FTP服务器上的相对路径
* @return List<String> 文件名列表,如果出现异常返回null。
* @throws IOException
* @author 宋立君
* @date 2014年06月25日
*/
public List<String> getFileNameList(String remotePath) throws IOException {
// 目录列表记录
List<String> fileNames = new ArrayList<String>();
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(userName, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return null;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile file : fs) {
fileNames.add(file.getName());
}
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return fileNames;
}
}
|
依赖包下载链接: http://pan.baidu.com/s/1nt7IoTr 密码: r93l
2、 汉字转拼音
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package com.itjh.test;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
public class SpellHelper {
//将中文转换为英文
public static String getEname(String name) {
HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
pyFormat.setCaseType(HanyuPinyinCaseType. LOWERCASE);
pyFormat.setToneType(HanyuPinyinToneType. WITHOUT_TONE);
pyFormat.setVCharType(HanyuPinyinVCharType. WITH_V);
return PinyinHelper. toHanyuPinyinString(name, pyFormat, "");
}
//姓、名的第一个字母需要为大写
public static String getUpEname(String name) {
char[] strs = name.toCharArray();
String newname = null;
//名字的长度
if (strs.length == 2) {
newname = toUpCase(getEname ("" + strs[0])) + " "
+ toUpCase(getEname ("" + strs[1]));
} else if (strs. length == 3) {
newname = toUpCase(getEname ("" + strs[0])) + " "
+ toUpCase(getEname ("" + strs[1] + strs[2]));
} else if (strs. length == 4) {
newname = toUpCase(getEname ("" + strs[0] + strs[1])) + " "
+ toUpCase(getEname ("" + strs[2] + strs[3]));
} else {
newname = toUpCase(getEname (name));
}
return newname;
}
//首字母大写
private static String toUpCase(String str) {
StringBuffer newstr = new StringBuffer();
newstr.append((str.substring(0, 1)).toUpperCase()).append(
str.substring(1, str.length()));
return newstr.toString();
}
public static void main(String[] args) {
System. out.println( getEname("李宇春"));
}
}
|
需要的jar包 pinyin4.jsr
3、zip工具类
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
package com.itjh.javaUtil;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;
/**
* Zip工具栏类,依赖于commons-compress-1.5.jar。
*
* @author 宋立君
* @date 2014年06月25日
*/
public class ZipUtil {
// public static void main(String[] args){
// try {
// //new ZipUtil().decompressZip(new
// File("d://img.zip"),"img/pic20140626.jpg","d://");
// new ZipUtil().decompressZip(new File("d://img.zip"),"flight.log","d://");
// //new File("d://flight.log").delete();
// //ZipUtil.compress(new File("D://测试压缩文件"),new File("d://img.zip"));
// // ZipUtil.compress(new File[]{new
// File("F:/testZIP/testzip.txt"),new File("d://ftp"),new
// File("e://ftp")},new File("d://压缩文件.zip"));
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
/**
* 把N多文件或文件夹压缩成zip。
*
* @param files
* 需要压缩的文件或文件夹。
* @param zipFilePath
* 压缩后的zip文件
* @throws IOException
* 压缩时IO异常。
* @author 宋立君
* @date 2014年06月25日
*/
public static void compress(File[] files, File zipFile) throws IOException {
if (CollectionUtil.isEmpty(files)) {
return;
}
ZipArchiveOutputStream out = new ZipArchiveOutputStream(zipFile);
out.setUseZip64(Zip64Mode.AsNeeded);
// 将每个文件用ZipArchiveEntry封装
for (File file : files) {
if (file == null) {
continue;
}
compressOneFile(file, out, "");
}
if (out != null) {
out.close();
}
}
/**
* 功能:压缩文件或文件夹。
*
* @author 宋立君
* @date 2014年06月25日
* @param srcFile
* 源文件。
* @param destFile
* 压缩后的文件
* @throws IOException
* 压缩时出现了异常。
*/
public static void compress(File srcFile, File destFile) throws IOException {
ZipArchiveOutputStream out = null;
try {
out = new ZipArchiveOutputStream(new BufferedOutputStream(
new FileOutputStream(destFile), 1024));
compressOneFile(srcFile, out, "");
} finally {
out.close();
}
}
/**
* 功能:压缩单个文件,非文件夹。私有,不对外开放。
*
* @author 宋立君
* @date 2014年06月25日
* @param srcFile
* 源文件,不能是文件夹。
* @param out
* 压缩文件的输出流。
* @param destFile
* 压缩后的文件
* @param dir
* 在压缩包中的位置,根目录传入/。
* @throws IOException
* 压缩时出现了异常。
*/
private static void compressOneFile(File srcFile,
ZipArchiveOutputStream out, String dir) throws IOException {
if (srcFile.isDirectory()) {// 对文件夹进行处理。
ZipArchiveEntry entry = new ZipArchiveEntry(dir + srcFile.getName()
+ "/");
out.putArchiveEntry(entry);
out.closeArchiveEntry();
// 循环文件夹中的所有文件进行压缩处理。
String[] subFiles = srcFile.list();
for (String subFile : subFiles) {
compressOneFile(new File(srcFile.getPath() + "/" + subFile),
out, (dir + srcFile.getName() + "/"));
}
} else { // 普通文件。
InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(srcFile));
// 创建一个压缩包。
ZipArchiveEntry entry = new ZipArchiveEntry(srcFile, dir
+ srcFile.getName());
out.putArchiveEntry(entry);
IOUtils.copy(is, out);
out.closeArchiveEntry();
} finally {
if (is != null)
is.close();
}
}
}
/**
* 功能:解压缩zip压缩包下的所有文件。
*
* @author 宋立君
* @date 2014年06月25日
* @param zipFile
* zip压缩文件
* @param dir
* 解压缩到这个路径下
* @throws IOException
* 文件流异常
*/
public void decompressZip(File zipFile, String dir) throws IOException {
ZipFile zf = new ZipFile(zipFile);
try {
for (Enumeration<ZipArchiveEntry> entries = zf.getEntries(); entries
.hasMoreElements();) {
ZipArchiveEntry ze = entries.nextElement();
// 不存在则创建目标文件夹。
File targetFile = new File(dir, ze.getName());
// 遇到根目录时跳过。
if (ze.getName().lastIndexOf("/") == (ze.getName().length() - 1)) {
continue;
}
// 如果文件夹不存在,创建文件夹。
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
InputStream i = zf.getInputStream(ze);
OutputStream o = null;
try {
o = new FileOutputStream(targetFile);
IOUtils.copy(i, o);
} finally {
if (i != null) {
i.close();
}
if (o != null) {
o.close();
}
}
}
} finally {
zf.close();
}
}
/**
* 功能:解压缩zip压缩包下的某个文件信息。
*
* @author 宋立君
* @date 2014年06月25日
* @param zipFile
* zip压缩文件
* @param fileName
* 某个文件名,例如abc.zip下面的a.jpg,需要传入/abc/a.jpg。
* @param dir
* 解压缩到这个路径下
* @throws IOException
* 文件流异常
*/
public void decompressZip(File zipFile, String fileName, String dir)
throws IOException {
// 不存在则创建目标文件夹。
File targetFile = new File(dir, fileName);
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
ZipFile zf = new ZipFile(zipFile);
Enumeration<ZipArchiveEntry> zips = zf.getEntries();
ZipArchiveEntry zip = null;
while (zips.hasMoreElements()) {
zip = zips.nextElement();
if (fileName.equals(zip.getName())) {
OutputStream o = null;
InputStream i = zf.getInputStream(zip);
try {
o = new FileOutputStream(targetFile);
IOUtils.copy(i, o);
} finally {
if (i != null) {
i.close();
}
if (o != null) {
o.close();
}
}
}
}
}
/**
* 功能:得到zip压缩包下的某个文件信息,只能在根目录下查找。
*
* @author 宋立君
* @date 2014年06月25日
* @param zipFile
* zip压缩文件
* @param fileName
* 某个文件名,例如abc.zip下面的a.jpg,需要传入/abc/a.jpg。
* @return ZipArchiveEntry 压缩文件中的这个文件,没有找到返回null。
* @throws IOException
* 文件流异常
*/
public ZipArchiveEntry readZip(File zipFile, String fileName)
throws IOException {
ZipFile zf = new ZipFile(zipFile);
Enumeration<ZipArchiveEntry> zips = zf.getEntries();
ZipArchiveEntry zip = null;
while (zips.hasMoreElements()) {
zip = zips.nextElement();
if (fileName.equals(zip.getName())) {
return zip;
}
}
return null;
}
/**
* 功能:得到zip压缩包下的所有文件信息。
*
* @author 宋立君
* @date 2014年06月25日
* @param zipFile
* zip压缩文件
* @return Enumeration<ZipArchiveEntry> 压缩文件中的文件枚举。
* @throws IOException
* 文件流异常
*/
public Enumeration<ZipArchiveEntry> readZip(File zipFile)
throws IOException {
ZipFile zf = new ZipFile(zipFile);
Enumeration<ZipArchiveEntry> zips = zf.getEntries();
return zips;
}
}
|
依赖包下载链接: http://pan.baidu.com/s/1eQ9YveU 密码: srnn
posted on 2014-06-26 17:58 kangxuebin 阅读(200) 评论(0) 收藏 举报
浙公网安备 33010602011771号