您的位置:

java下载图片,java下载图片保存到指定目录下,返回url

本文目录一览:

Java编写一下图片下载程序?

楼上的写的没错,不过感觉太麻烦了,用hutool工具包来写个方法

HttpUtil.downloadFile("", new File("F://demo4/baidu_logo.png"));

第一个参数为百度logo图片,第二个为我本地下载位置,下载结果如图

能不能用JAVA编写一个程序从网上下载一张图片呢?求完整程序!

package com.capinfotech.net; 

     

import java.io.ByteArrayOutputStream; 

import java.io.File; 

import java.io.FileOutputStream; 

import java.io.IOException; 

import java.io.InputStream; 

import java.net.HttpURLConnection; 

import java.net.URL; 

     

public class ImageRequest { 

     

     

    public static void main(String[] args) throws IOException { 

        URL url = new URL(""); 

        HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 

        InputStream inputStream = conn.getInputStream();   //通过输入流获得图片数据 

        byte[] getData = readInputStream(inputStream);     //获得图片的二进制数据 

             

        File imageFile = new File("tupian.jpg");   

        FileOutputStream fos = new FileOutputStream(imageFile);    

        fos.write(getData); 

        fos.close(); 

             

        System.out.println(" read picture success"); 

    } 

     

    public static  byte[] readInputStream(InputStream inputStream) throws IOException { 

        byte[] buffer = new byte[1024]; 

        int len = 0; 

        ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

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

            bos.write(buffer, 0, len); 

        } 

             

        bos.close(); 

        return bos.toByteArray(); 

    } 

}

java如何从数据库中下载以二进制存储的图片

/**

* Created by IntelliJ IDEA.

* User: ljt

* Date: 2003-3-31

* Time: 18:51:38

* To change this template use Options | File Templates.

*/

import oracle.jdbc.driver.OraclePreparedStatement;

import oracle.jdbc.driver.OracleResultSet;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.Statement;

import java.sql.Clob;

public class TestOpenDoc {

public OracleResultSet ors = null; //**这里rs一定要用Oracle提供的

public OraclePreparedStatement opst = null; //**PreparedStatement用

public Connection conn = null;

public Statement stmt = null;

public TestOpenDoc() {

}

public boolean getConnect() {

//这是我的数据库所在

String serverName = "prosrv";

try {

Class.forName("oracle.jdbc.driver.OracleDriver");

String url = "jdbc:oracle:thin:@" + serverName + ":1521:BOHDATA";

conn = DriverManager.getConnection(url, "appuser", "appuser");

}

catch (Exception e) {

System.out.println(e);

return false;

}

return true;

}

public static void main(String[] args) {

TestOpenDoc test = new TestOpenDoc();

if (!test.getConnect()) {

System.out.println("数据库连结错误");

return ;

}

try{

test.conn.setAutoCommit(false);

byte a[] = null; //**将测试文件test.doc读入此字节数组

java.io.FileInputStream fin = null;

java.io.FileOutputStream fout = null;

//Oracle提供的

try {

java.io.File f1 = new java.io.File("c:/test.doc");

java.io.File f2 = new java.io.File("d:/testout.doc"); //**从BLOB读出的信息写

//入该文 件,和源文件对比测试用

fin = new java.io.FileInputStream(f1);

fout = new java.io.FileOutputStream(f2);

int flength = (int) f1.length(); //**读入文件的字节长度

System.out.println("file length::" + flength);

a = new byte[flength];

int i = 0;

int itotal = 0;

//* 将文件读入字节数组

for (; itotal flength; itotal = i + itotal) {

i = fin.read(a, itotal, flength - itotal);

}

fin.close();

System.out.println("read itotal::" + itotal);

//**注意Oracle的 BLOB一定要用EMPTY_BLOB()初始化

String mysql =

"insert into filelist (FileName,FileSize,FileBody) values (?,?,EMPTY_BLOB())";

OraclePreparedStatement opst = (OraclePreparedStatement) test.conn.

prepareStatement(mysql);

opst.setString(1, "wordtemplate2");

opst.setInt(2, flength);

opst.executeUpdate();

opst.clearParameters();

// /**插入其它数据后,定位BLOB字段

mysql = "select filebody from filelist where filename=?";

opst = (OraclePreparedStatement) test.conn.prepareStatement(mysql);

opst.setString(1, "wordtemplate2");

OracleResultSet ors = (OracleResultSet) opst.executeQuery();

if (ors.next()) {

oracle.sql.BLOB blob = ors.getBLOB(1); //**得到BLOB字段

int j = blob.putBytes(1, a); //**将字节数组写入BLOB字段

System.out.println("j:" + j);

test.conn.commit();

ors.close();

Clob clob;

clob = ors.getClob("");

String str;

str = clob.toString();

str = clob.getSubString(0L,(int)clob.length());

System.out.println(str);

}

System.out.println("insert into ok");

byte b[] = null; //**保存从BLOB读出的字节

opst.clearParameters();

mysql = "select filebody from filelist where filename=?";

opst = (OraclePreparedStatement) test.conn.

prepareStatement(mysql);

opst.setString(1, "wordtemplate2");

ors = (OracleResultSet) opst.executeQuery();

if (ors.next()) {

oracle.sql.BLOB blob2 = ors.getBLOB(1);

System.out.println("blob2 length:" + blob2.length());

b = blob2.getBytes(1, flength); //**从BLOB取出字节流数据

System.out.println("b length::" + b.length);

test.conn.commit();

}

ors.close();

// 将从BLOB读出的字节写入文件

fout.write(b, 0, b.length);

fout.close();

System.out.println("write itotal::" + b.length);

}

catch (Exception e) {

System.out.println("errror :" + e.toString());

e.printStackTrace();

}

finally { //**关闭所有数据联接

test.conn.commit();

}

}

catch(Exception e){

System.out.println(e);

}

}

}

java从服务器下载图片怎么讲图片保存到本地的sdcard上

ublic HttpServletResponse download(String path, HttpServletResponse response) {

try {

// path是指欲下载的文件的路径。

File file = new File(path);

// 取得文件名。

String filename = file.getName();

// 取得文件的后缀名。

String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

// 以流的形式下载文件。

InputStream fis = new BufferedInputStream(new FileInputStream(path));

byte[] buffer = new byte[fis.available()];

fis.read(buffer);

fis.close();

// 清空response

response.reset();

// 设置response的Header

response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));

response.addHeader("Content-Length", "" + file.length());

OutputStream toClient = new BufferedOutputStream(response.getOutputStream());

response.setContentType("application/octet-stream");

toClient.write(buffer);

toClient.flush();

toClient.close();

} catch (IOException ex) {

ex.printStackTrace();

}

return response;

}

java关于下载图片。

URL url = new URL("图片地址");

File outFile = new File(“图片保存到本地路径”);

OutputStream os = new FileOutputStream(outFile);

InputStream is = url.openStream();

byte[] buff = new byte[1024];

while(true) {

int readed = is.read(buff);

if(readed == -1) {

break;

}

byte[] temp = new byte[readed];

System.arraycopy(buff, 0, temp, 0, readed);

os.write(temp);

}

is.close();

os.close();

java中怎么把当前项目中images文件夹中的图片下载到本地磁盘中??

将项目中images文件夹中文件的绝对路径作为超链接,点击链接就可以下载了。至于下载到本地那个磁盘就是用户自己选择了。