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地址下载图片到本地的更多相关文章

  1. java根据图片的url地址下载图片到本地

    package com.daojia.haobo.aicircle.util; import sun.misc.BASE64Encoder; import java.io.*; import java ...

  2. QTP 通过URL地址下载文件到本地(转)

    While automation, you may come to situations where you need to need to download a file on clicking a ...

  3. 通过scrapy内置的ImagePipeline下载图片到本地、并提取本地保存地址

    1.通过scrapy内置的ImagePipeline下载图片到本地 2.获取图片保存本地的地址 1.通过scrapy内置的ImagePipeline下载图片到本地 1)在settings.py中打开  ...

  4. scrapy中的ImagePipeline下载图片到本地、并提取本地的保存地址

    通过scrapy内置到ImagePipeline下载图片到本地 在settings中打开 ITEM_PIPELINES的注释,并在这里面加入 'scrapy.pipelines.images.Imag ...

  5. QT通过url下载图片到本地

    /* strUrl:下载图片时需要的url strFilePath:下载图片的位置(/home/XXX/YYY.png) */ void ThorPromote::downloadFileFromUr ...

  6. Java-->利用URL类下载图片

    --> 通过get 请求访问图片地址,将通过服务器响应的数据(即图片数据)存到本地文件中... --> HttpURLConnectionUtil 工具类 package com.drag ...

  7. php下载图片到本地

    写了一天,就写了这么点代码,凑合用吧. #saveImage.php<?php /** * 图片下载方法,提供两种图片保存方式: * 1.按照图片自带的名称保存 * 2.按照自定义文件名保存 * ...

  8. 通过HttpURLConnection下载图片到本地--下载附件

    一.背景说明 现在我做的系统中,需要有一个下载附件的功能,其实就是下载图片到本地中.相应的图片保存在多媒体系统中,我们只能拿到它的资源地址(url),而不是真实的文件. 这里记录的是下载单个图片.下篇 ...

  9. Java学习笔记——IO操作之以图片地址下载图片

    以图片地址下载图片 读取给定图片文件的内容,用FileInputStream public static byte[] mReaderPicture(String filePath) { byte[] ...

随机推荐

  1. IQueryable与IEnumberable的区别

    IEnumerable接口 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代.也就是说:实现了此接口的object,就可以直接使用foreach遍历此object: IQueryable 接口 ...

  2. linux初学 :简易的shell脚本

    什么是shell Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的 Shell有两种执行命令的方式: 交互式(Interacti ...

  3. hdu 4705 排列组合

    思路:枚举能是A,B,C在一条简单路径上的中点. 计算多少个几何能满足.在用总数减去 #pragma comment(linker, "/STACK:16777216") #inc ...

  4. hdu 2121 , hdu 4009 无定根最小树形图

    hdu 2121 题目:给出m条有向路,根不确定,求一棵最小的有向生成树. 分析:增加一个虚拟节点,连向n个节点,费用为inf(至少比sigma(cost_edge)大).以该虚拟节点为根求一遍最小树 ...

  5. Backbone.js学习之Router

    官方文档的解释: Web applications often provide linkable, bookmarkable, shareable URLs for important locatio ...

  6. django 学习-15 .Django文件上传(用户注册)

    1.vim blog/views.py from django.shortcuts  import  render_to_responsefrom django.http   import HttpR ...

  7. javascript遍历控件(实例详解)

    js遍历页面控件, 代码如下 复制代码  var inputArr = document.forms[0];   for( var i = 0; i < inputArr.length; i++ ...

  8. Cocos2d-x文本菜单

    文本菜单是菜单项只是显示文本,文本菜单类包括了MenuItemLabel.MenuItemFont和MenuItemAtlasFont.MenuItemLabel是个抽象类,具体使用的时候是使用Men ...

  9. js判断输入是否为空,获得输入的类型

    使用typeof算法 typeof的运算数未定义,返回的就是 "undefined". 下面定义x为运算数: 有一下5种情况: 运算数为数字 typeof(x) 返回的就是 &qu ...

  10. javascript构造函数小记

    function outer(){ function inner(){} return inner; } var a=outer(); var b=outer(); var c=new outer() ...