把网上图片下载到本地的java工具类
package com.swift; import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class DownloadPicUtil { public static final int DEF_CONN_TIMEOUT = 30000;
public static final int DEF_READ_TIMEOUT = 30000;
public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36"; public static void downloadPic(String strUrl,String savePath) throws Exception {
HttpURLConnection conn = null;
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("User-agent", userAgent);
conn.setUseCaches(false);
conn.setConnectTimeout(DEF_CONN_TIMEOUT);
conn.setReadTimeout(DEF_READ_TIMEOUT);
conn.setInstanceFollowRedirects(false);
conn.connect();
InputStream is = conn.getInputStream();//连接得到输入流内容
File file=new File(savePath);
if(!file.exists()) {//保存文件夹不存在则建立
file.mkdirs();
}
FileOutputStream fos=new FileOutputStream(new File(savePath,strUrl.substring(strUrl.lastIndexOf("/")+1)));//获取网址中的图片名
int intRead = 0;
byte[] buf=new byte[1024*2];//生成2K 大小的容器用于接收图片字节流
while ((intRead = is.read(buf)) != -1) {
fos.write(buf,0,intRead);//文件输出流到指定路径文件
fos.flush();
}
fos.close();//关闭输出流
if (conn != null) {
conn.disconnect();//关闭连接
}
}
}
上边这个不好看懂,重写了一个
package com.swift.servlet; import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder; import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import sun.misc.BASE64Encoder; /**
* Servlet implementation class DownServletYangyan
*/
public class DownServletYangyan extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path=this.getServletContext().getRealPath("download/美女.jpg");//这个是通过项目中的地址得到实际存放在地址
String mime=this.getServletContext().getMimeType(path);//这个只是知道一下是什么类型,没用
String filename="美女.jpg";//这个是下载时显示的下载名
String agent=request.getHeader("User-Agent");
if(agent.contains("MSIE")) {
filename = URLEncoder.encode(filename, "utf-8");
filename = filename.replace("+", " ");
}else if(agent.contains("firefox")) {
BASE64Encoder base64Encoder = new BASE64Encoder();
filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
}else {
filename = URLEncoder.encode(filename, "utf-8");
} response.setHeader("Content-Disposition", "attachment;filename="+filename);//告诉浏览器用附件法打开
FileInputStream fis=new FileInputStream(path);
ServletOutputStream out=response.getOutputStream();//servlet的响应输出流送回
byte[] buf=new byte[1024];
int len;
while((len=fis.read(buf))!=-1) {
out.write(buf, 0, len);
}
fis.close();
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }
把网上图片下载到本地的java工具类的更多相关文章
- java工具类系列 (四.SerializationUtils)
java工具类系列 (四.SerializationUtils) SerializationUtils该类为序列化工具类,也是lang包下的工具,主要用于序列化操作 import java.io.Se ...
- Java工具类——通过配置XML验证Map
Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...
- 排名前 16 的 Java 工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- 排名前16的Java工具类
原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...
- 第一章 Java工具类目录
在这一系列博客中,主要是记录在实际开发中会常用的一些Java工具类,方便后续开发中使用. 以下的目录会随着后边具体工具类的添加而改变. 浮点数精确计算 第二章 Java浮点数精确计算 crc32将任意 ...
- java工具类之按对象中某属性排序
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang ...
- 干货:排名前16的Java工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- Java工具类:给程序增加版权信息
我们九天鸟的p2p网贷系统,基本算是开发完成了. 现在,想给后端的Java代码,增加版权信息. 手动去copy-paste,太没有技术含量. 于是,写了个Java工具类,给Java源文件 ...
- 常用高效 Java 工具类总结
一.前言 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码 ...
随机推荐
- day04 ---Linux安装Python3
如何linux上安装python3 1.下载源代码,方式有2个, 1.在windows上下载,下载完成后,通过lrzsz工具,或者xftp工具,传输到linux服务器中 2.在linux中直接下载 c ...
- 7、kvm迁移操作
虚拟机迁移要确保虚拟机是关机状态. virsh shutdown privi-server virsh dumpxml privi-server > /etc/libvirt/qemu/priv ...
- list倒序删除
public static void main(String[] args) { List<Integer> nums = new ArrayList<Integer>(); ...
- python3+Appium自动化02-Capability配置
基本参数 参数 描述 实例 automationName 自动化测试引擎 Appium或 Selendroid platformName 手机操作系统 iOS, Android, 或 FirefoxO ...
- 牛客网Java刷题知识点之基本类型、引用类型
不多说,直接上干货! byte-short-int-long,方便识记.
- Hadoop实战:微博数据分析
项目需求 自定义输入格式,将明星微博数据排序后按粉丝数 关注数 微博数 分别输出到不同文件中. 数据集 下面是部分数据,猛戳此链接下载完整数据集 数据格式: 明星 明星微博名称 粉丝数 ...
- 5 - 参考函数-API
5.1 鼠标管理 a). MouseClick 点击鼠标 MouseClick ( "button" [, x, y [, clicks = 1 [, speed = 10]]] ...
- ubuntu 无法应用原保存的显示器配置
打开ubuntu之后的开启页面出现: 所选模式均不匹配可能的模式: 为 CRTC 63 尝试模式 CRTC 63:尝试 800x600@60Hz 模式输出在 1366x768@60Hz (通过 0) ...
- vue 导出excel
1.安装三个依赖包 npm install -S file-saver npm install -S xlsx npm install -D script-loader 2.在项目中创建一个文件夹(比 ...
- springboot 学习笔记(六)
(六)springboot整合activemq 1.现下载activemq,下载链接:http://activemq.apache.org/download.html,windows系统解压后进入bi ...