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. CSS3实战手册(第3版)(影印版)

    <CSS3实战手册(第3版)(影印版)> 基本信息 原书名:CSS3: The Missing Manual, 3E 作者: David Sawyer McFarland 出版社:东南大学 ...

  2. 1. SQL Server服务器监控实现方法

    对于服务器的监控,和对数据库的监控,很少有合二为一的工具,如果有的话,一般是付费软件,或者自行开发的工具.所以如果不想购买软件,也不想花精力去开发的话,可以结合一些免费/开源的工具.自定义脚本,来完成 ...

  3. 0. SQL Server监控清单

    数据库服务器的监控可大致分为两类: (1) 状态监控:数据库服务器有没有在健康地运行? (2) 性能监控:健康运行的同时,有没有性能问题?可不可以更快些? 一. 服务器 1. 状态监控 (1) 服务器 ...

  4. React Native(ios)项目中logo,启动屏设置

    由于logo和启动屏尺寸多,react native(ios)中没有命令可以自动生成各种的尺寸,所以可以使用以下办法:在ionic项目中生成(使用命令:ionic resources)后,再粘贴到re ...

  5. Java操作文件夹的工具类

    Java操作文件夹的工具类 import java.io.File; public class DeleteDirectory { /** * 删除单个文件 * @param fileName 要删除 ...

  6. Matlab的GUI参数传递方式总结

    MATLAB GUI传递方式 1.全局变量: 2.作为函数的参数传递: 3.利用控件的userdata数据: 4.为handles结构体添加新字段: 5.setappdata函数为句柄添加数据: 6. ...

  7. JQuery Mobile 实战一

    今天我们来使用JQuery Mobile来开发一个web mobile app. 要实现的如下所示效果: 开始: 第一步:添加JS包等引用,直接去官网下载最新的JQuery Mobile 包,http ...

  8. iOS开发中的那些小技巧

    前言:今天在写代码的过程中遇到一个需要修改系统navigationBar的背景色,我起初用的是barTintColor去修改但是防不住系统点击按钮的时候会有一个渲染高亮的效果,调了好久没有达到自己想要 ...

  9. MySQL 5.7 Zip 安装(win7)

    参考官方文档 http://dev.mysql.com/doc/refman/5.7/en/windows-install-archive.html 2.3.5.1 Extracting the In ...

  10. 洛谷 P3399 丝绸之路

    题目背景 张骞于公元前138年曾历尽艰险出使过西域.加强了汉朝与西域各国的友好往来.从那以后,一队队骆驼商队在这漫长的商贸大道上行进,他们越过崇山峻岭,将中国的先进技术带向中亚.西亚和欧洲,将那里的香 ...