您的位置:首页 > 其它

文件下载,上传,压缩,解压

2016-10-20 10:56 519 查看
package com.qb.chedai.server.util;

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.net.URL;

import java.util.Date;

import java.util.Enumeration;

import java.util.Map;

import org.apache.http.Header;

import org.apache.http.HttpEntity;

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

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

import org.apache.http.entity.mime.MultipartEntityBuilder;

import org.apache.http.entity.mime.content.FileBody;

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

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

import org.apache.http.util.EntityUtils;

import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipFile;

import org.apache.tools.zip.ZipOutputStream;

/**

 * 文件处理器,主要处理的功能有: 

 * 1.向文件服务器上传文件 

 * 2.从文件服务器下载文件 

 * 3.压缩文件 

 * 4.解压缩文件

 * 5.删除目录下的所有文件及文件夹

 * 

 * @author qianll

 *

 */

/**

 * 压缩依赖的jar包

 *                 <!-- CKFinder begin -->

        <dependency>

            <groupId>net.coobird</groupId>

            <artifactId>thumbnailator</artifactId>

            <version>0.4.2</version>

        </dependency>

        <dependency>

            <groupId>com.ckfinder</groupId>

            <artifactId>apache-ant-zip</artifactId>

            <version>2.3</version>

        </dependency>

        <dependency>

            <groupId>com.ckfinder</groupId>

            <artifactId>ckfinder</artifactId>

            <version>2.3</version>

        </dependency>

        <dependency>

            <groupId>com.ckfinder</groupId>

            <artifactId>ckfinderplugin-fileeditor</artifactId>

            <version>2.3</version>

        </dependency>

        <dependency>

            <groupId>com.ckfinder</groupId>

            <artifactId>ckfinderplugin-imageresize</artifactId>

            <version>2.3</version>

        </dependency>

        <!-- CKFinder end -->

 *

 */

public class FileProcessor {

/**
* The default buffer size to use.
*/
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

//private static String servicePicUploadURL = Global.getConfig("chedai.server.url") + "/basicservice/v1/weed";
private static String servicePicUploadURL = "http://192.168.1.182:10982"+"/basicservice/v1/weed";

/**
* 递归删除文件夹(或文件)

* @param path
*/
public static void deleteAllFilesOfDir(File path) {
if (!path.exists())
return;
if (path.isFile()) {
path.delete();
return;
}
File[] files = path.listFiles();
for (int i = 0; i < files.length; i++) {
deleteAllFilesOfDir(files[i]);
}
path.delete();
}

/**
* 从文件服务器下载文件

* @param url
* @param file
*/
public static void downloadFromUrl(String url, File file) {
InputStream input = null;
FileOutputStream output = null;
try {
URL httpurl = new URL(url);
input = httpurl.openStream();
output = openOutputStream(file, false);
copy(input, output);
} catch (Exception e) {
throw new RuntimeException("图片下载失败,url:"+url, e);
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException ioe) {
// ignore
}
try {
if (output != null) {
output.close();
}
} catch (IOException ioe) {
// ignore
}
}
}

    /**

     * 压缩文件或目录

     *

     * @param srcDirName   压缩的根目录

     * @param fileName     根目录下的待压缩的文件名或文件夹名,其中*或""表示跟目录下的全部文件

     * @param descFileName 目标zip文件

     */

    public static void zipFiles(String srcDirName, String fileName, String descFileName) {

        // 判断目录是否存在

        if (srcDirName == null) {

        throw new RuntimeException("文件压缩失败,目录 " + srcDirName + " 不存在!");

        }

        File fileDir = new File(srcDirName);

        if (!fileDir.exists() || !fileDir.isDirectory()) {

        throw new RuntimeException("文件压缩失败,目录 " + srcDirName + " 不存在!");

        }

        String dirPath = fileDir.getAbsolutePath();

        File descFile = new File(descFileName);

        ZipOutputStream zouts = null;

        try {

            zouts = new ZipOutputStream(new FileOutputStream(descFile));

            if ("*".equals(fileName) || "".equals(fileName)) {

                zipDirectoryToZipFile(dirPath, fileDir, zouts);

            } else {

                File file = new File(fileDir, fileName);

                if (file.isFile()) {

                    zipFilesToZipFile(dirPath, file, zouts);

                } else {

                    zipDirectoryToZipFile(dirPath, file, zouts);

                }

            }            

        } catch (Exception e) {

            throw new RuntimeException("压缩文件失败.",e);

        }finally{

        if(zouts != null){

        try {
zouts.close();
} catch (IOException e) {
e.printStackTrace();
}

        }

        }

    }

    

    /**

     * 解压缩ZIP文件,将ZIP文件里的内容解压到descFileName目录下

     *

     * @param zipFileName  需要解压的ZIP文件

     * @param descFileName 目标文件

     */

    public static boolean unZipFiles(String zipFileName, String descFileName) {

        String descFileNames = descFileName;

        if (!descFileNames.endsWith(File.separator)) {

            descFileNames = descFileNames + File.separator;

        }

        InputStream is = null;

        OutputStream os = null;

        try {

            // 根据ZIP文件创建ZipFile对象

            ZipFile zipFile = new ZipFile(zipFileName);

            ZipEntry entry = null;

            String entryName = null;

            String descFileDir = null;

            byte[] buf = new byte[4096];

            int readByte = 0;

            // 获取ZIP文件里所有的entry

            @SuppressWarnings("rawtypes") Enumeration enums = zipFile.getEntries();

            // 遍历所有entry

            while (enums.hasMoreElements()) {

                entry = (ZipEntry) enums.nextElement();

                // 获得entry的名字

                entryName = entry.getName();

                descFileDir = descFileNames + entryName;

                if (entry.isDirectory()) {

                    // 如果entry是一个目录,则创建目录

                    new File(descFileDir).mkdirs();

                    continue;

                } else {

                    // 如果entry是一个文件,则创建父目录

                    new File(descFileDir).getParentFile().mkdirs();

                }

                File file = new File(descFileDir);

                // 打开文件输出流

                os = new FileOutputStream(file);

                // 从ZipFile对象中打开entry的输入流

                is = zipFile.getInputStream(entry);

                while ((readByte = is.read(buf)) != -1) {

                    os.write(buf, 0, readByte);

                }

            }

            zipFile.close();

            return true;

        } catch (Exception e) {

        throw new RuntimeException("文件解压失败!",e);

        } finally{

        if(is != null){

        try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}

        }

        if(os != null){

        try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}

        }

        }

    }

    /**

     * 上传文件到文件服务器

     * @param file

     * @return

     */
