最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
整理收集的一些常用java工具類

1.json轉(zhuǎn)換工具

1.??package?com.taotao.utils;??

3.??import?java.util.List;??

5.??import?com.fasterxml.jackson.core.JsonProcessingException;??
6.??import?com.fasterxml.jackson.databind.JavaType;??
7.??import?com.fasterxml.jackson.databind.JsonNode;??
8.??import?com.fasterxml.jackson.databind.ObjectMapper;??

10.??/**?
11.??*?json轉(zhuǎn)換工具類?
12.??*/??13.??public?class?JsonUtils?{??

15.??//?定義jackson對象??16.??private?static?final?ObjectMapper?MAPPER?=?new?ObjectMapper();??

18.??/**?
19.??*?將對象轉(zhuǎn)換成json字符串。?
20.??*?

Title:?pojoToJson

? 21.??*?

Description:?

? 22.??*?@param?data? 23.??*?@return?24.??*/??25.??public?static?String?objectToJson(Object?data)?{?? 26.??try?{?? 27.??String?string?=?MAPPER.writeValueAsString(data);?? 28.??return?string;?? 29.??}?catch?(JsonProcessingException?e)?{?? 30.??e.printStackTrace();?? 31.??}?? 32.??return?null;?? 33.??}?? 35.??/**? 36.??*?將json結(jié)果集轉(zhuǎn)化為對象? 37.??*?? 38.??*?@param?jsonData?json數(shù)據(jù)? 39.??*?@param?clazz?對象中的object類型? 40.??*?@return?41.??*/??42.??public?static??T?jsonToPojo(String?jsonData,?Class?beanType)?{?? 43.??try?{?? 44.??T?t?=?MAPPER.readValue(jsonData,?beanType);?? 45.??return?t;?? 46.??}?catch?(Exception?e)?{?? 47.??e.printStackTrace();?? 48.??}?? 49.??return?null;?? 50.??}?? 52.??/**? 53.??*?將json數(shù)據(jù)轉(zhuǎn)換成pojo對象list? 54.??*?

Title:?jsonToList

? 55.??*?

Description:?

? 56.??*?@param?jsonData? 57.??*?@param?beanType? 58.??*?@return?59.??*/??60.??public?static?List?jsonToList(String?jsonData,?Class?beanType)?{?? 61.??JavaType?javaType?=?MAPPER.getTypeFactory().constructParametricType(List.class,?beanType);?? 62.??try?{?? 63.??List?list?=?MAPPER.readValue(jsonData,?javaType);?? 64.??return?list;?? 65.??}?catch?(Exception?e)?{?? 66.??e.printStackTrace();?? 67.??}?? 69.??return?null;?? 70.??}?? 72.??}

2.cookie的讀寫

1.??package?com.taotao.common.utils;??

3.??import?java.io.UnsupportedEncodingException;??
4.??import?java.net.URLDecoder;??
5.??import?java.net.URLEncoder;??

7.??import?javax.servlet.http.Cookie;??
8.??import?javax.servlet.http.HttpServletRequest;??
9.??import?javax.servlet.http.HttpServletResponse;??

