URL地址下载图片到本地
package test.dao; import eh.base.dao.DoctorDAO;
import eh.entity.base.Doctor;
import junit.framework.TestCase;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils; import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List; /**
* Created by houxr on 2016/5/16.
*/
public class DownloadJPGFromUrlTest extends TestCase { private static ClassPathXmlApplicationContext appContext; static {
appContext = new ClassPathXmlApplicationContext("test/spring.xml");
} private static DoctorDAO dao = appContext.getBean("doctorDAO", DoctorDAO.class); /**
* 根据手机号码获取 医生二维码图片
*/
public void testGetTicketAndUrlByDoctorId() {
String s="13957120890,13858060709,13958166572," +
"13758143571,13805752614,13867469261," +
"13957178822,13868101010,18758205727";
List<String> list = new ArrayList<String>();
Doctor doctor=null;
String[] newstr = s.split(",");
for(int i =0;i<newstr.length;i++){
list.add(newstr[i]);
}
List<Doctor> doctorList=new ArrayList<Doctor>();
for(int j=0;j<list.size();j++){
doctor=dao.getByMobile(list.get(j));
dao.getTicketAndUrlByDoctorId(doctor.getDoctorId());
//doctorList.add(doctor);
System.out.println(doctor.getName());
}
System.out.println("====二维码生成end===="+doctorList.size());
} /**
* 下载文件到本地
* @param urlString 被下载的文件地址
* @param filename 本地文件名
* @throws Exception 各种异常
*/
public static void download(String urlString, String filename) throws Exception {
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
OutputStream os = new FileOutputStream(filename);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
} public static void getWxDoctorPhoto(String mobile) {
try{
if(!StringUtils.isEmpty(mobile)) {
Doctor doctor = dao.getByMobile(mobile);
//从图片服务器上下载图片
download("http://url/upload/" + doctor.getQrCode(),
"E:/wxphoto/" + doctor.getName() + "_" + doctor.getMobile() + ".jpg");
}
}catch (Exception e){
e.printStackTrace();
}
} public static void main(String[] args) {
getWxDoctorPhoto("15268293359");
} }
URL地址下载图片到本地的更多相关文章
- java根据图片的url地址下载图片到本地
package com.daojia.haobo.aicircle.util; import sun.misc.BASE64Encoder; import java.io.*; import java ...
- QTP 通过URL地址下载文件到本地(转)
While automation, you may come to situations where you need to need to download a file on clicking a ...
- 通过scrapy内置的ImagePipeline下载图片到本地、并提取本地保存地址
1.通过scrapy内置的ImagePipeline下载图片到本地 2.获取图片保存本地的地址 1.通过scrapy内置的ImagePipeline下载图片到本地 1)在settings.py中打开 ...
- scrapy中的ImagePipeline下载图片到本地、并提取本地的保存地址
通过scrapy内置到ImagePipeline下载图片到本地 在settings中打开 ITEM_PIPELINES的注释,并在这里面加入 'scrapy.pipelines.images.Imag ...
- QT通过url下载图片到本地
/* strUrl:下载图片时需要的url strFilePath:下载图片的位置(/home/XXX/YYY.png) */ void ThorPromote::downloadFileFromUr ...
- Java-->利用URL类下载图片
--> 通过get 请求访问图片地址,将通过服务器响应的数据(即图片数据)存到本地文件中... --> HttpURLConnectionUtil 工具类 package com.drag ...
- php下载图片到本地
写了一天,就写了这么点代码,凑合用吧. #saveImage.php<?php /** * 图片下载方法,提供两种图片保存方式: * 1.按照图片自带的名称保存 * 2.按照自定义文件名保存 * ...
- 通过HttpURLConnection下载图片到本地--下载附件
一.背景说明 现在我做的系统中,需要有一个下载附件的功能,其实就是下载图片到本地中.相应的图片保存在多媒体系统中,我们只能拿到它的资源地址(url),而不是真实的文件. 这里记录的是下载单个图片.下篇 ...
- Java学习笔记——IO操作之以图片地址下载图片
以图片地址下载图片 读取给定图片文件的内容,用FileInputStream public static byte[] mReaderPicture(String filePath) { byte[] ...
随机推荐
- apply和call的区别在哪里
apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性. Function.apply(obj,args)方法能接收两个参数obj:这个对象将代替Function类里this对象args:这 ...
- uva 12284 直接判断
思路:见代码 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm ...
- Jquery Validate验证是否为图片格式
//验证是否为图片 jQuery.validator.addMethod("checkPic", function (value, element) { var filepath ...
- 关于inodes占用100%的问题及解决方法
#df shows no file systems processedPosted by John Quaglieri on 27 July 2012 07:26 AMA df -m command ...
- JNA参数传递问题,Java数组
版权声明:本文为博主原创文章,未经博主允许不得转载. 本文主要讲述使用JNA模拟结构体并将结构体数组作为参数传递给对应的方法. C语言结构体定义如下: typedef struct Rect { in ...
- C#右键复制路径
using System;//Environment using System.Windows.Forms; //add referece of System.Windows.Forms :DataF ...
- Android实现抽奖转盘
一.SurfaceView认识及的应用的思路 SurfaceView继承自(extends)View,View是在UI线程中进行绘制: 而SurfaceView是在一个子线程中对自己进行绘制,优势:避 ...
- Jquery操作radio,checkbox,select表单操作实现代码
一 .Select jQuery获取Select选择的Text和Value: 1. $("#select_id").change(function(){//code...}); / ...
- UI布局
1,初始化控件一般在onCreate()中完成,由于构造器中尚未完成控件加载,不能在其内初始化控件. 2,Activity子类必须含有无参构造.Intent.startActivity()方法调用的是 ...
- (转)Centos5.5安装MONO2.10.8和Jexus 5.0开启Linux平台.net应用新篇章
注:本文只做本人记录使用,也可供大家参考,有兴趣的可以一起讨论. 安装步骤 1.yum –y update 2.安装Mono源码安装需要的库 yum -y install gcc gcc-c++ bi ...