public static String upload(File file) {
String app_key = Global.getConfig("apiPicUploadKey").trim();
String app_secret = Global.getConfig("apiPicUploadSecret").trim();
Long ts = new Date().getTime();
String method = "POST";
// 拼装字符串
String MD5Str = "app_key=" + app_key + "&app_secret=" + app_secret
+ "&method=" + method + "&ts=" + ts;

// MD5字符串
String sign = MD5Util.string2MD5(MD5Str.trim());
// 构建URL
String picUploadURL = servicePicUploadURL + "?app_key=" + app_key
+ "&method=" + method + "&sign=" + sign + "&ts=" + ts;

System.out.println(picUploadURL);
String fid = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost(picUploadURL.trim());
FileBody f1 = new FileBody(file);
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("f1", f1).build();
httppost.setEntity(reqEntity);
CloseableHttpResponse resp = httpclient.execute(httppost);
System.out.println(resp.getStatusLine());
for(Header head:resp.getAllHeaders()){
System.out.println(head.getName()+" : "+head.getValue());
}
HttpEntity resEntity = resp.getEntity();

System.out.println("Response content length: "
+ resEntity.getContentLength());
String ret = EntityUtils.toString(resEntity, "UTF-8");
System.out.println("返回内容:" + ret);
Map<String, Object> map = StringToMapUtil.jsonToObject(ret);
String result = (String) map.get("result");
result = result.replace("[", "").replace("]", "");
fid = (String) StringToMapUtil.jsonToObject(result).get("fid");

EntityUtils.consume(resEntity);

} catch (Exception e) {
throw new RuntimeException("上传zip包失败!",e);
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fid;
}

/**
* 获取文件名

* @param url
* @return
*/
private static String getFileNameFromUrl(String url) {
String name = new Long(System.currentTimeMillis()).toString() + ".X";
int index = url.lastIndexOf("/");
if (index > 0) {
name = url.substring(index + 1);
if (name.trim().length() > 0) {
return name;
}
}
return name;
}

/**
* 打开文件输出流

* @param file
* @param append
* @return
* @throws IOException
*/
private static FileOutputStream openOutputStream(File file, boolean append)
throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file
+ "' exists but is a directory");
}
if (file.canWrite() == false) {
throw new IOException("File '" + file
+ "' cannot be written to");
}
} else {
File parent = file.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent
+ "' could not be created");
}
}
}
return new FileOutputStream(file, append);
}

