本文目錄一覽:
- 1、Java編寫一下圖片下載程序?
- 2、能不能用JAVA編寫一個程序從網上下載一張圖片呢?求完整程序!
- 3、java如何從數據庫中下載以二進制存儲的圖片
- 4、java從服務器下載圖片怎麼講圖片保存到本地的sdcard上
- 5、java關於下載圖片。
- 6、java中怎麼把當前項目中images文件夾中的圖片下載到本地磁盤中??
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文件夾中文件的絕對路徑作為超鏈接,點擊鏈接就可以下載了。至於下載到本地那個磁盤就是用戶自己選擇了。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/188524.html