12.??/**?
13.??*??
14.??*?Cookie?工具類?
15.??*?
16.??*/??17.??public?final?class?CookieUtils?{??

19.??/**?
20.??*?得到Cookie的值,?不編碼?
21.??*??
22.??*?@param?request?
23.??*?@param?cookieName?
24.??*?@return?25.??*/??26.??public?static?String?getCookieValue(HttpServletRequest?request,?String?cookieName)?{??
27.??return?getCookieValue(request,?cookieName,?false);??
28.??}??

30.??/**?
31.??*?得到Cookie的值,?
32.??*??
33.??*?@param?request?
34.??*?@param?cookieName?
35.??*?@return?36.??*/??37.??public?static?String?getCookieValue(HttpServletRequest?request,?String?cookieName,?boolean?isDecoder)?{??
38.??Cookie[]?cookieList?=?request.getCookies();??
39.??if?(cookieList?==?null?||?cookieName?==?null)?{??
40.??return?null;??
41.??}??
42.??String?retValue?=?null;??
43.??try?{??
44.??for?(int?i?=?0;?i??0)??
149.??cookie.setMaxAge(cookieMaxage);??
150.??if?(null?!=?request)?{//?設(shè)置域名的cookie??151.??String?domainName?=?getDomainName(request);??
152.??System.out.println(domainName);??
153.??if?(!"localhost".equals(domainName))?{??
154.??cookie.setDomain(domainName);??
155.??}??
156.??}??
157.??cookie.setPath("/");??
158.??response.addCookie(cookie);??
159.??}?catch?(Exception?e)?{??
160.??e.printStackTrace();??
161.??}??
162.??}??

164.??/**?
165.??*?設(shè)置Cookie的值,并使其在指定時(shí)間內(nèi)生效?
166.??*??
167.??*?@param?cookieMaxage?cookie生效的最大秒數(shù)?
168.??*/??169.??private?static?final?void?doSetCookie(HttpServletRequest?request,?HttpServletResponse?response,??
170.??String?cookieName,?String?cookieValue,?int?cookieMaxage,?String?encodeString)?{??
171.??try?{??
172.??if?(cookieValue?==?null)?{??
173.??cookieValue?=?"";??
174.??}?else?{??
175.??cookieValue?=?URLEncoder.encode(cookieValue,?encodeString);??
176.??}??
177.??Cookie?cookie?=?new?Cookie(cookieName,?cookieValue);??
178.??if?(cookieMaxage?>?0)??
179.??cookie.setMaxAge(cookieMaxage);??
180.??if?(null?!=?request)?{//?設(shè)置域名的cookie??181.??String?domainName?=?getDomainName(request);??
182.??System.out.println(domainName);??
183.??if?(!"localhost".equals(domainName))?{??
184.??cookie.setDomain(domainName);??
185.??}??
186.??}??
187.??cookie.setPath("/");??
188.??response.addCookie(cookie);??
189.??}?catch?(Exception?e)?{??
190.??e.printStackTrace();??
191.??}??
192.??}??

194.??/**?
195.??*?得到cookie的域名?
196.??*/??197.??private?static?final?String?getDomainName(HttpServletRequest?request)?{??
198.??String?domainName?=?null;??

200.??String?serverName?=?request.getRequestURL().toString();??
201.??if?(serverName?==?null?||?serverName.equals(""))?{??
202.??domainName?=?"";??
203.??}?else?{??
204.??serverName?=?serverName.toLowerCase();??
205.??serverName?=?serverName.substring(7);??
206.??final?int?end?=?serverName.indexOf("/");??
207.??serverName?=?serverName.substring(0,?end);??
208.??final?String[]?domains?=?serverName.split("\\.");??
209.??int?len?=?domains.length;??
210.??if?(len?>?3)?{??
211.??//?www.xxx.com.cn??212.??domainName?=?"."?+?domains[len?-?3]?+?"."?+?domains[len?-?2]?+?"."?+?domains[len?-?1];??
213.??}?else?if?(len?<=?3?&&?len?>?1)?{??
214.??//?xxx.com?or?xxx.cn??215.??domainName?=?"."?+?domains[len?-?2]?+?"."?+?domains[len?-?1];??
216.??}?else?{??
217.??domainName?=?serverName;??
218.??}??
219.??}??

221.??if?(domainName?!=?null?&&?domainName.indexOf(":")?>?0)?{??
222.??String[]?ary?=?domainName.split("\\:");??
223.??domainName?=?ary[0];??
224.??}??
225.??return?domainName;??
226.??}??

228.??}

3.HttpClientUtil

1.??package?com.taotao.utils;??

3.??import?java.io.IOException;??
4.??import?java.net.URI;??
5.??import?java.util.ArrayList;??
6.??import?java.util.List;??
7.??import?java.util.Map;??

9.??import?org.apache.http.NameValuePair;??
10.??import?org.apache.http.client.entity.UrlEncodedFormEntity;??
11.??import?org.apache.http.client.methods.CloseableHttpResponse;??
12.??import?org.apache.http.client.methods.HttpGet;??
13.??import?org.apache.http.client.methods.HttpPost;??
14.??import?org.apache.http.client.utils.URIBuilder;??
15.??import?org.apache.http.entity.ContentType;??
16.??import?org.apache.http.entity.StringEntity;??
17.??import?org.apache.http.impl.client.CloseableHttpClient;??
18.??import?org.apache.http.impl.client.HttpClients;??
19.??import?org.apache.http.message.BasicNameValuePair;??
20.??import?org.apache.http.util.EntityUtils;??

22.??public?class?HttpClientUtil?{??

24.??public?static?String?doGet(String?url,?Map?param)?{??

26.??//?創(chuàng)建Httpclient對象??27.??CloseableHttpClient?httpclient?=?HttpClients.createDefault();??

29.??String?resultString?=?"";??
30.??CloseableHttpResponse?response?=?null;??
31.??try?{??
32.??//?創(chuàng)建uri??33.??URIBuilder?builder?=?new?URIBuilder(url);??
34.??if?(param?!=?null)?{??
35.??for?(String?key?:?param.keySet())?{??
36.??builder.addParameter(key,?param.get(key));??
37.??}??
38.??}??
39.??URI?uri?=?builder.build();??

41.??//?創(chuàng)建http?GET請求??42.??HttpGet?httpGet?=?new?HttpGet(uri);??

44.??//?執(zhí)行請求??45.??response?=?httpclient.execute(httpGet);??
46.??//?判斷返回狀態(tài)是否為200??47.??if?(response.getStatusLine().getStatusCode()?==?200)?{??
48.??resultString?=?EntityUtils.toString(response.getEntity(),?"UTF-8");??
49.??}??
50.??}?catch?(Exception?e)?{??
51.??e.printStackTrace();??
52.??}?finally?{??
53.??try?{??
54.??if?(response?!=?null)?{??
55.??response.close();??
56.??}??
57.??httpclient.close();??
58.??}?catch?(IOException?e)?{??
59.??e.printStackTrace();??
60.??}??
61.??}??
62.??return?resultString;??
63.??}??

65.??public?static?String?doGet(String?url)?{??
66.??return?doGet(url,?null);??
67.??}??