// copy from InputStream
// -----------------------------------------------------------------------
/**
* Copy bytes from an <code>InputStream</code> to an
* <code>OutputStream</code>.
* <p>
* This method buffers the input internally, so there is no need to use a
* <code>BufferedInputStream</code>.
* <p>
* Large streams (over 2GB) will return a bytes copied value of
* <code>-1</code> after the copy has completed since the correct number of
* bytes cannot be returned as an int. For large streams use the
* <code>copyLarge(InputStream, OutputStream)</code> method.

* @param input
*            the <code>InputStream</code> to read from
* @param output
*            the <code>OutputStream</code> to write to
* @return the number of bytes copied
* @throws NullPointerException
*             if the input or output is null
* @throws IOException
*             if an I/O error occurs
* @throws ArithmeticException
*             if the byte count is too large
* @since Commons IO 1.1
*/
private static int copy(InputStream input, OutputStream output)
throws IOException {
long count = copyLarge(input, output);
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int) count;
}

/**
* Copy bytes from a large (over 2GB) <code>InputStream</code> to an
* <code>OutputStream</code>.
* <p>
* This method buffers the input internally, so there is no need to use a
* <code>BufferedInputStream</code>.

* @param input
*            the <code>InputStream</code> to read from
* @param output
*            the <code>OutputStream</code> to write to
* @return the number of bytes copied
* @throws NullPointerException
*             if the input or output is null
* @throws IOException
*             if an I/O error occurs
* @since Commons IO 1.3
*/
private static long copyLarge(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}

    /**

     * 将目录压缩到ZIP输出流

     *

     * @param dirPath 目录路径

     * @param fileDir 文件信息

     * @param zouts   输出流

     */

    private static void zipDirectoryToZipFile(String dirPath, File fileDir, ZipOutputStream zouts) {

        if (fileDir.isDirectory()) {

            File[] files = fileDir.listFiles();

            // 空的文件夹

            if(files!=null){

            if (files.length == 0) {

                // 目录信息

                ZipEntry entry = new ZipEntry(getEntryName(dirPath, fileDir));

                try {
                    zouts.putNextEntry(entry);

    zouts.setEncoding("GBK"); //解决linux下中文乱码问题,使用org.apache.tools的压缩jar包

                    zouts.closeEntry();

                } catch (Exception e) {

                    throw new RuntimeException(e);

                }

                return;

            }

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

                if (files[i].isFile()) {

                    // 如果是文件,则调用文件压缩方法

                    zipFilesToZipFile(dirPath, files[i], zouts);

                } else {

                    // 如果是目录,则递归调用

                    zipDirectoryToZipFile(dirPath, files[i], zouts);

                }

            }

            }

        }

    }

    /**

     * 将文件压缩到ZIP输出流

     *

     * @param dirPath 目录路径

     * @param file    文件

     * @param zouts   输出流

     */

    private static void zipFilesToZipFile(String dirPath, File file, ZipOutputStream zouts) {

        FileInputStream fin = null;

        ZipEntry entry = null;

        // 创建复制缓冲区

        byte[] buf = new byte[4096];

        int readByte = 0;

        if (file.isFile()) {

            try {

                // 创建一个文件输入流

                fin = new FileInputStream(file);

                // 创建一个ZipEntry

                entry = new ZipEntry(getEntryName(dirPath, file));

                // 存储信息到压缩文件

                zouts.putNextEntry(entry);

                // 复制字节到压缩文件

                while ((readByte = fin.read(buf)) != -1) {

                    zouts.write(buf, 0, readByte);
                }

                zouts.setEncoding("GBK"); //解决linux下中文乱码问题,使用org.apache.tools的压缩jar包

                zouts.closeEntry();

                //System.out.println("添加文件 " + file.getAbsolutePath() + " 到zip文件中!");

            } catch (Exception e) {

                e.printStackTrace();

            }finally{

            if(fin != null){

            try{

            fin.close();

            }catch(Exception e){

            //ignore;

            }

            }

            }

        }

    }

    

    /**

     * 获取待压缩文件在ZIP文件中entry的名字,即相对于跟目录的相对路径名

     *

     * @param dirPat 目录名

     * @param file   entry文件名

     * @return

     */

    private static String getEntryName(String dirPath, File file) {

        String dirPaths = dirPath;

        if (!dirPaths.endsWith(File.separator)) {

            dirPaths = dirPaths + File.separator;

        }

        String filePath = file.getAbsolutePath();

        // 对于目录,必须在entry名字后面加上"/",表示它将以目录项存储

        if (file.isDirectory()) {

            filePath += "/";

        }

        int index = filePath.indexOf(dirPaths);

        return filePath.substring(index + dirPaths.length());

    }

    

    public static void main(String[] args){

    File file = new File("D:\\car-credit\\chedai_server_dev02\\2201.zip");

    upload(file);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: