java常用工具类

www.yabo19.app 1

1.json转换工具1. package com.taotao.utils;

  1. import java.util.List;

  2. import com.fasterxml.jackson.core.JsonProcessingException;

  3. import com.fasterxml.jackson.databind.JavaType;

  4. yabo2019app,import com.fasterxml.jackson.databind.JsonNode;

  5. import com.fasterxml.jackson.databind.ObjectMapper;

  6. /**

  7. * json转换工具类

  8. */

  9. public class JsonUtils {

  10. // 定义jackson对象

  11. private static final ObjectMapper MAPPER = new ObjectMapper();

  12. /**

  13. * 将对象转换成json字符串。

  14. * <p>Title: pojoToJson</p>

  15. * <p>Description: </p>

  16. * @param data

  17. * @return

  18. */

  19. public static String objectToJson(Object data) {

  20. try {

  21. String string = MAPPER.writeValueAsString;

  22. return string;

  23. } catch (JsonProcessingException e) {

  24. e.printStackTrace();

  25. }

  26. return null;

  27. }

  28. /**

  29. * 将json结果集转化为对象

  30. *

  31. * @param jsonData json数据

  32. * @param clazz 对象中的object类型

  33. * @return

  34. */

  35. public static <T> T jsonToPojo(String jsonData, Class<T>
    beanType) {

  36. try {

  37. T t = MAPPER.readValue(jsonData, beanType);

  38. return t;

  39. } catch (Exception e) {

  40. e.printStackTrace();

  41. }

  42. return null;

  43. }

  44. /**

  45. * 将json数据转换成pojo对象list

  46. * <p>Title: jsonToList</p>

  47. * <p>Description: </p>

  48. * @param jsonData

  49. * @param beanType

  50. * @return

  51. */

  52. public static <T>List<T> jsonToList(String jsonData,
    Class<T> beanType) {

  53. JavaType javaType =
    MAPPER.getTypeFactory().constructParametricType(List.class, beanType);

  54. try {

  55. List<T> list = MAPPER.readValue(jsonData, javaType);

  56. return list;

  57. www.yabo19.app,} catch (Exception e) {

  58. e.printStackTrace();

  59. }

  60. return null;

  61. }

  62. }

