Java通过url远程地址下载文件工具类

package com.baiwang.media.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DownFileUtil {

    private static final Logger logger = LoggerFactory.getLogger(DownFileUtil.class);  
    
    
    //根据文件链接把文件下载下来并且转成字节码  
    public static void getImageFromURL(String returnName,String urlPath,HttpServletResponse response) {  
        InputStream inputStream = null;  
        OutputStream outputStream = null;  
        HttpURLConnection conn = null;  
        try {  
            URL url = new URL(urlPath);  
            conn = (HttpURLConnection) url.openConnection();  
            conn.setDoInput(true);  
            // conn.setDoOutput(true);  
            conn.setRequestMethod("GET");  
            conn.setConnectTimeout(6000);  
            inputStream = conn.getInputStream();             
            outputStream = response.getOutputStream();
			response.reset();
			//设置响应类型	PDF文件为"application/pdf",WORD文件为:"application/msword", EXCEL文件为:"application/vnd.ms-excel"。  
			response.setContentType("application/octet-stream;charset=utf-8");
 
			//设置响应的文件名称,并转换成中文编码
			//returnName = URLEncoder.encode(returnName,"UTF-8");
			returnName = response.encodeURL(new String(returnName.getBytes(),"iso8859-1"));	//保存的文件名,必须和页面编码一致,否则乱码
			
			//attachment作为附件下载;inline客户端机器有安装匹配程序,则直接打开;注意改变配置,清除缓存,否则可能不能看到效果
			response.addHeader("Content-Disposition",   "attachment;filename="+returnName);  
			
			//将文件读入响应流
			outputStream = response.getOutputStream();
			int length = 1024;
			int readLength=0;
			byte buf[] = new byte[1024];
			readLength = inputStream.read(buf, 0, length);
			while (readLength != -1) {
				outputStream.write(buf, 0, readLength);
				readLength = inputStream.read(buf, 0, length);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				outputStream.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				outputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}

    }  
  
  
    public static byte[] readInputStream(InputStream is) {  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        byte[] buffer = new byte[1024];  
        int length = -1;  
        try {  
            while ((length = is.read(buffer)) != -1) {  
                baos.write(buffer, 0, length);  
            }  
            baos.flush();  
        } catch (IOException e) {  
            logger.error("IOException", e);  
        }  
        byte[] data = baos.toByteArray();  
        try {  
            is.close();  
            baos.close();  
        } catch (IOException e) {  
            logger.error("IOException", e);  
        }  
        return data;  
    }  
}
发布了69 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44407691/article/details/104380094