java web 从服务器上下载图片资料

package com.Action;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

public class HttpUtils {

public static final String URL_PATH = "http://cs.pulaipuwang.com/yesilovepjustdoit2014/PartnerImg/1401125174761qiang.jpg";       
//实例图片,实际开发中可能是获得服务器上的所有图片,或者部分图片,不可能是具体某一张图片

public HttpUtils() {

// TODO Auto-generated constructor stub

}

//把从服务器获得图片的输入流InputStream写到本地磁盘

public  static void saveImageToDisk() {

InputStream inputStream = getInputStream();

byte[] data = new byte[1024];

int len = 0;

FileOutputStream fileOutputStream = null;

try {

fileOutputStream = new FileOutputStream("F:\\test2.jpg");

while ((len = inputStream.read(data)) != -1) {

fileOutputStream.write(data, 0, len);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

if (inputStream != null) {

try {

inputStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

if (fileOutputStream != null) {

try {

fileOutputStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

// 从服务器获得一个输入流(本例是指从服务器获得一个image输入流)

public static  InputStream getInputStream() {

InputStream inputStream = null;

HttpURLConnection httpURLConnection = null;

try {

URL url = new URL(URL_PATH);

httpURLConnection = (HttpURLConnection) url.openConnection();

// 设置网络连接超时时间

httpURLConnection.setConnectTimeout(3000);

// 设置应用程序要从网络连接读取数据

httpURLConnection.setDoInput(true);

httpURLConnection.setRequestMethod("GET");

int responseCode = httpURLConnection.getResponseCode();

if (responseCode == 200) {

// 从服务器返回一个输入流

inputStream = httpURLConnection.getInputStream();

System.out.println(inputStream+"**********************");

}

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return inputStream;

}

public static void main(String args[]) {

// 从服务器端获得图片,保存到本地

//    saveImageToDisk();

File file= new File("C://Program Files//Apache Software Foundation//Tomcat 6.0//webapps//plpwmanagers//DpImg//");    //得到服务器上DpImg文件下所有图片

String test[];

test=file.list();          //将每张图片依次存放到 test 数组中

System.out.println(test.length);

for(int i=0;i<test.length;i++)

{

System.out.println(test[i]);

}

}

}

java web 从服务器上下载图片资料,布布扣,bubuko.com

时间: 2025-01-04 05:54:22

java web 从服务器上下载图片资料的相关文章

从服务器上下载图片

java从数据库中获取图片 qq:1093619789 任务:实现图片在linux上根据时间来切换 public boolean findByWinter() throws IOException {// SELECT // IssueUserNo,Pic Boolean picType = false; // FROM // T_BgGraph // where PicID=1 /eyou/ui/image/ String sql = "select Top 1 * from T_BgGrap

从服务器上下载图片到本地

//从服务器下载IMG资源 private IEnumerator DownLoadToLocalIMG(string url, string strname) { Debug.Log("从服务器下载资源img:" + strname); //url编码 WWW.EscapeURL(url); //访问url WWW www = new WWW(url); //url解码 WWW.UnEscapeURL(url); //根据URL获取文件的名字. string filename = u

Android从网络上下载图片实现

1.背景介绍 网络上图片的请求,是我们最常见的网络请求之一,不亚于对json/xml数据的请求.一般要展示给用户看的,都不会是纯粹的文字,往往都是图文信息.而在移动互联网时代,图文又往往需要最新的资讯,数据都是从网络上获取. 像我们都在使用的微信,它的朋友圈中就好多图文信息:使用的新浪微博,用户的图标也是图片信息,等等诸如此类.由此可见,对于图片的请求处理,非常重要,我们做开发的应该掌握.今天介绍一下笔者在开发Android项目过程中使用过的一些代码. 2.思路分析 (1)取得与服务器的连接 (

Android 异步从网络上下载图片

package com.example.android_asynctask; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClie

集合差集 哈希表 比较数据库中的图片和服务器上的图片,将服务器上的垃圾图片删除

SSH 框架下code: public String deleRubbishAd(){ int deleADcount = 0; rubbishADtp = configDao.rubbishADtp(); //数据库中的广告图片集合 Map<Object,Object> shujuku= new HashMap<Object,Object>(); File adfile = new File("C://Program Files//Apache Software Fou

从ftp服务器上下载文件

从ftp服务器上下载文件 FTP服务器(File Transfer Protocol Server)是在互联网上提供文件存储和访问服务的计算机,它们依照FTP协议提供服务. FTP是File Transfer Protocol(文件传输协议).顾名思义,就是专门用来传输文件的协议.简单地说,支持FTP协议的服务器就是FTP服务器. 那么怎样从ftp服务器上下载文件呢?具体操作如下: ftpget -u zyx -p 123456  192.168.1.156  /hello ftpget :指令

Java通过FTP服务器上传下载文件的解决方案

对于使用文件进行交换数据的应用来说,使用FTP 服务器是一个很不错的解决方案.本文使用Apache Jakarta Commons Net(commons-net-3.3.jar)基于FileZilla Server服务器实现FTP服务器上文件的上传/下载/删除等操作. 关于FileZilla Server服务器的详细搭建配置过程,详情请见FileZilla Server安装配置教程.之前有朋友说,上传大文件(几百M以上的文件)到FTP服务器时会重现无法重命名的问题,但本人亲测上传2G的文件到F

java+web+大文件上传下载

文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用HTML5的API,对文件上传进行渐进式增强:     * iframe上传  * ajax上传  * 进度条  * 文件预览  * 拖放上传 1.1 传统形式 文件上传的传统形式,是使用表单元素file,参考 http://www.ruanyifeng.com/blog/2012/08/file_

java web 网站头像上传处理 (springmvc +bootstrap+cropper)

制作头像上传.请根据您的实际需求,修改代码,不完全正确,仅供参考! 前端页面设计使用bootstrap ,头像预览和剪裁工具使用cropper 后台使用springmvc. 现在来看前端的页面设计 前端页面设计,自然需要bootstrap .jqury 和cropper ,这可以自行去网上百度查找 剪裁效果图 html 文件 <!DOCTYPE html> <html> <head lang="en"> <meta charset="