FileUtil.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package com.yihu.jw.utils;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import java.io.*;
  5. import java.lang.reflect.Method;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.nio.MappedByteBuffer;
  9. import java.security.AccessController;
  10. import java.security.PrivilegedAction;
  11. public class FileUtil {
  12. private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
  13. /**
  14. * 在MappedByteBuffer释放后再对它进行读操作的话就会引发jvm crash,在并发情况下很容易发生
  15. * 正在释放时另一个线程正开始读取,于是crash就发生了。所以为了系统稳定性释放前一般需要检 查是否还有线程在读或写
  16. * @param mappedByteBuffer
  17. */
  18. public static void freedMappedByteBuffer(final MappedByteBuffer mappedByteBuffer) {
  19. try {
  20. if (mappedByteBuffer == null) {
  21. return;
  22. }
  23. mappedByteBuffer.force();
  24. AccessController.doPrivileged(new PrivilegedAction<Object>() {
  25. @Override
  26. public Object run() {
  27. try {
  28. Method getCleanerMethod = mappedByteBuffer.getClass().getMethod("cleaner", new Class[0]);
  29. //可以访问private的权限
  30. getCleanerMethod.setAccessible(true);
  31. //在具有指定参数的 方法对象上调用此 方法对象表示的底层方法
  32. sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(mappedByteBuffer,
  33. new Object[0]);
  34. cleaner.clean();
  35. } catch (Exception e) {
  36. logger.error("clean MappedByteBuffer error!!!", e);
  37. }
  38. logger.info("clean MappedByteBuffer completed!!!");
  39. return null;
  40. }
  41. });
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. /**
  47. * @从制定URL下载文件并保存到指定目录
  48. * @param filePath 文件将要保存的目录
  49. * @param method 请求方法,包括POST和GET
  50. * @param url 请求的路径
  51. * @return
  52. */
  53. public static File saveUrlAs(String url, String filePath, String method,String fileName){
  54. //System.out.println("fileName---->"+filePath);
  55. //创建不同的文件夹目录
  56. File file=new File(filePath);
  57. //判断文件夹是否存在
  58. if (!file.exists())
  59. {
  60. //如果文件夹不存在,则创建新的的文件夹
  61. file.mkdirs();
  62. }
  63. FileOutputStream fileOut = null;
  64. HttpURLConnection conn = null;
  65. InputStream inputStream = null;
  66. try
  67. {
  68. // 建立链接
  69. URL httpUrl=new URL(url);
  70. conn=(HttpURLConnection) httpUrl.openConnection();
  71. //以Post方式提交表单,默认get方式
  72. conn.setRequestMethod(method);
  73. conn.setDoInput(true);
  74. conn.setDoOutput(true);
  75. // post方式不能使用缓存
  76. conn.setUseCaches(false);
  77. //连接指定的资源
  78. conn.connect();
  79. //获取网络输入流
  80. inputStream=conn.getInputStream();
  81. BufferedInputStream bis = new BufferedInputStream(inputStream);
  82. //判断文件的保存路径后面是否以/结尾
  83. if (!filePath.endsWith("/")) {
  84. filePath += "/";
  85. }
  86. //写入到文件(注意文件保存路径的后面一定要加上文件的名称)
  87. fileOut = new FileOutputStream(filePath+fileName);
  88. BufferedOutputStream bos = new BufferedOutputStream(fileOut);
  89. byte[] buf = new byte[4096];
  90. int length = bis.read(buf);
  91. //保存文件
  92. while(length != -1)
  93. {
  94. bos.write(buf, 0, length);
  95. length = bis.read(buf);
  96. }
  97. bos.close();
  98. bis.close();
  99. conn.disconnect();
  100. } catch (Exception e)
  101. {
  102. e.printStackTrace();
  103. System.out.println("抛出异常!!");
  104. }
  105. return file;
  106. }
  107. }