69.??public?static?String?doPost(String?url,?Map?param)?{??
70.??//?創(chuàng)建Httpclient對象??71.??CloseableHttpClient?httpClient?=?HttpClients.createDefault();??
72.??CloseableHttpResponse?response?=?null;??
73.??String?resultString?=?"";??
74.??try?{??
75.??//?創(chuàng)建Http?Post請求??76.??HttpPost?httpPost?=?new?HttpPost(url);??
77.??//?創(chuàng)建參數(shù)列表??78.??if?(param?!=?null)?{??
79.??List?paramList?=?new?ArrayList<>();??
80.??for?(String?key?:?param.keySet())?{??
81.??paramList.add(new?BasicNameValuePair(key,?param.get(key)));??
82.??}??
83.??//?模擬表單??84.??UrlEncodedFormEntity?entity?=?new?UrlEncodedFormEntity(paramList);??
85.??httpPost.setEntity(entity);??
86.??}??
87.??//?執(zhí)行http請求??88.??response?=?httpClient.execute(httpPost);??
89.??resultString?=?EntityUtils.toString(response.getEntity(),?"utf-8");??
90.??}?catch?(Exception?e)?{??
91.??e.printStackTrace();??
92.??}?finally?{??
93.??try?{??
94.??response.close();??
95.??}?catch?(IOException?e)?{??
96.??//?TODO?Auto-generated?catch?block??97.??e.printStackTrace();??
98.??}??
99.??}??

101.??return?resultString;??
102.??}??

104.??public?static?String?doPost(String?url)?{??
105.??return?doPost(url,?null);??
106.??}??

108.??public?static?String?doPostJson(String?url,?String?json)?{??
109.??//?創(chuàng)建Httpclient對象??110.??CloseableHttpClient?httpClient?=?HttpClients.createDefault();??
111.??CloseableHttpResponse?response?=?null;??
112.??String?resultString?=?"";??
113.??try?{??
114.??//?創(chuàng)建Http?Post請求??115.??HttpPost?httpPost?=?new?HttpPost(url);??
116.??//?創(chuàng)建請求內(nèi)容??117.??StringEntity?entity?=?new?StringEntity(json,?ContentType.APPLICATION_JSON);??
118.??httpPost.setEntity(entity);??
119.??//?執(zhí)行http請求??120.??response?=?httpClient.execute(httpPost);??
121.??resultString?=?EntityUtils.toString(response.getEntity(),?"utf-8");??
122.??}?catch?(Exception?e)?{??
123.??e.printStackTrace();??
124.??}?finally?{??
125.??try?{??
126.??response.close();??
127.??}?catch?(IOException?e)?{??
128.??//?TODO?Auto-generated?catch?block??129.??e.printStackTrace();??
130.??}??
131.??}??

133.??return?resultString;??
134.??}??
135.??}

4.FastDFSClient工具類

1.??package?cn.itcast.fastdfs.cliennt;??

3.??import?org.csource.common.NameValuePair;??
4.??import?org.csource.fastdfs.ClientGlobal;??
5.??import?org.csource.fastdfs.StorageClient1;??
6.??import?org.csource.fastdfs.StorageServer;??
7.??import?org.csource.fastdfs.TrackerClient;??
8.??import?org.csource.fastdfs.TrackerServer;??