2.cookie的读写

  1. package com.taotao.common.utils;

  2. java常用工具类。import java.io.UnsupportedEncodingException;

  3. import java.net.URLDecoder;

  4. import java.net.URLEncoder;

  5. import javax.servlet.http.Cookie;

  6. import javax.servlet.http.HttpServletRequest;

  7. import javax.servlet.http.HttpServletResponse;

  8. /**

  9. *

  10. * Cookie 工具类

  11. *

  12. */

  13. public final class CookieUtils {

  14. /**

  15. * 得到Cookie的值, 不编码

  16. *

  17. * @param request

  18. * @param cookieName

  19. * @return

  20. */

  21. public static String getCookieValue(HttpServletRequest request,
    String cookieName) {

  22. return getCookieValue(request, cookieName, false);

  23. }

  24. /**

  25. * 得到Cookie的值,

  26. *

  27. * @param request

  28. * @param cookieName

  29. * @return

  30. */

  31. public static String getCookieValue(HttpServletRequest request,
    String cookieName, boolean isDecoder) {

  32. Cookie[] cookieList = request.getCookies();

  33. if (cookieList == null || cookieName == null) {

  34. return null;

  35. }

  36. String retValue = null;

  37. try {

  38. for (int i = 0; i < cookieList.length; i++) {

  39. if (cookieList[i].getName().equals(cookieName)) {

  40. if (isDecoder) {

  41. retValue = URLDecoder.decode(cookieList[i].getValue(), “UTF-8”);

  42. } else {

  43. retValue = cookieList[i].getValue();

  44. }

  45. break;

  46. }

  47. }

  48. } catch (UnsupportedEncodingException e) {

  49. e.printStackTrace();

  50. }

  51. return retValue;

  52. }

  53. /**

  54. * 得到Cookie的值,

  55. *

  56. * @param request

  57. * @param cookieName

  58. * @return

  59. */

  60. public static String getCookieValue(HttpServletRequest request,
    String cookieName, String encodeString) {

  61. Cookie[] cookieList = request.getCookies();

  62. if (cookieList == null || cookieName == null) {

  63. return null;

  64. }

  65. String retValue = null;

  66. try {

  67. for (int i = 0; i < cookieList.length; i++) {

  68. if (cookieList[i].getName().equals(cookieName)) {

  69. retValue = URLDecoder.decode(cookieList[i].getValue(),
    encodeString);

  70. break;

  71. }

  72. }

  73. } catch (UnsupportedEncodingException e) {

  74. e.printStackTrace();

  75. }

  76. return retValue;

  77. }

  78. /**

  79. * 设置Cookie的值 不设置生效时间默认浏览器关闭即失效,也不编码

  80. */

  81. public static void setCookie(HttpServletRequest request,
    HttpServletResponse response, String cookieName,

  82. String cookieValue) {

  83. setCookie(request, response, cookieName, cookieValue, -1);

  84. }

  85. /**

  86. * 设置Cookie的值 在指定时间内生效,但不编码

  87. */

  88. public static void setCookie(HttpServletRequest request,
    HttpServletResponse response, String cookieName,

  89. String cookieValue, int cookieMaxage) {

  90. setCookie(request, response, cookieName, cookieValue, cookieMaxage,
    false);

  91. }

  92. /**

  93. * 设置Cookie的值 不设置生效时间,但编码

  94. */

  95. public static void setCookie(HttpServletRequest request,
    HttpServletResponse response, String cookieName,

  96. String cookieValue, boolean isEncode) {

  97. setCookie(request, response, cookieName, cookieValue, -1,
    isEncode);

  98. }

  99. /**

  100. * 设置Cookie的值 在指定时间内生效, 编码参数

  101. */

  102. public static void setCookie(HttpServletRequest request,
    HttpServletResponse response, String cookieName,

  103. String cookieValue, int cookieMaxage, boolean isEncode) {

  104. doSetCookie(request, response, cookieName, cookieValue,
    cookieMaxage, isEncode);

  105. }

  106. /**

  107. * 设置Cookie的值 在指定时间内生效, 编码参数

  108. */

  109. public static void setCookie(HttpServletRequest request,
    HttpServletResponse response, String cookieName,

  110. String cookieValue, int cookieMaxage, String encodeString) {

  111. doSetCookie(request, response, cookieName, cookieValue,
    cookieMaxage, encodeString);

  112. }

  113. /**

  114. *java常用工具类。 删除Cookie带cookie域名

  115. */

  116. public static void deleteCookie(HttpServletRequest request,
    HttpServletResponse response,

  117. String cookieName) {

  118. doSetCookie(request, response, cookieName, “”, -1, false);

  119. }

  120. /**

  121. * 设置Cookie的值,并使其在指定时间内生效

  122. *

  123. * @param cookieMaxage cookie生效的最大秒数

  124. */

  125. private static final void doSetCookie(HttpServletRequest request,
    HttpServletResponse response,

  126. String cookieName, String cookieValue, int cookieMaxage, boolean
    isEncode) {

  127. try {

  128. if (cookieValue == null) {

  129. cookieValue = “”;

  130. } else if {

  131. cookieValue = URLEncoder.encode(cookieValue, “utf-8”);

  132. }

  133. Cookie cookie = new Cookie(cookieName, cookieValue);

  134. if (cookieMaxage > 0)

  135. cookie.setMaxAge(cookieMaxage);

  136. if (null != request) {// 设置域名的cookie

  137. String domainName = getDomainName;

  138. System.out.println(domainName);

  139. if (!”localhost”.equals(domainName)) {

  140. cookie.setDomain(domainName);

  141. java常用工具类。java常用工具类。}

  142. }

  143. cookie.setPath;

  144. response.addCookie;

  145. } catch (Exception e) {

  146. e.printStackTrace();

  147. }

  148. }

  149. /**

  150. * 设置Cookie的值,并使其在指定时间内生效

  151. *

  152. * @param cookieMaxage cookie生效的最大秒数

  153. */

  154. private static final void doSetCookie(HttpServletRequest request,
    HttpServletResponse response,

  155. String cookieName, String cookieValue, int cookieMaxage, String
    encodeString) {

  156. try {

  157. if (cookieValue == null) {

  158. cookieValue = “”;

  159. } else {

  160. cookieValue = URLEncoder.encode(cookieValue, encodeString);

  161. }

  162. Cookie cookie = new Cookie(cookieName, cookieValue);

  163. if (cookieMaxage > 0)

  164. cookie.setMaxAge(cookieMaxage);

  165. if (null != request) {// 设置域名的cookie

  166. String domainName = getDomainName;

  167. System.out.println(domainName);

  168. if (!”localhost”.equals(domainName)) {

  169. cookie.setDomain(domainName);

  170. }

  171. }

  172. cookie.setPath;

  173. response.addCookie;

  174. } catch (Exception e) {

  175. e.printStackTrace();

  176. }

  177. }

  178. /**

  179. * 得到cookie的域名

  180. */

  181. private static final String getDomainName(HttpServletRequest
    request) {

  182. String domainName = null;

  183. String serverName = request.getRequestURL().toString();

  184. if (serverName == null || serverName.equals {

  185. domainName = “”;

  186. } else {

  187. serverName = serverName.toLowerCase();

  188. serverName = serverName.substring;

  189. final int end = serverName.indexOf;

  190. serverName = serverName.substring;

  191. final String[] domains = serverName.split;

  192. int len = domains.length;

  193. if (len > 3) {

  194. // www.xxx.com.cn

  195. domainName = “.” + domains[len – 3] + “.” + domains[len – 2] +
    “.” + domains[len – 1];

  196. } else if (len <= 3 && len > 1) {

  197. // xxx.com or xxx.cn

  198. domainName = “.” + domains[len – 2] + “.” + domains[len – 1];

  199. } else {

  200. domainName = serverName;

  201. }

  202. }

  203. if (domainName != null && domainName.indexOf > 0) {

  204. String[] ary = domainName.split;

  205. domainName = ary[0];

  206. }

  207. return domainName;

  208. }

  209. }

3.HttpClientUtil1. package com.taotao.utils;

  1. import java.io.IOException;

  2. import java.net.URI;

  3. import java.util.ArrayList;

  4. import java.util.List;

  5. import java.util.Map;

  6. import org.apache.http.NameValuePair;

  7. import org.apache.http.client.entity.UrlEncodedFormEntity;

  8. import org.apache.http.client.methods.CloseableHttpResponse;

  9. import org.apache.http.client.methods.HttpGet;

  10. import org.apache.http.client.methods.HttpPost;

  11. import org.apache.http.client.utils.URIBuilder;

  12. import org.apache.http.entity.ContentType;

  13. import org.apache.http.entity.StringEntity;

  14. import org.apache.http.impl.client.CloseableHttpClient;

  15. import org.apache.http.impl.client.HttpClients;

  16. import org.apache.http.message.BasicNameValuePair;

  17. import org.apache.http.util.EntityUtils;

  18. public class HttpClientUtil {

  19. public static String doGet(String url, Map<String, String>
    param) {

  20. // 创建Httpclient对象

  21. CloseableHttpClient httpclient = HttpClients.createDefault();

  22. String resultString = “”;

  23. CloseableHttpResponse response = null;

  24. try {

  25. // 创建uri

  26. URIBuilder builder = new URIBuilder;

  27. if (param != null) {

  28. for (String key : param.keySet {

  29. builder.addParameter(key, param.get;

  30. }

  31. }

  32. URI uri = builder.build();

  33. // 创建http GET请求

  34. HttpGet httpGet = new HttpGet;

  35. // 执行请求

  36. response = httpclient.execute;

  37. // 判断返回状态是否为200

  38. if (response.getStatusLine().getStatusCode {

  39. resultString = EntityUtils.toString(response.getEntity(), “UTF-8”);

  40. }

  41. } catch (Exception e) {

  42. e.printStackTrace();

  43. } finally {

  44. try {

  45. if (response != null) {

  46. response.close();

  47. }

  48. httpclient.close();

  49. } catch (IOException e) {

  50. e.printStackTrace();

  51. }

  52. }

  53. return resultString;

  54. }

  55. public static String doGet(String url) {

  56. return doGet(url, null);

  57. }

  58. public static String doPost(String url, Map<String, String>
    param) {

  59. // 创建Httpclient对象

  60. CloseableHttpClient httpClient = HttpClients.createDefault();

  61. CloseableHttpResponse response = null;

  62. String resultString = “”;

  63. try {

  64. // 创建Http Post请求

  65. HttpPost httpPost = new HttpPost;

  66. // 创建参数列表

  67. if (param != null) {

  68. List<NameValuePair> paramList = new ArrayList<>();

  69. for (String key : param.keySet {

  70. paramList.add(new BasicNameValuePair(key, param.get;

  71. }

  72. // 模拟表单

  73. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);

  74. httpPost.setEntity;

  75. }

  76. // 执行http请求

  77. response = httpClient.execute;

  78. resultString = EntityUtils.toString(response.getEntity(), “utf-8”);

  79. } catch (Exception e) {

  80. e.printStackTrace();

  81. } finally {

  82. try {

  83. response.close();

  84. } catch (IOException e) {

  85. // TODO Auto-generated catch block

  86. e.printStackTrace();

  87. }

  88. }

  89. return resultString;

  90. }

  91. public static String doPost(String url) {

  92. return doPost(url, null);

  93. }

  94. public static String doPostJson(String url, String json) {

  95. // 创建Httpclient对象

  96. CloseableHttpClient httpClient = HttpClients.createDefault();

  97. CloseableHttpResponse response = null;

  98. String resultString = “”;

  99. try {

  100. // 创建Http Post请求

  101. HttpPost httpPost = new HttpPost;

  102. // 创建请求内容

  103. StringEntity entity = new StringEntity(json,
    ContentType.APPLICATION_JSON);

  104. httpPost.setEntity;

  105. // 执行http请求

  106. response = httpClient.execute;

  107. resultString = EntityUtils.toString(response.getEntity(), “utf-8”);

  108. } catch (Exception e) {

  109. e.printStackTrace();

  110. } finally {

  111. try {

  112. response.close();

  113. } catch (IOException e) {

  114. // TODO Auto-generated catch block

  115. e.printStackTrace();

  116. }

  117. }

  118. return resultString;

  119. }

  120. }

4.FastDFSClient工具类1. package cn.itcast.fastdfs.cliennt;

  1. import org.csource.common.NameValuePair;

  2. import org.csource.fastdfs.ClientGlobal;

  3. import org.csource.fastdfs.StorageClient1;

  4. import org.csource.fastdfs.StorageServer;

  5. import org.csource.fastdfs.TrackerClient;

  6. import org.csource.fastdfs.TrackerServer;

  7. public class FastDFSClient {

  8. private TrackerClient trackerClient = null;

  9. private TrackerServer trackerServer = null;

  10. private StorageServer storageServer = null;

  11. private StorageClient1 storageClient = null;

  12. public FastDFSClient(String conf) throws Exception {

  13. if (conf.contains(“classpath:”)) {

  14. conf = conf.replace(“classpath:”,
    this.getClass().getResource.getPath;

  15. }

  16. ClientGlobal.init;

  17. trackerClient = new TrackerClient();

  18. trackerServer = trackerClient.getConnection();

  19. storageServer = null;

  20. storageClient = new StorageClient1(trackerServer, storageServer);

  21. }

  22. /**

  23. * 上传文件方法

  24. * <p>Title: uploadFile</p>

  25. * <p>Description: </p>

  26. * @param fileName 文件全路径

  27. * @param extName 文件扩展名,不包含

  28. * @param metas 文件扩展信息

  29. * @return

  30. * @throws Exception

  31. */

  32. public String uploadFile(String fileName, String extName,
    NameValuePair[] metas) throws Exception {

  33. String result = storageClient.upload_file1(fileName, extName,
    metas);

  34. return result;

  35. }

  36. public String uploadFile(String fileName) throws Exception {

  37. return uploadFile(fileName, null, null);

  38. }

  39. public String uploadFile(String fileName, String extName) throws
    Exception {

  40. return uploadFile(fileName, extName, null);

  41. }

  42. /**

  43. * 上传文件方法

  44. * <p>Title: uploadFile</p>

  45. * <p>Description: </p>

  46. * @param fileContent 文件的内容,字节数组

  47. * @param extName 文件扩展名

  48. * @param metas 文件扩展信息

  49. * @return

  50. * @throws Exception

  51. */

  52. public String uploadFile(byte[] fileContent, String extName,
    NameValuePair[] metas) throws Exception {

  53. String result = storageClient.upload_file1(fileContent, extName,
    metas);

  54. return result;

  55. }

  56. public String uploadFile(byte[] fileContent) throws Exception {

  57. return uploadFile(fileContent, null, null);

  58. }

  59. public String uploadFile(byte[] fileContent, String extName)
    throws Exception {

  60. return uploadFile(fileContent, extName, null);

  61. }

  62. }

  63. <span style=”font-size:14px;font-weight:normal;”>public class
    FastDFSTest {

  64. @Test

  65. public void testFileUpload() throws Exception {

  66. // 1、加载配置文件,配置文件中的内容就是tracker服务的地址。

6.
ClientGlobal.init(“D:/workspaces-itcast/term197/taotao-manager-web/src/main/resources/resource/client.conf”);

  1. // 2、创建一个TrackerClient对象。直接new一个。

  2. TrackerClient trackerClient = new TrackerClient();

  3. // 3、使用TrackerClient对象创建连接,获得一个TrackerServer对象。

  4. TrackerServer trackerServer = trackerClient.getConnection();

  5. // 4、创建一个StorageServer的引用,值为null

  6. StorageServer storageServer = null;

  7. //
    5、创建一个StorageClient对象,需要两个参数TrackerServer对象、StorageServer的引用

  8. StorageClient storageClient = new StorageClient(trackerServer,
    storageServer);

  9. // 6、使用StorageClient对象上传图片。

  10. //扩展名不带“.”

  11. String[] strings =
    storageClient.upload_file(“D:/Documents/Pictures/images/200811281555127886.jpg”,
    “jpg”, null);

  12. // 7、返回数组。包含组名和图片的路径。

  13. for (String string : strings) {

  14. System.out.println;

  15. }

  16. }

  17. }</span>

#
![image]()

5.获取异常的堆栈信息

  1. package com.taotao.utils;

  2. import java.io.PrintWriter;

  3. import java.io.StringWriter;

  4. public class ExceptionUtil {

  5. /**

  6. * 获取异常的堆栈信息

  7. *

  8. * @param t

  9. * @return

  10. */

  11. public static String getStackTrace(Throwable t) {

  12. StringWriter sw = new StringWriter();

  13. PrintWriter pw = new PrintWriter;

  14. try {

  15. t.printStackTrace;

  16. return sw.toString();

  17. } finally {

  18. pw.close();

  19. }

  20. }

  21. }

#6.easyUIDataGrid对象返回值1. package com.taotao.result;

  1. import java.util.List;

  2. /**

  3. * easyUIDataGrid对象返回值

  4. * <p>Title: EasyUIResult</p>

  5. * <p>Description: </p>

  6. * <p>Company: www.itcast.com</p>

  7. * @author 入云龙

  8. * @date 2015年7月21日下午4:12:52

  9. * @version 1.0

  10. */

  11. public class EasyUIResult {

  12. private Integer total;

  13. private List<?> rows;

  14. public EasyUIResult(Integer total, List<?> rows) {

  15. this.total = total;

  16. this.rows = rows;

  17. }

  18. public EasyUIResult(long total, List<?> rows) {

  19. this.total = total;

  20. this.rows = rows;

  21. }

  22. public Integer getTotal() {

  23. return total;

  24. }

  25. public void setTotal(Integer total) {

  26. this.total = total;

  27. }

  28. public List<?> getRows() {

  29. return rows;

  30. }

  31. public void setRows(List<?> rows) {

  32. this.rows = rows;

  33. }

  34. }

7.ftp上传下载工具类1. package com.taotao.utils;

  1. import java.io.File;

  2. import java.io.FileInputStream;

  3. import java.io.FileNotFoundException;

  4. import java.io.FileOutputStream;

  5. import java.io.IOException;

  6. import java.io.InputStream;

  7. import java.io.OutputStream;

  8. import org.apache.commons.net.ftp.FTP;

  9. import org.apache.commons.net.ftp.FTPClient;

  10. import org.apache.commons.net.ftp.FTPFile;

  11. import org.apache.commons.net.ftp.FTPReply;

  12. /**

  13. * ftp上传下载工具类

  14. * <p>Title: FtpUtil</p>

  15. * <p>Description: </p>

  16. * <p>Company: www.itcast.com</p>

  17. * @author 入云龙

  18. * @date 2015年7月29日下午8:11:51

  19. * @version 1.0

  20. */

  21. public class FtpUtil {

  22. /**

  23. * Description: 向FTP服务器上传文件

  24. * @param host FTP服务器hostname

  25. * @param port FTP服务器端口

  26. * @param username FTP登录账号

  27. * @param password FTP登录密码

  28. * @param basePath FTP服务器基础目录

  29. * @param filePath
    FTP服务器文件存放路径。例如分日期存放:/2015/01/01。文件的路径为basePath+filePath

  30. * @param filename 上传到FTP服务器上的文件名

  31. * @param input 输入流

  32. * @return 成功返回true,否则返回false

  33. */

  34. public static boolean uploadFile(String host, int port, String
    username, String password, String basePath,

  35. String filePath, String filename, InputStream input) {

  36. boolean result = false;

  37. FTPClient ftp = new FTPClient();

  38. try {

  39. int reply;

  40. ftp.connect(host, port);// 连接FTP服务器

  41. // 如果采用默认端口,可以使用ftp.connect的方式直接连接FTP服务器

  42. ftp.login(username, password);// 登录

  43. reply = ftp.getReplyCode();

  44. if (!FTPReply.isPositiveCompletion {

  45. ftp.disconnect();

  46. return result;

  47. }

  48. //切换到上传目录

  49. if (!ftp.changeWorkingDirectory(basePath+filePath)) {

  50. //如果目录不存在创建目录

  51. String[] dirs = filePath.split;

  52. String tempPath = basePath;

  53. for (String dir : dirs) {

  54. if (null == dir || “”.equals continue;

  55. tempPath += “/” + dir;

  56. if (!ftp.changeWorkingDirectory) {

  57. if (!ftp.makeDirectory) {

  58. return result;

  59. } else {

  60. ftp.changeWorkingDirectory;

  61. }

  62. }

  63. }

  64. }

  65. //设置上传文件的类型为二进制类型

  66. ftp.setFileType(FTP.BINARY_FILE_TYPE);

  67. //上传文件

  68. if (!ftp.storeFile(filename, input)) {

  69. return result;

  70. }

  71. input.close();

  72. ftp.logout();

  73. result = true;

  74. } catch (IOException e) {

  75. e.printStackTrace();

  76. } finally {

  77. if (ftp.isConnected {

  78. try {

  79. ftp.disconnect();

  80. } catch (IOException ioe) {

  81. }

  82. }

  83. }

  84. return result;

  85. }

  86. /**

  87. * Description: 从FTP服务器下载文件

  88. * @param host FTP服务器hostname

  89. * @param port FTP服务器端口

  90. * @param username FTP登录账号

  91. * @param password FTP登录密码

  92. * @param remotePath FTP服务器上的相对路径

  93. * @param fileName 要下载的文件名

  94. * @param localPath 下载后保存到本地的路径

  95. * @return

  96. */

  97. public static boolean downloadFile(String host, int port, String
    username, String password, String remotePath,

  98. String fileName, String localPath) {

  99. boolean result = false;

  100. FTPClient ftp = new FTPClient();

  101. try {

  102. int reply;

  103. ftp.connect(host, port);

  104. // 如果采用默认端口,可以使用ftp.connect的方式直接连接FTP服务器

  105. ftp.login(username, password);// 登录

  106. reply = ftp.getReplyCode();

  107. if (!FTPReply.isPositiveCompletion {

  108. ftp.disconnect();

  109. return result;

  110. }

  111. ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录

  112. FTPFile[] fs = ftp.listFiles();

  113. for (FTPFile ff : fs) {

  114. if (ff.getName().equals) {

  115. File localFile = new File(localPath + “/” + ff.getName;

  116. OutputStream is = new FileOutputStream(localFile);

  117. ftp.retrieveFile(ff.getName;

  118. is.close();

  119. }

  120. }

  121. ftp.logout();

  122. result = true;

  123. } catch (IOException e) {

  124. e.printStackTrace();

  125. } finally {

  126. if (ftp.isConnected {

  127. try {

  128. ftp.disconnect();

  129. } catch (IOException ioe) {

  130. }

  131. }

  132. }

  133. return result;

  134. }

  135. public static void main(String[] args) {

  136. try {

  137. FileInputStream in=new FileInputStream(new
    File(“D:\\temp\\image\\gaigeming.jpg”));

  138. boolean flag = uploadFile(“192.168.25.133”, 21, “ftpuser”,
    “ftpuser”, “/home/ftpuser/www/images”,”/2015/01/21″, “gaigeming.jpg”,
    in);

  139. System.out.println;

  140. } catch (FileNotFoundException e) {

  141. e.printStackTrace();

  142. }

  143. }

  144. }

8.各种id生成策略1. package com.taotao.utils;

  1. import java.util.Random;

  2. /**

  3. * 各种id生成策略

  4. * <p>Title: IDUtils</p>

  5. * <p>Description: </p>

  6. * @date 2015年7月22日下午2:32:10

  7. * @version 1.0

  8. */

  9. public class IDUtils {

  10. /**

  11. * 图片名生成

  12. */

  13. public static String genImageName() {

  14. //取当前时间的长整形值包含毫秒

  15. long millis = System.currentTimeMillis();

  16. //long millis = System.nanoTime();

  17. //加上三位随机数

  18. Random random = new Random();

  19. int end3 = random.nextInt;

  20. //如果不足三位前面补0

  21. String str = millis + String.format(“%03d”, end3);

  22. return str;

  23. }

  24. /**

  25. * 商品id生成

  26. */

  27. public static long genItemId() {

  28. //取当前时间的长整形值包含毫秒

  29. long millis = System.currentTimeMillis();

  30. //long millis = System.nanoTime();

  31. //加上两位随机数

  32. Random random = new Random();

  33. int end2 = random.nextInt;

  34. //如果不足两位前面补0

  35. String str = millis + String.format(“%02d”, end2);

  36. long id = new Long;

  37. return id;

  38. }

  39. public static void main(String[] args) {

  40. for(int i=0;i< 100;i++)

  41. System.out.println(genItemId;

  42. }

  43. }

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图