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. 《算法导论》习题解答 Chapter 22.1-2(邻接矩阵与链表)

    链表如图: 矩阵: 1 2 3 4 5 6 7 1 0 1 1 0 0 0 0 2 1 0 0 1 1 0 0 3 1 0 0 0 0 1 1 4 0 1 0 0 0 0 0 5 0 1 0 0 0 ...

  2. 重构3-Pull Up Method(方法上移)

    上移方法(Pull Up Method)重构是将方法向继承链上层迁移的过程.用于一个方法被多个实现者使用时 public abstract class Vehicle { // other metho ...

  3. Java Script基础(七) HTML DOM模型

    一.HTML DOM. HTML DOM的特性和方法是专门针对HTML的,HTML中的每个节点都是一个对象,通过访问属性和方法的方式,让一些DOM操作更加简便,在HTML DOM中有专门用来处理白哦个 ...

  4. DML 数据操纵语言

    1.INSERT(插入)语言结构 INSERT INTO table(表名)(要插入的列名) VALUES(要插入的具体值): table:要插入数据的表的表名 column[,column]:表中要 ...

  5. asp.net在word页眉插入条形码

    条形码控件需要引用MSBCODE9.OCX组件.该组件的默认位置是C:\Program Files\Microsoft Office\Office12\2052\MSBCODE9.OCX using  ...

  6. Centos7.5 搭建Red5视频直播服务器

    一.安装java环境 yum install java-1.7.0-openjdk 详细步骤请参考 http://www.cnblogs.com/java-qzq/p/5845509.html 我的这 ...

  7. 编译cordova-android代码

    开发cordova-android的插件得引入cordova的jar包,网上打包好的参差不齐,也不一定是最新版本,下面我们自己打包 cordova的jar包. 1.clone代码 cordova-an ...

  8. 转: Android官方培训课程中文版(v0.9.5)

    转: https://segmentfault.com/a/1190000004279679 1. 胡凯 tx SNG的一个开发者. http://hukai.me/android-training- ...

  9. android四种更新UI的方法

    笔记: // 使用handler.post(Runnable)更新UI public void updateUI_Fun1() { new Thread() { public void run() { ...

  10. 找不到命名空间命名空间:System.Windows.Forms

    System.Windows.Forms在system.windows.forms.dll中.需要添加引用.在解决方案资源管理器中的引用上单击右键,选择添加引用.找到System.windows.fo ...