文件下载(仅作参考,因为是反编译过来的)

package com.chinasofti.ro.bizframework.core.utils;

import java.io.*;
import java.net.*;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Downloader
{

    public Downloader()
    {
    }

    private static String b(String s)
    {
        try
        {
            return URLEncoder.encode(s, "UTF-8");
        }
        catch(UnsupportedEncodingException unsupportedencodingexception)
        {
            return s;
        }
    }

    public static HttpServletResponse download(InputStream is, HttpServletResponse response)
    {
        return download(is, ((String) (null)), response);
    }

    public static void openOnline(InputStream is, String fileName, HttpServletResponse response)
    {
        if(is == null)
            return;
        BufferedInputStream bufferedinputstream = new BufferedInputStream(is);
        byte abyte0[] = new byte[1024];
        int i = 0;
        response.reset();
        URL url = new URL("file:///" + fileName);
        response.setContentType(url.openConnection().getContentType());
        response.setHeader("Content-Disposition", "inline; filename=" + b(fileName));
        javax.servlet.ServletOutputStream servletoutputstream = response.getOutputStream();
        while((i = bufferedinputstream.read(abyte0)) >= 0)
            servletoutputstream.write(abyte0, 0, i);
        bufferedinputstream.close();
        servletoutputstream.close();
        if(is != null)
            is.close();
        break MISSING_BLOCK_LABEL_168;
        Exception exception;
        exception;
        if(is != null)
            is.close();
        throw exception;
    }

    public static HttpServletResponse download(Reader reader, String filename, HttpServletResponse response)
    {
        if(filename == null || "".equals(filename))
            filename = "bizdownloadfile";
        if(reader == null)
            return response;
        BufferedInputStream bufferedinputstream = new BufferedInputStream(new ReaderInputStream(reader));
        response.reset();
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(b(filename)) + "; filename*=utf-8''" + new String(b(filename)));
        BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(response.getOutputStream());
        response.setContentType("application/octet-stream");
        byte abyte0[] = new byte[1024];
        boolean flag = false;
        int i;
        int j;
        for(j = 0; (i = bufferedinputstream.read(abyte0)) >= 0; j += i)
            bufferedoutputstream.write(abyte0, 0, i);

        bufferedinputstream.read(abyte0);
        bufferedinputstream.close();
        response.addHeader("Content-Length", "" + j);
        bufferedoutputstream.flush();
        bufferedoutputstream.close();
        if(reader != null)
            reader.close();
        break MISSING_BLOCK_LABEL_241;
        Exception exception;
        exception;
        if(reader != null)
            reader.close();
        throw exception;
        return response;
    }

    public static HttpServletResponse downloadUseCustomResponseHeader(InputStream is, HttpServletResponse response)
    {
        if(is == null)
            return response;
        BufferedInputStream bufferedinputstream = new BufferedInputStream(is);
        byte abyte0[] = new byte[1024];
        int i = 0;
        javax.servlet.ServletOutputStream servletoutputstream = response.getOutputStream();
        while((i = bufferedinputstream.read(abyte0)) >= 0)
            servletoutputstream.write(abyte0, 0, i);
        bufferedinputstream.close();
        servletoutputstream.close();
        if(is != null)
            is.close();
        break MISSING_BLOCK_LABEL_88;
        Exception exception;
        exception;
        if(is != null)
            is.close();
        throw exception;
        return response;
    }

    public static HttpServletResponse download(InputStream is, String filename, HttpServletResponse response)
    {
        if(filename == null || "".equals(filename))
            filename = "bizdownloadfile";
        if(is == null)
            return response;
        BufferedInputStream bufferedinputstream = new BufferedInputStream(is);
        byte abyte0[] = new byte[1024];
        int i = 0;
        response.reset();
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(b(filename)) + "; filename*=utf-8''" + new String(b(filename)));
        response.addHeader("Content-Length", "" + bufferedinputstream.available());
        response.setContentType("application/octet-stream");
        javax.servlet.ServletOutputStream servletoutputstream = response.getOutputStream();
        while((i = bufferedinputstream.read(abyte0)) >= 0)
            servletoutputstream.write(abyte0, 0, i);
        bufferedinputstream.close();
        servletoutputstream.close();
        if(is != null)
            is.close();
        break MISSING_BLOCK_LABEL_207;
        Exception exception;
        exception;
        if(is != null)
            is.close();
        throw exception;
        return response;
    }

    public static HttpServletResponse download(File file, HttpServletResponse response)
    {
        if(file == null)
            return response;
        else
            return download(((InputStream) (new FileInputStream(file))), file.getName(), response);
    }

    public static HttpServletResponse download(String filePath, HttpServletResponse response)
    {
        File file = new File(filePath);
        return download(file, response);
    }

    public static void download(String filePath, HttpServletResponse response, boolean isOnLine)
    {
        File file = new File(filePath);
        if(!file.exists())
        {
            response.sendError(404, "File not found:" + filePath);
            return;
        }
        if(isOnLine)
            openOnline(new FileInputStream(file), file.getName(), response);
        else
            download(((InputStream) (new FileInputStream(file))), file.getName(), response);
    }

    public static void downloadFromNet(String url, String savePath)
    {
        int i = 0;
        boolean flag = false;
        String s = url.substring(url.lastIndexOf("/"));
        URL url1 = new URL(url);
        try
        {
            URLConnection urlconnection = url1.openConnection();
            InputStream inputstream = urlconnection.getInputStream();
            FileOutputStream fileoutputstream = new FileOutputStream(savePath + s);
            byte abyte0[] = new byte[1204];
            int j;
            while((j = inputstream.read(abyte0)) != -1)
            {
                i += j;
                fileoutputstream.write(abyte0, 0, j);
            }
            fileoutputstream.close();
        }
        catch(FileNotFoundException filenotfoundexception)
        {
            logger.error(filenotfoundexception.getMessage());
        }
        catch(IOException ioexception)
        {
            logger.error(ioexception.getMessage());
        }
    }

    static Class a(String s)
    {
        try
        {
            return Class.forName(s);
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            throw (new NoClassDefFoundError()).initCause(classnotfoundexception);
        }
    }

    protected static Logger logger;
    public static final String DEFAULT_DOWNLOAD_FILENAME = "bizdownloadfile";

    static
    {
        logger = LoggerFactory.getLogger(com.chinasofti.ro.bizframework.core.utils.Downloader.class);
    }

    private class ReaderInputStream extends InputStream
    {

        public int read()
        {
            if(index >= length)
                fillBuffer();
            if(index >= length)
                return -1;
            else
                return 255 & buffer[index++];
        }

        protected void fillBuffer()
        {
            if(length < 0)
                return;
            int i = reader.read(chars);
            if(i < 0)
            {
                length = -1;
            } else
            {
                byteArrayOut.reset();
                writer.write(chars, 0, i);
                writer.flush();
                buffer = byteArrayOut.toByteArray();
                length = buffer.length;
                index = 0;
            }
        }

        public int read(byte data[], int off, int len)
        {
            if(index >= length)
                fillBuffer();
            if(index >= length)
            {
                return -1;
            } else
            {
                int i = Math.min(len, length - index);
                System.arraycopy(buffer, index, data, off, i);
                index += i;
                return i;
            }
        }

        public int available()
        {
            return index >= length ? ((int) (length < 0 || !reader.ready() ? 0 : 1)) : length - index;
        }

        public void close()
        {
            reader.close();
        }

        protected Reader reader;
        protected ByteArrayOutputStream byteArrayOut;
        protected Writer writer;
        protected char chars[];
        protected byte buffer[];
        protected int index;
        protected int length;

        public ReaderInputStream(Reader reader1)
        {
            reader = reader1;
            byteArrayOut = new ByteArrayOutputStream();
            writer = new OutputStreamWriter(byteArrayOut);
            chars = new char[1024];
        }

        public ReaderInputStream(Reader reader1, String s)
        {
            reader = reader1;
            byteArrayOut = new ByteArrayOutputStream();
            writer = new OutputStreamWriter(byteArrayOut, s);
            chars = new char[1024];
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值