Java-从url获取文件信息,压缩并导出

Java-从url获取文件信息,压缩并导出

本文主要用来从url获取文件输入流,并且将其加入zip的输出流并导出

环境
jfinal
tomcat
jdk1.7

jfinal并不是官网标准的框架。

目录


1从url获取文件信息

文件1:GetFile.java
结构:

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class GetFile {
    //1.1从url获取文件的输入流
    public InputStream getFileInputStream(String url) throws Exception{}
    //1.2从url获取文件名
    public String getFileName(String url) throws IOException{}
}

1.1从url获取文件的输入流

public InputStream getFileInputStream(String url) throws Exception{
        InputStream murl = new URL(url).openStream();
        return murl;
    }

1.2从url获取文件名

public String getFileName(String url) throws IOException {
        String filename = "";
        boolean isok = false;
        // 从UrlConnection中获取文件名称
        try {
            URL myURL = new URL(url);
            URLConnection conn = myURL.openConnection();
            if (conn == null) {
                return null;
            }
            Map<String, List<String>> hf = conn.getHeaderFields();
            if (hf == null) {
                return null;
            }
            Set<String> key = hf.keySet();
            if (key == null) {
                return null;
            }
            for (String skey : key) {
                List<String> values = hf.get(skey);
                for (String value : values) {
                    String result;
                    try {
                        result = new String(value.getBytes("ISO-8859-1"), "GBK");
                        int location = result.indexOf("filename");
                        if (location >= 0) {
                            result = result.substring(location
                                    + "filename".length());
                            filename = result
                                    .substring(result.indexOf("=") + 1);
                            isok = true;
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }// ISO-8859-1 UTF-8 gb2312
                }
                if (isok) {
                    break;
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return filename;
    }

2输出

文件2:ZipController.java
结构:

import com.jfinal.core.Controller;
import org.apache.commons.codec.binary.Base64;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;


public class ZipController extends Controller {
    //2.1
    public void index() throws IOException {}
    //2.2
    public void zipAction(String url,InputStream is,ZipOutputStream zos,GetFile gf,byte[] buffer) throws Exception{}
    //2.3
    public String encodeFileName(HttpServletRequest request, String fileName) throws UnsupportedEncodingException{}
}

2.1 index()–业务处理,压缩,导出。

public void index() throws IOException {
        String fileName = "List";
        HttpServletResponse response = getResponse();
        HttpServletRequest request = getRequest();
        response.setHeader("Connection", "close");
        response.setHeader("Content-Type", "application/vnd.ms-excel;charset=UTF-8");
        String filename = System.currentTimeMillis() + fileName+".zip";
        filename = encodeFileName(request, filename);
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        OutputStream out = null;
        out = response.getOutputStream();

        try {
            // create byte buffer
            byte[] buffer = new byte[1024];
            ZipOutputStream zos = new ZipOutputStream(out);
            GetFile gf = new GetFile();
            InputStream is= null;

            String url1 = "http://xxxxxxxxxxx";
            String url2 = "http://xxxxxxxxxxx";
            zipAction(url1,is,zos,gf,buffer);
            zipAction(url2,is,zos,gf,buffer);

            zos.closeEntry();
            zos.close();
            is.close();
        }catch (Exception ioe) {
            System.out.println("Error creating zip file" + ioe);
        }
    }

2.2 zipAction()–处理各种流,压缩具体实现

    public void zipAction(String url,InputStream is,ZipOutputStream zos,GetFile gf,byte[] buffer) throws Exception{
        is = gf.getFileInputStream(url);
        String fileName1 = gf.getFileName(url);
        zos.putNextEntry(new ZipEntry(fileName1));
        int length;
        while ((length = is.read(buffer)) > 0) {
            zos.write(buffer, 0, length);
        }
    }

2.3 encodeFileName()–文件名处理

    public String encodeFileName(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
        String agent = request.getHeader("USER-AGENT");
        if (null != agent && -1 != agent.indexOf("MSIE")) {
            return URLEncoder.encode(fileName, "UTF-8");
        } else if (null != agent && -1 != agent.indexOf("Mozilla")) {
            return "=?UTF-8?B?"+ (new String(Base64.encodeBase64(fileName.getBytes("UTF-8")))) + "?=";
        } else {
            return fileName;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值