10.??public?class?FastDFSClient?{??

12.??private?TrackerClient?trackerClient?=?null;??
13.??private?TrackerServer?trackerServer?=?null;??
14.??private?StorageServer?storageServer?=?null;??
15.??private?StorageClient1?storageClient?=?null;??

17.??public?FastDFSClient(String?conf)?throws?Exception?{??
18.??if?(conf.contains("classpath:"))?{??
19.??conf?=?conf.replace("classpath:",?this.getClass().getResource("/").getPath());??
20.??}??
21.??ClientGlobal.init(conf);??
22.??trackerClient?=?new?TrackerClient();??
23.??trackerServer?=?trackerClient.getConnection();??
24.??storageServer?=?null;??
25.??storageClient?=?new?StorageClient1(trackerServer,?storageServer);??
26.??}??

28.??/**?
29.??*?上傳文件方法?
30.??*?

Title:?uploadFile

? 31.??*?

Description:?

? 32.??*?@param?fileName?文件全路徑? 33.??*?@param?extName?文件擴(kuò)展名,不包含(.)? 34.??*?@param?metas?文件擴(kuò)展信息? 35.??*?@return?36.??*?@throws?Exception? 37.??*/??38.??public?String?uploadFile(String?fileName,?String?extName,?NameValuePair[]?metas)?throws?Exception?{?? 39.??String?result?=?storageClient.upload_file1(fileName,?extName,?metas);?? 40.??return?result;?? 41.??}?? 43.??public?String?uploadFile(String?fileName)?throws?Exception?{?? 44.??return?uploadFile(fileName,?null,?null);?? 45.??}?? 47.??public?String?uploadFile(String?fileName,?String?extName)?throws?Exception?{?? 48.??return?uploadFile(fileName,?extName,?null);?? 49.??}?? 51.??/**? 52.??*?上傳文件方法? 53.??*?

Title:?uploadFile

? 54.??*?

Description:?

? 55.??*?@param?fileContent?文件的內(nèi)容,字節(jié)數(shù)組? 56.??*?@param?extName?文件擴(kuò)展名? 57.??*?@param?metas?文件擴(kuò)展信息? 58.??*?@return?59.??*?@throws?Exception? 60.??*/??61.??public?String?uploadFile(byte[]?fileContent,?String?extName,?NameValuePair[]?metas)?throws?Exception?{?? 63.??String?result?=?storageClient.upload_file1(fileContent,?extName,?metas);?? 64.??return?result;?? 65.??}?? 67.??public?String?uploadFile(byte[]?fileContent)?throws?Exception?{?? 68.??return?uploadFile(fileContent,?null,?null);?? 69.??}?? 71.??public?String?uploadFile(byte[]?fileContent,?String?extName)?throws?Exception?{?? 72.??return?uploadFile(fileContent,?extName,?null);?? 73.??}?? 74.??}
1.??public?class?FastDFSTest?{??

3.??@Test??
4.??public?void?testFileUpload()?throws?Exception?{??
5.??//?1、加載配置文件,配置文件中的內(nèi)容就是tracker服務(wù)的地址。??6.??ClientGlobal.init("D:/workspaces-itcast/term197/taotao-manager-web/src/main/resources/resource/client.conf");??
7.??//?2、創(chuàng)建一個(gè)TrackerClient對象。直接new一個(gè)。??8.??TrackerClient?trackerClient?=?new?TrackerClient();??
9.??//?3、使用TrackerClient對象創(chuàng)建連接,獲得一個(gè)TrackerServer對象。??10.??TrackerServer?trackerServer?=?trackerClient.getConnection();??
11.??//?4、創(chuàng)建一個(gè)StorageServer的引用,值為null??12.??StorageServer?storageServer?=?null;??
13.??//?5、創(chuàng)建一個(gè)StorageClient對象,需要兩個(gè)參數(shù)TrackerServer對象、StorageServer的引用??14.??StorageClient?storageClient?=?new?StorageClient(trackerServer,?storageServer);??
15.??//?6、使用StorageClient對象上傳圖片。??16.??//擴(kuò)展名不帶“.”??17.??String[]?strings?=?storageClient.upload_file("D:/Documents/Pictures/images/200811281555127886.jpg",?"jpg",?null);??
18.??//?7、返回?cái)?shù)組。包含組名和圖片的路徑。??19.??for?(String?string?:?strings)?{??
20.??System.out.println(string);??
21.??}??
22.??}??
23.??}??

#?![image](/upload/otherpic58/2509688-45370c9d24b87e31%7CimageView2/2/w/1240)

5.獲取異常的堆棧信息

1.??package?com.taotao.utils;??

3.??import?java.io.PrintWriter;??
4.??import?java.io.StringWriter;??

6.??public?class?ExceptionUtil?{??

8.??/**?
9.??*?獲取異常的堆棧信息?
10.??*??
11.??*?@param?t?
12.??*?@return?13.??*/??14.??public?static?String?getStackTrace(Throwable?t)?{??
15.??StringWriter?sw?=?new?StringWriter();??
16.??PrintWriter?pw?=?new?PrintWriter(sw);??

18.??try?{??
19.??t.printStackTrace(pw);??
20.??return?sw.toString();??
21.??}?finally?{??
22.??pw.close();??
23.??}??
24.??}??
25.??}

#6.easyUIDataGrid對象返回值

1.??package?com.taotao.result;??

3.??import?java.util.List;??

5.??/**?
6.??*?easyUIDataGrid對象返回值?
7.??*?

Title:?EasyUIResult

? 8.??*?

Description:?

? 9.??*?

Company:?www.itcast.com

?? 10.??*?@author??入云龍? 11.??*?@date????2015年7月21日下午4:12:52? 12.??*?@version?1.0? 13.??*/??14.??public?class?EasyUIResult?{?? 16.??private?Integer?total;?? 18.??private?List?rows;?? 20.??public?EasyUIResult(Integer?total,?List?rows)?{?? 21.??this.total?=?total;?? 22.??this.rows?=?rows;?? 23.??}?? 25.??public?EasyUIResult(long?total,?List?rows)?{?? 26.??this.total?=?(int)?total;?? 27.??this.rows?=?rows;?? 28.??}?? 30.??public?Integer?getTotal()?{?? 31.??return?total;?? 32.??}?? 33.??public?void?setTotal(Integer?total)?{?? 34.??this.total?=?total;?? 35.??}?? 36.??public?List?getRows()?{?? 37.??return?rows;?? 38.??}?? 39.??public?void?setRows(List?rows)?{?? 40.??this.rows?=?rows;?? 41.??}?? 44.??}

7.ftp上傳下載工具類

1.??package?com.taotao.utils;??

3.??import?java.io.File;??
4.??import?java.io.FileInputStream;??
5.??import?java.io.FileNotFoundException;??
6.??import?java.io.FileOutputStream;??
7.??import?java.io.IOException;??
8.??import?java.io.InputStream;??
9.??import?java.io.OutputStream;??

11.??import?org.apache.commons.net.ftp.FTP;??
12.??import?org.apache.commons.net.ftp.FTPClient;??
13.??import?org.apache.commons.net.ftp.FTPFile;??
14.??import?org.apache.commons.net.ftp.FTPReply;??

16.??/**?
17.??*?ftp上傳下載工具類?
18.??*?

Title:?FtpUtil

? 19.??*?

Description:?

? 20.??*?

Company:?www.itcast.com

?? 21.??*?@author??入云龍? 22.??*?@date????2015年7月29日下午8:11:51? 23.??*?@version?1.0? 24.??*/??25.??public?class?FtpUtil?{?? 27.??/**?? 28.??*?Description:?向FTP服務(wù)器上傳文件?? 29.??*?@param?host?FTP服務(wù)器hostname?? 30.??*?@param?port?FTP服務(wù)器端口?? 31.??*?@param?username?FTP登錄賬號(hào)?? 32.??*?@param?password?FTP登錄密碼?? 33.??*?@param?basePath?FTP服務(wù)器基礎(chǔ)目錄? 34.??*?@param?filePath?FTP服務(wù)器文件存放路徑。例如分日期存放:/2015/01/01。文件的路徑為basePath+filePath? 35.??*?@param?filename?上傳到FTP服務(wù)器上的文件名?? 36.??*?@param?input?輸入流?? 37.??*?@return?成功返回true,否則返回false?? 38.??*/????39.??public?static?boolean?uploadFile(String?host,?int?port,?String?username,?String?password,?String?basePath,?? 40.??String?filePath,?String?filename,?InputStream?input)?{?? 41.??boolean?result?=?false;?? 42.??FTPClient?ftp?=?new?FTPClient();?? 43.??try?{?? 44.??int?reply;?? 45.??ftp.connect(host,?port);//?連接FTP服務(wù)器??46.??//?如果采用默認(rèn)端口,可以使用ftp.connect(host)的方式直接連接FTP服務(wù)器??47.??ftp.login(username,?password);//?登錄??48.??reply?=?ftp.getReplyCode();?? 49.??if?(!FTPReply.isPositiveCompletion(reply))?{?? 50.??ftp.disconnect();?? 51.??return?result;?? 52.??}?? 53.??//切換到上傳目錄??54.??if?(!ftp.changeWorkingDirectory(basePath+filePath))?{?? 55.??//如果目錄不存在創(chuàng)建目錄??56.??String[]?dirs?=?filePath.split("/");?? 57.??String?tempPath?=?basePath;?? 58.??for?(String?dir?:?dirs)?{?? 59.??if?(null?==?dir?||?"".equals(dir))?continue;?? 60.??tempPath?+=?"/"?+?dir;?? 61.??if?(!ftp.changeWorkingDirectory(tempPath))?{?? 62.??if?(!ftp.makeDirectory(tempPath))?{?? 63.??return?result;?? 64.??}?else?{?? 65.??ftp.changeWorkingDirectory(tempPath);?? 66.??}?? 67.??}?? 68.??}?? 69.??}?? 70.??//設(shè)置上傳文件的類型為二進(jìn)制類型??71.??ftp.setFileType(FTP.BINARY_FILE_TYPE);?? 72.??//上傳文件??73.??if?(!ftp.storeFile(filename,?input))?{?? 74.??return?result;?? 75.??}?? 76.??input.close();?? 77.??ftp.logout();?? 78.??result?=?true;?? 79.??}?catch?(IOException?e)?{?? 80.??e.printStackTrace();?? 81.??}?finally?{?? 82.??if?(ftp.isConnected())?{?? 83.??try?{?? 84.??ftp.disconnect();?? 85.??}?catch?(IOException?ioe)?{?? 86.??}?? 87.??}?? 88.??}?? 89.??return?result;?? 90.??}?? 92.??/**?? 93.??*?Description:?從FTP服務(wù)器下載文件?? 94.??*?@param?host?FTP服務(wù)器hostname?? 95.??*?@param?port?FTP服務(wù)器端口?? 96.??*?@param?username?FTP登錄賬號(hào)?? 97.??*?@param?password?FTP登錄密碼?? 98.??*?@param?remotePath?FTP服務(wù)器上的相對路徑?? 99.??*?@param?fileName?要下載的文件名?? 100.??*?@param?localPath?下載后保存到本地的路徑?? 101.??*?@return??102.??*/????103.??public?static?boolean?downloadFile(String?host,?int?port,?String?username,?String?password,?String?remotePath,?? 104.??String?fileName,?String?localPath)?{?? 105.??boolean?result?=?false;?? 106.??FTPClient?ftp?=?new?FTPClient();?? 107.??try?{?? 108.??int?reply;?? 109.??ftp.connect(host,?port);?? 110.??//?如果采用默認(rèn)端口,可以使用ftp.connect(host)的方式直接連接FTP服務(wù)器??111.??ftp.login(username,?password);//?登錄??112.??reply?=?ftp.getReplyCode();?? 113.??if?(!FTPReply.isPositiveCompletion(reply))?{?? 114.??ftp.disconnect();?? 115.??return?result;?? 116.??}?? 117.??ftp.changeWorkingDirectory(remotePath);//?轉(zhuǎn)移到FTP服務(wù)器目錄??118.??FTPFile[]?fs?=?ftp.listFiles();?? 119.??for?(FTPFile?ff?:?fs)?{?? 120.??if?(ff.getName().equals(fileName))?{?? 121.??File?localFile?=?new?File(localPath?+?"/"?+?ff.getName());?? 123.??OutputStream?is?=?new?FileOutputStream(localFile);?? 124.??ftp.retrieveFile(ff.getName(),?is);?? 125.??is.close();?? 126.??}?? 127.??}?? 129.??ftp.logout();?? 130.??result?=?true;?? 131.??}?catch?(IOException?e)?{?? 132.??e.printStackTrace();?? 133.??}?finally?{?? 134.??if?(ftp.isConnected())?{?? 135.??try?{?? 136.??ftp.disconnect();?? 137.??}?catch?(IOException?ioe)?{?? 138.??}?? 139.??}?? 140.??}?? 141.??return?result;?? 142.??}?? 144.??public?static?void?main(String[]?args)?{?? 145.??try?{???? 146.??FileInputStream?in=new?FileInputStream(new?File("D:\\temp\\image\\gaigeming.jpg"));???? 147.??boolean?flag?=?uploadFile("192.168.25.133",?21,?"ftpuser",?"ftpuser",?"/home/ftpuser/www/images","/2015/01/21",?"gaigeming.jpg",?in);???? 148.??System.out.println(flag);???? 149.??}?catch?(FileNotFoundException?e)?{???? 150.??e.printStackTrace();???? 151.??}???? 152.??}?? 153.??}

8.各種id生成策略

1.??package?com.taotao.utils;??

3.??import?java.util.Random;??

5.??/**?
6.??*?各種id生成策略?
7.??*?

Title:?IDUtils

? 8.??*?

Description:?

? 9.??*?@date????2015年7月22日下午2:32:10? 10.??*?@version?1.0? 11.??*/??12.??public?class?IDUtils?{??14.??/**? 15.??*?圖片名生成? 16.??*/??17.??public?static?String?genImageName()?{?? 18.??//取當(dāng)前時(shí)間的長×××值包含毫秒??19.??long?millis?=?System.currentTimeMillis();?? 20.??//long?millis?=?System.nanoTime();??21.??//加上三位隨機(jī)數(shù)??22.??Random?random?=?new?Random();?? 23.??int?end3?=?random.nextInt(999);?? 24.??//如果不足三位前面補(bǔ)0??25.??String?str?=?millis?+?String.format("%03d",?end3);?? 27.??return?str;?? 28.??}?? 30.??/**? 31.??*?商品id生成? 32.??*/??33.??public?static?long?genItemId()?{?? 34.??//取當(dāng)前時(shí)間的長×××值包含毫秒??35.??long?millis?=?System.currentTimeMillis();?? 36.??//long?millis?=?System.nanoTime();??37.??//加上兩位隨機(jī)數(shù)??38.??Random?random?=?new?Random();?? 39.??int?end2?=?random.nextInt(99);?? 40.??//如果不足兩位前面補(bǔ)0??41.??String?str?=?millis?+?String.format("%02d",?end2);?? 42.??long?id?=?new?Long(str);?? 43.??return?id;?? 44.??}?? 46.??public?static?void?main(String[]?args)?{?? 47.??for(int?i=0;i

##9.上傳圖片返回值

1.??package?com.result;??
2.??/**?
3.??*?上傳圖片返回值?
4.??*?

Title:?PictureResult

? 5.??*?

Description:?

? 6.??*?

Company:?www.itcast.com

?? 7.??*?@author??入云龍? 8.??*?@date????2015年7月22日下午2:09:02? 9.??*?@version?1.0? 10.??*/??11.??public?class?PictureResult?{??13.??/**? 14.??*?上傳圖片返回值,成功:0?失?。?????? 15.??*/??16.??private?Integer?error;?? 17.??/**? 18.??*?回顯圖片使用的url? 19.??*/??20.??private?String?url;?? 21.??/**? 22.??*?錯(cuò)誤時(shí)的錯(cuò)誤消息? 23.??*/??24.??private?String?message;?? 25.??public?PictureResult(Integer?state,?String?url)?{?? 26.??this.url?=?url;?? 27.??this.error?=?state;?? 28.??}?? 29.??public?PictureResult(Integer?state,?String?url,?String?errorMessage)?{?? 30.??this.url?=?url;?? 31.??this.error?=?state;?? 32.??this.message?=?errorMessage;?? 33.??}?? 34.??public?Integer?getError()?{?? 35.??return?error;?? 36.??}?? 37.??public?void?setError(Integer?error)?{?? 38.??this.error?=?error;?? 39.??}?? 40.??public?String?getUrl()?{?? 41.??return?url;?? 42.??}?? 43.??public?void?setUrl(String?url)?{?? 44.??this.url?=?url;?? 45.??}?? 46.??public?String?getMessage()?{?? 47.??return?message;?? 48.??}?? 49.??public?void?setMessage(String?message)?{?? 50.??this.message?=?message;?? 51.??}?? 53.??}

10.自定義響應(yīng)結(jié)構(gòu)

1.??package?com.result;??

3.??import?java.util.List;??

5.??import?com.fasterxml.jackson.databind.JsonNode;??
6.??import?com.fasterxml.jackson.databind.ObjectMapper;??

8.??/**?
9.??*?自定義響應(yīng)結(jié)構(gòu)?
10.??*/??11.??public?class?TaotaoResult?{??

13.??//?定義jackson對象??14.??private?static?final?ObjectMapper?MAPPER?=?new?ObjectMapper();??

16.??//?響應(yīng)業(yè)務(wù)狀態(tài)??17.??private?Integer?status;??

19.??//?響應(yīng)消息??20.??private?String?msg;??

22.??//?響應(yīng)中的數(shù)據(jù)??23.??private?Object?data;??

25.??public?static?TaotaoResult?build(Integer?status,?String?msg,?Object?data)?{??
26.??return?new?TaotaoResult(status,?msg,?data);??
27.??}??

29.??public?static?TaotaoResult?ok(Object?data)?{??
30.??return?new?TaotaoResult(data);??
31.??}??

33.??public?static?TaotaoResult?ok()?{??
34.??return?new?TaotaoResult(null);??
35.??}??

37.??public?TaotaoResult()?{??

39.??}??

41.??public?static?TaotaoResult?build(Integer?status,?String?msg)?{??
42.??return?new?TaotaoResult(status,?msg,?null);??
43.??}??

45.??public?TaotaoResult(Integer?status,?String?msg,?Object?data)?{??
46.??this.status?=?status;??
47.??this.msg?=?msg;??
48.??this.data?=?data;??
49.??}??

51.??public?TaotaoResult(Object?data)?{??
52.??this.status?=?200;??
53.??this.msg?=?"OK";??
54.??this.data?=?data;??
55.??}??

57.??//????public?Boolean?isOK()?{??58.??//????????return?this.status?==?200;??59.??//????}??61.??public?Integer?getStatus()?{??
62.??return?status;??
63.??}??

65.??public?void?setStatus(Integer?status)?{??
66.??this.status?=?status;??
67.??}??

69.??public?String?getMsg()?{??
70.??return?msg;??
71.??}??

73.??public?void?setMsg(String?msg)?{??
74.??this.msg?=?msg;??
75.??}??

77.??public?Object?getData()?{??
78.??return?data;??
79.??}??

81.??public?void?setData(Object?data)?{??
82.??this.data?=?data;??
83.??}??

85.??/**?
86.??*?將json結(jié)果集轉(zhuǎn)化為TaotaoResult對象?
87.??*??
88.??*?@param?jsonData?json數(shù)據(jù)?
89.??*?@param?clazz?TaotaoResult中的object類型?
90.??*?@return?91.??*/??92.??public?static?TaotaoResult?formatToPojo(String?jsonData,?Class?clazz)?{??
93.??try?{??
94.??if?(clazz?==?null)?{??
95.??return?MAPPER.readValue(jsonData,?TaotaoResult.class);??
96.??}??
97.??JsonNode?jsonNode?=?MAPPER.readTree(jsonData);??
98.??JsonNode?data?=?jsonNode.get("data");??
99.??Object?obj?=?null;??
100.??if?(clazz?!=?null)?{??
101.??if?(data.isObject())?{??
102.??obj?=?MAPPER.readValue(data.traverse(),?clazz);??
103.??}?else?if?(data.isTextual())?{??
104.??obj?=?MAPPER.readValue(data.asText(),?clazz);??
105.??}??
106.??}??
107.??return?build(jsonNode.get("status").intValue(),?jsonNode.get("msg").asText(),?obj);??
108.??}?catch?(Exception?e)?{??
109.??return?null;??
110.??}??
111.??}??

113.??/**?
114.??*?沒有object對象的轉(zhuǎn)化?
115.??*??
116.??*?@param?json?
117.??*?@return?118.??*/??119.??public?static?TaotaoResult?format(String?json)?{??
120.??try?{??
121.??return?MAPPER.readValue(json,?TaotaoResult.class);??
122.??}?catch?(Exception?e)?{??
123.??e.printStackTrace();??
124.??}??
125.??return?null;??
126.??}??

128.??/**?
129.??*?Object是集合轉(zhuǎn)化?
130.??*??
131.??*?@param?jsonData?json數(shù)據(jù)?
132.??*?@param?clazz?集合中的類型?
133.??*?@return?134.??*/??135.??public?static?TaotaoResult?formatToList(String?jsonData,?Class?clazz)?{??
136.??try?{??
137.??JsonNode?jsonNode?=?MAPPER.readTree(jsonData);??
138.??JsonNode?data?=?jsonNode.get("data");??
139.??Object?obj?=?null;??
140.??if?(data.isArray()?&&?data.size()?>?0)?{??
141.??obj?=?MAPPER.readValue(data.traverse(),??
142.??MAPPER.getTypeFactory().constructCollectionType(List.class,?clazz));??
143.??}??
144.??return?build(jsonNode.get("status").intValue(),?jsonNode.get("msg").asText(),?obj);??
145.??}?catch?(Exception?e)?{??
146.??return?null;??
147.??}??
148.??}??

150.??}

##11.jedis操作

1.??package?com.taotao.jedis;??

3.??public?interface?JedisClient?{??

5.??String?set(String?key,?String?value);??
6.??String?get(String?key);??
7.??Boolean?exists(String?key);??
8.??Long?expire(String?key,?int?seconds);??
9.??Long?ttl(String?key);??
10.??Long?incr(String?key);??
11.??Long?hset(String?key,?String?field,?String?value);??
12.??String?hget(String?key,?String?field);??
13.??Long?hdel(String?key,?String...?field);??
14.??}
1.??package?com.taotao.jedis;??

3.??import?org.springframework.beans.factory.annotation.Autowired;??

5.??import?redis.clients.jedis.JedisCluster;??

7.??public?class?JedisClientCluster?implements?JedisClient?{??

9.??@Autowired??10.??private?JedisCluster?jedisCluster;??

12.??@Override??13.??public?String?set(String?key,?String?value)?{??
14.??return?jedisCluster.set(key,?value);??
15.??}??

17.??@Override??18.??public?String?get(String?key)?{??
19.??return?jedisCluster.get(key);??
20.??}??

22.??@Override??23.??public?Boolean?exists(String?key)?{??
24.??return?jedisCluster.exists(key);??
25.??}??

27.??@Override??28.??public?Long?expire(String?key,?int?seconds)?{??
29.??return?jedisCluster.expire(key,?seconds);??
30.??}??

32.??@Override??33.??public?Long?ttl(String?key)?{??
34.??return?jedisCluster.ttl(key);??
35.??}??

37.??@Override??38.??public?Long?incr(String?key)?{??
39.??return?jedisCluster.incr(key);??
40.??}??

42.??@Override??43.??public?Long?hset(String?key,?String?field,?String?value)?{??
44.??return?jedisCluster.hset(key,?field,?value);??
45.??}??

47.??@Override??48.??public?String?hget(String?key,?String?field)?{??
49.??return?jedisCluster.hget(key,?field);??
50.??}??

52.??@Override??53.??public?Long?hdel(String?key,?String...?field)?{??
54.??return?jedisCluster.hdel(key,?field);??
55.??}??

57.??}
1.??package?com.taotao.jedis;??

4.??import?org.springframework.beans.factory.annotation.Autowired;??

7.??import?redis.clients.jedis.Jedis;??
8.??import?redis.clients.jedis.JedisPool;??

11.??public?class?JedisClientPool?implements?JedisClient?{??

13.??@Autowired??14.??private?JedisPool?jedisPool;??

17.??@Override??18.??public?String?set(String?key,?String?value)?{??
19.??Jedis?jedis?=?jedisPool.getResource();??
20.??String?result?=?jedis.set(key,?value);??
21.??jedis.close();??
22.??return?result;??
23.??}??

26.??@Override??27.??public?String?get(String?key)?{??
28.??Jedis?jedis?=?jedisPool.getResource();??
29.??String?result?=?jedis.get(key);??
30.??jedis.close();??
31.??return?result;??
32.??}??

35.??@Override??36.??public?Boolean?exists(String?key)?{??
37.??Jedis?jedis?=?jedisPool.getResource();??
38.??Boolean?result?=?jedis.exists(key);??
39.??jedis.close();??
40.??return?result;??
41.??}??

44.??@Override??45.??public?Long?expire(String?key,?int?seconds)?{??
46.??Jedis?jedis?=?jedisPool.getResource();??
47.??Long?result?=?jedis.expire(key,?seconds);??
48.??jedis.close();??
49.??return?result;??
50.??}??

53.??@Override??54.??public?Long?ttl(String?key)?{??
55.??Jedis?jedis?=?jedisPool.getResource();??
56.??Long?result?=?jedis.ttl(key);??
57.??jedis.close();??
58.??return?result;??
59.??}??

62.??@Override??63.??public?Long?incr(String?key)?{??
64.??Jedis?jedis?=?jedisPool.getResource();??
65.??Long?result?=?jedis.incr(key);??
66.??jedis.close();??
67.??return?result;??
68.??}??

71.??@Override??72.??public?Long?hset(String?key,?String?field,?String?value)?{??
73.??Jedis?jedis?=?jedisPool.getResource();??
74.??Long?result?=?jedis.hset(key,?field,?value);??
75.??jedis.close();??
76.??return?result;??
77.??}??

80.??@Override??81.??public?String?hget(String?key,?String?field)?{??
82.??Jedis?jedis?=?jedisPool.getResource();??
83.??String?result?=?jedis.hget(key,?field);??
84.??jedis.close();??
85.??return?result;??
86.??}??

89.??@Override??90.??public?Long?hdel(String?key,?String...?field)?{??
91.??Jedis?jedis?=?jedisPool.getResource();??
92.??Long?result?=?jedis.hdel(key,?field);??
93.??jedis.close();??
94.??return?result;??
95.??}??

98.??}


創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),崖州網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:崖州等地區(qū)。崖州做網(wǎng)站價(jià)格咨詢:028-86922220


本文題目:整理收集的一些常用java工具類
網(wǎng)頁URL:http://fisionsoft.com.cn/article/jogegh.html