123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package com.yihu.jw.utils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.*;
- import java.lang.reflect.Method;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.nio.MappedByteBuffer;
- import java.security.AccessController;
- import java.security.PrivilegedAction;
- public class FileUtil {
- private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
- /**
- * 在MappedByteBuffer释放后再对它进行读操作的话就会引发jvm crash,在并发情况下很容易发生
- * 正在释放时另一个线程正开始读取,于是crash就发生了。所以为了系统稳定性释放前一般需要检 查是否还有线程在读或写
- * @param mappedByteBuffer
- */
- public static void freedMappedByteBuffer(final MappedByteBuffer mappedByteBuffer) {
- try {
- if (mappedByteBuffer == null) {
- return;
- }
- mappedByteBuffer.force();
- AccessController.doPrivileged(new PrivilegedAction<Object>() {
- @Override
- public Object run() {
- try {
- Method getCleanerMethod = mappedByteBuffer.getClass().getMethod("cleaner", new Class[0]);
- //可以访问private的权限
- getCleanerMethod.setAccessible(true);
- //在具有指定参数的 方法对象上调用此 方法对象表示的底层方法
- sun.misc.Cleaner cleaner = (sun.misc.Cleaner) getCleanerMethod.invoke(mappedByteBuffer,
- new Object[0]);
- cleaner.clean();
- } catch (Exception e) {
- logger.error("clean MappedByteBuffer error!!!", e);
- }
- logger.info("clean MappedByteBuffer completed!!!");
- return null;
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * @从制定URL下载文件并保存到指定目录
- * @param filePath 文件将要保存的目录
- * @param method 请求方法,包括POST和GET
- * @param url 请求的路径
- * @return
- */
- public static File saveUrlAs(String url, String filePath, String method,String fileName){
- //System.out.println("fileName---->"+filePath);
- //创建不同的文件夹目录
- File file=new File(filePath);
- //判断文件夹是否存在
- if (!file.exists())
- {
- //如果文件夹不存在,则创建新的的文件夹
- file.mkdirs();
- }
- FileOutputStream fileOut = null;
- HttpURLConnection conn = null;
- InputStream inputStream = null;
- try
- {
- // 建立链接
- URL httpUrl=new URL(url);
- conn=(HttpURLConnection) httpUrl.openConnection();
- //以Post方式提交表单,默认get方式
- conn.setRequestMethod(method);
- conn.setDoInput(true);
- conn.setDoOutput(true);
- // post方式不能使用缓存
- conn.setUseCaches(false);
- //连接指定的资源
- conn.connect();
- //获取网络输入流
- inputStream=conn.getInputStream();
- BufferedInputStream bis = new BufferedInputStream(inputStream);
- //判断文件的保存路径后面是否以/结尾
- if (!filePath.endsWith("/")) {
- filePath += "/";
- }
- //写入到文件(注意文件保存路径的后面一定要加上文件的名称)
- fileOut = new FileOutputStream(filePath+fileName);
- BufferedOutputStream bos = new BufferedOutputStream(fileOut);
- byte[] buf = new byte[4096];
- int length = bis.read(buf);
- //保存文件
- while(length != -1)
- {
- bos.write(buf, 0, length);
- length = bis.read(buf);
- }
- bos.close();
- bis.close();
- conn.disconnect();
- } catch (Exception e)
- {
- e.printStackTrace();
- System.out.println("抛出异常!!");
- }
- return file;
- }
- }
|